1、SSH版本信息
1、aopalliance-1.0.jar 2、aspectjweaver-1.7.3.jar 3、commons-dbcp2-2.4.0.jar
4、commons-logging-1.2.jar 5、commons-pool2-2.6.0.jar 6、jackson-annotations-2.9.6.jar
7、jackson-core-2.9.6.jar 8、jackson-databind-2.9.6.jar 9、log4j-core-2.11.0.jar
10、mybatis-3.4.6.jar 11、mybatis-spring-1.3.2.jar 12、mysql-connector-java-8.0.11.jar
13、spring-aop-5.0.6.RELEASE.jar 14、spring-beans-5.0.6.RELEASE.jar 15、spring-context-5.0.6.RELEASE.jar
16、spring-core-5.0.6.RELEASE.jar 17、spring-expression-5.0.6.RELEASE.jar 18、spring-jdbc-5.0.6.RELEASE.jar
19、spring-tx-5.0.6.RELEASE.jar 20、spring-web-5.0.6.RELEASE.jar 21、spring-webmvc-5.0.7.RELEASE.jar
2、配置文件
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>BookSystemSSM2</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- POST提交过滤器 UTF-8 -->
<filter>
<filter-name>encoding</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>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
</web-app>
spring-servlet.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 支持注解方式使用springmvc -->
<mvc:annotation-driven/>
<!-- 自动扫描controller -->
<context:component-scan base-package="controller"/>
<!-- 配置一个springmvc框架的视图解析器 ,当使用容器外跳转(redirect)时就不管用了-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 通过setter方法注入前缀-->
<property name="prefix" value= ""/>
<!-- 通过setter方法注入后缀-->
<property name="suffix" value= ".jsp"/>
</bean>
<!-- 支持使用json方式传递数据-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html; charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
</beans>
applicationContext.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:component-scan base-package="dao"></context:component-scan>
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源datasource -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<!-- 初始化连接数量; -->
<property name="initialSize" value="0" />
<!-- 最大并发连接数 -->
<property name="maxTotal" value="20" />
<!-- 最大空闲连接数 -->
<property name="maxIdle" value="20" />
<!-- 最小空闲连接数 -->
<property name="minIdle" value="0" />
<!-- 最大等待时长 -->
<property name="maxWaitMillis" value="60000" />
<!-- 超过时间限制是否回收 -->
<property name="removeAbandonedOnBorrow" value="true" />
<!-- 超过时间限制多长; -->
<property name="removeAbandonedTimeout" value="180"/>
<!-- 数据源连接参数配置; -->
<property name="username" value="${db.username}"/>
<property name="url" value="${db.url}"/>
<property name="password" value="${db.password}"/>
<property name="driverClassName" value="${db.driverClassName}"/>
</bean>
<!-- 配置SessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- <property name="mapperLocations" value="classpath:mapper/*.xml" /> -->
<property name="mapperLocations">
<list>
<value>classpath:config/*.xml</value>
</list>
</property>
</bean>
<!-- 自动扫描mapper接口,注入sqlSessionFactory -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> -->
<!-- 定义切面 -->
<aop:config>
<aop:pointcut expression="execution(* dao.*.* (..))" id="txPointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
<!-- 声明式事务 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="isValid" propagation="REQUIRED" read-only="true"/>
<tx:method name="add" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
</beans>
db.properties
db.username=jack
db.url=jdbc:mysql://localhost:3306/booksystem?serverTimezone=Asia/Shanghai&useSSL=true
db.password=12345678
db.driverClassName=com.mysql.cj.jdbc.Driver
BookMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.BookMapper">
<select id="find" parameterType="String" resultType="entity.Book">
select * from book where name like #{pattern}
</select>
</mapper>
UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.UserMapper">
<select id="isvalid" parameterType="entity.User" resultType="entity.User">
select * from user where name=#{name} and pwd=#{pwd}
</select>
</mapper>