SSM框架学习笔记

    xiaoxiao2021-03-25  156

    第一次做SSM框架项目 学习笔记

    首先先介绍什么是SSM?

    Spring+SpringMVC+Mybatis 就是SSM的简称

    Spring

    一,Spring是一个开源框架,它能降低开发企业应用程序的复杂性,降低了开发成本并整合了各种流行框架,它以IOC和AOP(面向切面编程)二种先进的技术基础完美的简化了企业级开发的复杂度。包含以下七个模块

    1,Spring core 模块

    2,Context模块

    3,AOP模块

    4,DAO模块

    5,ORM映射模块

    6,Web模块

    7,MVC模块

    二,SpringMVC是典型的MVC模式设计模式,围绕DispatcherServlet核心控制器展开,核心控制器就是截获请求,分发到相应的业务控制器,由业务控制器调用业务处理方法处理业务逻辑,返回一个模型和视图对象,核心控制器在根据此对象找到视图显示出结果。

    三,mybatis是支持SQL查询,储存过程和高级映射的优秀持久层框架。。MyBatis 消除

    了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索。MyBatis 可以使用简单的

    XML 或注解用于配置和原始映射,将接口和 Java 的 POJO(Plain Old Java Objects,普通的

    Java 对象)映射成数据库中的记录

    以上是对于整合框架的一个简单介绍概念性的东西大都是出自书本,下面将贴出我做的这个项目的具体配置文件,

    web.xml

    <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>

    </context-param>

    <filter>

    <filter-name>CharacterEncodingFilter</filter-name>

    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

    <init-param>

    <param-name>encoding</param-name>

    <param-value>utf-8</param-value>

    </init-param>

    <init-param>

    <param-name>forceEncoding</param-name>

    <param-value>true</param-value>

    </init-param>

    </filter>

    <filter-mapping>

    <filter-name>CharacterEncodingFilter</filter-name>

    <url-pattern>/*</url-pattern>

    </filter-mapping>

    <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

    <servlet>

    <servlet-name>WebApp</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath:spring/mvc-dispatcher-servlet.xml</param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>

    </servlet>

    <servlet-mapping>

    <servlet-name>WebApp</servlet-name>

    <url-pattern>*.do</url-pattern>

    </servlet-mapping>

     

    sqlMapConfig.xml

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 全局setting配置,根据需要添加 --> <!-- 配置别名 --> <typeAliases> <!-- 批量扫描设置别名 --> <!-- 指定包名时,Mybatis会在包名下面搜索需要的Java Bean --> <package name="com.sports.pojo" /> </typeAliases>  <!-- 配置Mapper 备注:由于使用Spring整合mybtais的整合包进行mapper扫描,这里不需要配置了 必须遵循:mapper.xml和mapper.java文件同名且在同一目录下 <mappers></mappers> --> </configuration>

     

     

    applicationContext-dao.xml

    <beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

    http://www.springframework.org/schema/mvc

    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context-3.2.xsd

    http://www.springframework.org/schema/aop

    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

    http://www.springframework.org/schema/tx

    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <!-- 配置数据源,使用dbcp连接池 -->

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>

    <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8"/>

    <property name="username" value="root"/>

    <property name="password" value="root"/>

    <property name="maxActive" value="30"/>

    <property name="maxIdle" value="5"/>

    </bean>

    <!-- 配置SqlSessionFactory -->

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

    <!-- 数据源 -->

    <property name="dataSource" ref="dataSource"/>

    <!-- 加载mybatis的全局配置文件 -->

    <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />

    </bean>

    <!-- 配置Mapper扫描器 -->

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

    <!-- 扫描包路径,如果需要扫描多个包中间用半角逗号隔开 -->

    <property name="basePackage" value="com.roymark.mapper"/>

    <!-- 这边不能使用ref="sqlSessionFactory"原因是因为上面加载配置文件导致这边引用会报错 -->

    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />

    </bean>

    </beans>

     

    applicationContext-service.xml

    <beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

    http://www.springframework.org/schema/mvc

    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context-3.2.xsd

    http://www.springframework.org/schema/aop

    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

    http://www.springframework.org/schema/tx

    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <!-- 扫描标注@Service注解的service -->

    <context:component-scan base-package="com.roymark.service"/>

    </beans>

    applicationContext-transaction.xml

    <beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

    http://www.springframework.org/schema/mvc

    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context-3.2.xsd

    http://www.springframework.org/schema/aop

    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

    http://www.springframework.org/schema/tx

    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <!-- 事务管理器 对mybatis操作数据库事务控制,spring使用jdbc的事务控制类 -->

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

    <!-- 数据源 dataSource在applicationContext-dao.xml中配置了 -->

    <property name="dataSource" ref="dataSource" />

    </bean>

    <!-- 通知 -->

    <tx:advice id="txAdvice" transaction-manager="transactionManager">

    <tx:attributes>

    <!-- 传播行为 -->

    <tx:method name="save*" propagation="REQUIRED" />

    <tx:method name="delete*" propagation="REQUIRED" />

    <tx:method name="insert*" propagation="REQUIRED" />

    <tx:method name="update*" propagation="REQUIRED" />

    <tx:method name="find*" propagation="SUPPORTS" read-only="true" />

    <tx:method name="get*" propagation="SUPPORTS" read-only="true" />

    <tx:method name="select*" propagation="SUPPORTS" read-only="true" />

    </tx:attributes>

    </tx:advice>

    <!-- aop -->

    <aop:config>

    <aop:advisor advice-ref="txAdvice"

    pointcut="execution(* com.roymark.service.impl.*.*(..))" />

    </aop:config>

    </beans>

    mvc-dispatcher-servlet

    <beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

    http://www.springframework.org/schema/mvc

    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context-3.2.xsd

    http://www.springframework.org/schema/aop

    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

    http://www.springframework.org/schema/tx

    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">

    <property name="messageConverters">

    <list>

    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>

    </list>

    </property>

    </bean> -->

    <!-- 组件扫描器扫描这一层组要扫描处理器 -->

    <context:component-scan base-package="com.roymark.web.controller"></context:component-scan>

    <!-- 配置注解的映射器和适配器以及其他配置 -->

    <mvc:annotation-driven>

    <mvc:message-converters register-defaults="true">

    <bean class="org.springframework.http.converter.StringHttpMessageConverter">

    <property name="supportedMediaTypes">

    <list>

    <value>application/json;charset=UTF-8</value>

    <value>text/html;charset=UTF-8</value>

    <value>text/xml;charset=UTF-8</value>

    <value>text/plain;charset=UTF-8</value>

    </list>

    </property>

    </bean>

    </mvc:message-converters>

    </mvc:annotation-driven>

    <!-- 处理静态资源问题 -->

    <mvc:default-servlet-handler />

    <!-- 实现文件上传bean对象 -->

    <bean name="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <property name="defaultEncoding" value="UTF-8"/> <!-- 设置编码格式 -->

    <property name="maxUploadSize" value="1500000"/> <!-- 限制上传文件大小 -->

    <property name="uploadTempDir" value="upload/temp"/> <!-- 设置临时存放路径 -->

    </bean>

    </beans>

     

     

     

    转载请注明原文地址: https://ju.6miu.com/read-18972.html

    最新回复(0)