1.导入jar包
spring+springmvc的jar包
mybatisjar包
mybatis-spring-1.3.1.jar
c3p0的jar包
log4j的jar包
jstl的jar包
jacksonjar包
数据库的jar的包
Common-fileupload
2.添加配置文件
2.1 添加log4j.properties配置文件
由于MyBatis依赖与log4j输出sql语句信息,所以需要配置log4j配置文件。
#设置输出级别和输出位置
log4j.rootLogger=debug,Console
#设置控制台相关的参数
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#设置MyBatis的输出内容
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
2.2 添加mybatis.xml映射文件
在SSM集合框架中,可以省略mybatis配置文件,也可以添加,只不过需要在sqlSessionFactory中配置configLocation属性即可。
<?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>
<!-- 通过全局配置文件设置延迟加载 -->
<settings>
<!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。 -->
<setting name="aggressiveLazyLoading" value="false" />
<!-- 开启或者关闭二级缓存。 -->
<setting name="cacheEnabled" value="true"/>
</settings>
<!-- 取别名 -->
<typeAliases>
<package name="com.znsd.ssm.entities" />
</typeAliases>
</configuration>
2.3 添加springmvc.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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- SpringMVC的配置文件,只扫描@Controller和@ControllerAdvice注解修饰的类 -->
<context:component-scan base-package="com.znsd.ssm" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!-- 视图解析器:将逻辑视图转发到对应的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置文件上传组件 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 指定默认的编码格式 -->
<property name="defaultEncoding" value="UTF-8" />
<!-- 指定允许上传的文件大小,单位Byte-->
<property name="maxUploadSize" value="512000" />
</bean>
<!-- 将静态资源交由tomcat来处理 -->
<mvc:default-servlet-handler />
<!-- 注册类型转换器 -->
<mvc:annotation-driven />
</beans>
2.4 添加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" 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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- SpringIOC的配置文件,排除@Controller和@ControllerAdvice注解修饰的类 -->
<context:component-scan base-package="com.znsd.ssm">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>
<!-- 配置c3p0连接池属性 -->
配置数据库跟用户名 密码
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/work" />
<property name="user" value="root" />
<property name="password" value="123456" />
<!-- 队列中的最小连接数 -->
<property name="minPoolSize" value="15"></property>
<!-- 队列中的最大连接数 -->
<property name="maxPoolSize" value="25"></property>
<!-- 当连接耗尽时创建的连接数 -->
<property name="acquireIncrement" value="15"></property>
<!-- 等待时间 -->
<property name="checkoutTimeout" value="10000"></property>
<!-- 初始化连接数 -->
<property name="initialPoolSize" value="20"></property>
<!-- 最大空闲时间,超出时间连接将被丢弃 -->
<property name="maxIdleTime" value="20"></property>
<!-- 每隔60秒检测空闲连接 -->
<property name="idleConnectionTestPeriod" value="60000"></property>
</bean>
<!-- Spring扫描所有的mapper文件(属性xml映射文件) -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 设置连接池对象 -->
<property name="dataSource" ref="dataSource" />
<!-- Mappper所在的包路径 -->
<property name="mapperLocations" value="classpath:com/znsd/ssm/mapper/mapperXml/*.xml" />
<!-- 指定mybatis的配置文件 -->
<property name="configLocation" value="classpath:mybatis.xml"></property>
</bean>
<!-- Spring 扫描sql语句接口包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 接口sql文件dao包所在的包路径 -->
<property name="basePackage" value="com.znsd.ssm.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- 配置事务 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 事务的通知方式 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="search*" propagation="REQUIRED" read-only="true" />
<tx:method name="query*" propagation="REQUIRED" read-only="true" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="submit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- AOP切面拦截事务 -->
切面拦截加在服务层
<aop:config>
<aop:pointcut id="serviceMethod"
expression="execution(* com.znsd.ssm.service.*.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config>
</beans>
2.5 配置web.xml映射文件
整合所有配置文件
<!-- 开启编码过滤器 -->
<filter>
<description>字符集过滤器</description>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<description>字符集编码</description>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring的监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 配置SpringMVC的处理请求的Servlet -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 表示系统启动时自动加载这个servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 设置是否支持put和delete请求 -->
<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>
3.添加属性类
4.配置属性类映射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="对应的接口">
<!-- 开启二级缓存 -->
<cache type="org.mybatis.caches.ehcache.EhcacheCache" >
<property name="timeToIdleSeconds" value="3600" /><!--1 hour -->
<property name="timeToLiveSeconds" value="3600" /><!--1 hour -->
<property name="maxEntriesLocalHeap" value="1000" />
<property name="maxEntriesLocalDisk" value="10000000" />
<property name="memoryStoreEvictionPolicy" value="LRU" />
</cache>
<resultMap type="com.znsd.ssm.entities.Student" id="studentMap">
<id column="studentNo" property="studentNo" />
<result column="studentName" property="stuName" />
<result column="loginPwd" property="loginPwd" />
<result column="sex" property="sex" />
<result column="phone" property="phone" />
<result column="bornDate" property="bornDate" />
<result column="email" property="email" />
<result column="identityCard" property="identityCard" />
<result column="age" property="age" />
<!-- 通过association来设置外键关联,多对一的关系 -->
<association property="grade" resultMap="com.znsd.ssm.dao.GradeMapper.gradeMap">
</association>
<!-- 分布查询 -->
<!-- <association property="grade" column="gradeId" -->
<!-- select="com.znsd.ssm.dao.GradeMapper.selectOne"> -->
<!-- </association> -->
</resultMap>
<!-- 查询学生列表 -->
<select id="selectList" resultMap="studentMap">
select * from students s
inner join grade g on s.gradeId = g.gradeId;
</select>
<!-- 查询某个学生 -->
<select id="selectOne" parameterType="int" resultType="com.znsd.ssm.entities.Student">
select * from students s
where studentNo = #{studentNo};
</select>
<!-- 插入学生信息 -->
<insert id="insertStudent" parameterType="com.znsd.ssm.entities.Student" useGeneratedKeys="true" keyProperty="studentNo">
insert into students(studentName, loginPwd, sex,phone,bornDate,email,identityCard,age,gradeid)
values(#{studentName},#{loginPwd}, #{sex}, #{phone}, #{bornDate},#{email},#{identityCard},#{age},#{gradeid})
</insert>
<!-- 更新学生信息 -->
<update id="updateStudent" parameterType="com.znsd.ssm.entities.Student">
update students set studentName=#{studentName},loginPwd=#{loginPwd},sex=${sex},phone=#{phone},bornDate=#{bornDate},email=#{email},identityCard=#{identityCard},age=#{age},gradeid=#{gradeid}
where studentNo = #{studentNo}
</update>
<!-- 删除学生信息 -->
<delete id="deleteStudent" parameterType="int">
delete from students where studentNo = #{studentNo}
</delete>
</mapper>
5.添加属性类映射文件Dao层接口
方法名与xml映射文件id一致
public List<Student> selectList();
public Student selectOne(Integer studentNo);
public long insertStudent(Student student);
public int updateStudent(Student student);
public int deleteStudent(Integer studentNo);