相关的配置文件:
web.xml
springmvc的配置文件:
applicationContext-springmvc.xml
spring的配置文件:
applicationContext.xml
mybatis的全局配置文件
mybatis-config.xml
数据源的部分, sql映射文件的加载放到spring的配置文件去处理
留下一些全局配置:
别名扫描
缓存
懒加载
分页
sql映射文件
*Mapper.xml(*通配,例如UserMapper)
数据库连接信息
jdbc.properties
log4j日志配置文件
log4j.properties
1.导入jar
页面相关
jstl
spring web相关
spring-web
spring-webmvc
json(3)
文件上传(2)
spring核心
core
context
spel
beans
aop相关
aop
aopalliance
aspects
aspectjweaver
数据库相关
spring-jdbc
spring-tx
数据库驱动mysql
连接池(替换dataSource接口的实现类)druid
mybatis相关
myabtis核心包及依赖包
其他:
lombok
mybatis-spring的整合包
2.配置web.xml
web应用监听器, 加载spring的配置文件
前端控制器
启动时就加载
指定springmvc配置文件的地址及名字
url-pattern : /
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ssm-my</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- web应用监听器, 加载spring的配置文件 -->
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 前端控制器
启动时就加载
指定springmvc配置文件的地址及名字
url-pattern : /
-->
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3.配置springmvc的配置文件
包扫描(只扫描controller)
三大组件<mvc:annotation-driven>
视图解析器
静态资源处理器
文件上传解析器
拦截器
消息转换器
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.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.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<!-- 包扫描(只扫描controller) -->
<context:component-scan base-package="com.zty" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 三大组件<mvc:annotation-driven> -->
<mvc:annotation-driven/>
<!-- 视图解析器 prefix配置前缀,suffix配置后缀-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 静态资源处理器,静态资源放行 -->
<mvc:default-servlet-handler/>
<!-- 文件上传解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
<!-- 拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/assets/**"/>
<mvc:exclude-mapping path="/auth/**"/>
<bean class="com.zty.interceptor.AuthInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
<!-- 消息转换器 -->
<bean id="conversionServiceFactoryBean" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.zty.converter.String2DateConverter"></bean>
<bean class="com.zty.converter.String2User"></bean>
</set>
</property>
</bean>
</beans>
===============================================
4.配置spring的配置文件
包扫描(除了controller)
加载jdbc.properties
数据源(连接池)
===========
配置sqlSessionFactory >> session
加载mybatis的核心配置文件
注入数据源
指定sql映射文件的地址
配置mapper接口
配置包扫描, 指定mapper的包名
<mybatis:>
sqlSessionFactory最开始是由sqlSessionFactoryBuilder加载mybatis核心配置文件产生的
此时, mybatis的核心配置文件中主要包含了数据源和sql映射文件
ssm整合之后, 对于数据库的访问都依赖于mybatis
UserMapper接口的代理类
sqlSession.getMapper(UserMapper.class);
PoJoMapper
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.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.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<!--
1.约束
beans context tx aop mybatis
2.加载jdbc.properties
3.包扫描(非controller组件)
4.数据源(连接池)
5.事务管理器
6.开启注解式事务 可以不要开启注解的aop
7.sqlSessionFactoryBean
-->
<!-- 包扫描(除了controller) -->
<context:component-scan base-package="com.zty">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 加载jdbc.properties -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 数据源, 连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"></bean>
<!-- 开启注解的事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- mybatis 整合部分 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- mybatis核心配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- sql映射文件 -->
<property name="mapperLocations" value="classpath:sqlMapper/*.xml"></property>
</bean>
<!-- 扫描所有mapper接口 -->
<mybatis-spring:scan base-package="com.zty.mapper"/>
</beans>
5.mybatis-config.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>
<!-- 全局的一些设置 -->
<settings>
<!-- 开启数据经典命名到java bean 驼峰命名的映射 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 分步查询懒加载 -->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<!-- 分步查询懒加载 -->
<!-- 开启二级缓存 , 总开关-->
<setting name="cacheEnabled" value="true"/>
</settings>
<!-- 类型别名 -->
<typeAliases>
<!-- 针对某一个java bean 取别名 , 缺点: 类比较多的时候, 配置也会很多-->
<!-- <typeAlias alias="User" type="com.zty.pojo.User"/> -->
<!-- 通过包扫描, 批量设置别名, 扫描pojo基础包, 在这个包下面的所有类, 以类名作为别名, 大小写不敏感-->
<package name="com.zty.pojo"/>
<package name="com.zty.vo"/>
</typeAliases>
</configuration>
6.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="com.zty.mapper.UserMapper">
<resultMap type="User" id="UserMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
</resultMap>
<select id="selectUserById" resultMap="UserMap">
select * from user where id = #{id}
</select>
</mapper>