ssm整合
spring和springmvc是天然集成,重点整合mybatis与spring
- 由spring容器管理mybatis的mapper
- 由spring利用声明式事务(AOP)进行事务综合的管理
添加配置文件
添加依赖
在maven中添加依赖包
<!--spring依赖包-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<!--spring事务处理依赖包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-version}</version>
</dependency>
<!--json依赖包-->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
</dependency>
<!--添加处理json为javabean-->
<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!--文件下载插件-->
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<!--日志相关依赖包-->
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--连接池-->
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--mybaits与spring整合所需的依赖-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!--servlet-->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--处理时间日期格式-->
<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.9</version>
</dependency>
<!--mybatis分页依赖-->
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.8</version>
</dependency>
<!--apche用于MD5加密-->
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
为了进行版本管理,这里<version>${spring-version}</version>中我们通过版本指定统一进行版本管理
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!--用于统一版本-->
<spring-version>5.1.5.RELEASE</spring-version>
</properties>
比如这里吧spring的版本控制为5.1.5.RELEASE
编辑web.xml
在resources下新建spring文件夹用来存放spring相关的配置文件
<!--指定整个程序上下文配置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
解决字符编码以及全Http请求支持过滤器的添加
<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>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--注册一个支持所有http请求类型的过滤器-->
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
注册前端控制器DispatcherServlet
<servlet>
<!--
这里的servlet-name是有讲究的,
如果我们不去修改spring配置文件默认的位置,
那么springmvc它会去WEB-INF下面找一个叫做springmvc-servlet.xml的文件
-->
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--可以重命名springmvc的文件名-->
<param-name>namespace</param-name>
<param-value>springmvc-servlet</param-value>
</init-param>
<init-param>
<!--用来制定上下文的位置-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
注册一个请求过滤器
浏览器form表单只支持GET与POST请求,而DELETE、PUT等method并不支持,spring3.0添加了一个过滤器,可以将这些请求转换为标准的http方法,使得支持GET、POST、PUT与DELETE请求,该过滤器为HiddenHttpMethodFilter。
<!--注册一个支持所有http请求类型的过滤器-->
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
创建一个监听器
用来检查applicationContext.xml配置文件,并自动装配
编辑spring-servlet.xml
新建 spring-servlet.xml 位于 resources/spring 文件夹下
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
配置注解扫描
<!--
启动注解
同时排除了service注解
-->
<context:component-scan base-package="com.lxj">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
配置视图解析器
<!--配置一个视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
文件上传解析器
<!--文件上传解析器
id必须为multipartResolver,原因是源代码里面写死了这个名字
-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--单个文件最大上传大小-->
<property name="maxUploadSizePerFile" value="20000000"></property>
<!--指定上传编码-->
<property name="defaultEncoding" value="UTF-8"></property>
<!--定义最大文件大小 总的,单位为bytes-->
<property name="maxUploadSize" value="200000000"></property>
</bean>
静态资源处理
<!--默认的servlet处理者,进行静态资源处理-->
<mvc:default-servlet-handler/>
编辑spring-mybatis.xml
新建 springmvc-mybatis.xml 位于 resources/spring 文件夹下
配置java中mapper文件的扫描包
<context:component-scan base-package="com.lxj.mapper">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
引入数据库相关文件+c3p0的相关配置
<!--引入数据库相关的配置文件-->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!--如果有需要,可以把所有属性写到properties文件夹当中去-->
<!--c3p0连接池的私有属性-->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!--关闭连接后不自动commit-->
<property name="autoCommitOnClose" value="false"/>
<!--获取连接超时时间-->
<property name="checkoutTimeout" value="100000"/>
<!--当获取连接失败时重新连接次数-->
<property name="acquireRetryAttempts" value="2"/>
</bean>
整合spring和mybatis
- 注入mybatis的sqlSessionFactory
- 声明式的事务管理
<!--最后关键一步,整合mybatis-->
<!--
1 注入一股mybatis的sqlSessionFactory(十分关键)
2 声明式的事务管理
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--引入mappers文件-->
<!--<property name="configLocation" value="classpath:mybatis-config.xml"/>-->
<!--这就是要求所有的mapper文件必须在com/lxj/mapper之下-->
<property name="mapperLocations" value="classpath:com/lxj/mapper/**/*xml"/>
<property name="configuration">
<bean class="org.apache.ibatis.session.Configuration">
<!--可以加入驼峰命名,其它mybatis配置也就是mybatis.cfg.xml的相关配置都会转移到这里来-->
<property name="mapUnderscoreToCamelCase" value="true"/>
</bean>
</property>
<!--插件配置-->
<!--分页插件-->
<property name="plugins">
<array>
<!--分页插件的配置 拦截器实现分页功能(不再使用limit子句)-->
<bean class="com.github.pagehelper.PageInterceptor">
<!--这里的几个配置主要演示如何使用-->
<property name="properties">
<value>
helperDialect=mysql
reasonable=true
supportMethodsArguments=true
params=count=countSql
autoRuntimeDialect=true
</value>
</property>
</bean>
</array>
</property>
</bean>
<!--持久层接口-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lxj.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
</bean>
<!--事务管理 使用数据源事务管理类进行管理-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--确定事务管理的策略 transaction-manager:指向上面的transactionManage-->
<tx:advice transaction-manager="transactionManager" id="transactionAdvice">
<!--事务管理的相关值以及它的传播性-->
<tx:attributes>
<!--查询相关配置为只读 select开头或者get 或者query-->
<tx:method name="select*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<!--delete update insert add的相关操作如果出错就回滚-->
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<!--
使用aop对事务管理的范围进行织入 明确几个点
1 对哪些地方需要进行事务管理 execution书写,明确边界
2 使用什么策略去管理策略 我们使用了tx:advice全部书写与其中,
在我们的aop的advisor当中只需要去引用这个事务管理者的建议即可
-->
<aop:config>
<aop:pointcut id="txCut" expression="execution(* com.lxj.service..*.*(..))"/>
<aop:advisor advice-ref="transactionAdvice" pointcut-ref="txCut"/>
</aop:config>
采用注解进行事务管理
<!--采用注解进行事务管理,请在Service的实现类上加上@Transanctional注解-->
<tx:annotation-driven transaction-manager="transactionManager"/>
编辑spring-context.xml配置文件
该配置文件位于resources/spring下
注册spring容器
<!--需spring的基础配置-->
<!--1 spring容器注册-->
<context:annotation-config/>
自动扫描service层
<!--2 自动扫描配置-->
<context:component-scan base-package="com.lxj.service"/>
<!--3 激活aop注解方式的代理-->
<aop:aspectj-autoproxy/>
处理数据类型格式化
<!-- 消息格式转换 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="registerDefaultFormatters" value="false"/>
<property name="formatters">
<set>
<bean class="org.springframework.format.number.NumberFormatAnnotationFormatterFactory"/>
</set>
</property>
<property name="formatterRegistrars">
<set>
<bean class="org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar">
<property name="dateFormatter">
<bean class="org.springframework.format.datetime.joda.DateTimeFormatterFactoryBean">
<property name="pattern" value="yyyyMMdd"/>
</bean>
</property>
</bean>
</set>
</property>
</bean>
编辑applicationContext.xml
新建 applicationContext.xml 位于 resources/spring 文件夹下
用于整合上面三个xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
引入spring和其它整合的配置文件
-->
<import resource="classpath:spring/spring-*.xml"/>
</beans>
编辑springmvc-servlet.xml
新建 springmvc-servlet.xml 位于 resources 文件夹下
配置扫描controller的配置
<!--配置一个注解扫描的包-->
<context:component-scan base-package="com.lxj.controller"/>
配置视图解析器
<!--配置一个视图解析器
常用的内部资源视图解析器
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--1.前缀-->
<property name="prefix" value="/jsp/"/>
<!--2.后缀-->
<property name="suffix" value=".jsp"/>
</bean>
配置各种拦截器
<!--拦截器的配置-->
<!--自定义的拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<!--
/*的写法只能拦截/name方法,只能有一层,不能多层拦截
-->
<mvc:mapping path="/**/*"/>
<bean class="com.lxj.interceptors.MethodTimerInterceptor">
</bean>
</mvc:interceptor>
<mvc:interceptor>
<!--
只想拦截/user/**/*
还需开方登录权限
-->
<mvc:mapping path="/user/**/*"/>
<!--还要排除登录的URI-->
<mvc:exclude-mapping path="/user/login"/>
<bean class="com.lxj.interceptors.SessionInterceptor">
</bean>
</mvc:interceptor>
<mvc:interceptor>
<!--
只想拦截/user/**/*
还需开方登录权限
-->
<mvc:mapping path="/user/**/*"/>
<!--还要排除登录的URI-->
<mvc:exclude-mapping path="/user/login"/>
<bean class="com.lxj.interceptors.SessionInterceptor">
</bean>
</mvc:interceptor>
</mvc:interceptors>
其它配置
<!--默认的servlet处理者-->
<mvc:default-servlet-handler/>
<!--还要加上它,不然注解失效,全部都交给它去处理了-->
<mvc:annotation-driven/>