这里介绍一下SSM整合进流程,下面截图是我整合完毕的一个项目结构图。
第一步mybatis配置:
a.在resource文件夹下建立mybatis的配置文件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>
<!-- 使用jdbc的getGeneratedKeys 获取数据库自增主键值 -->
<setting name="useGeneratedKeys" value="true"/>
<!-- 使用列别名替换列名 默认:true
select name as title from table//把数据库中的列名转化为类中的属性
-->
<setting name="useColumnLabel" value="true"/>
<!-- 开启驼峰命名转换:Table(create_time)->Entity(createTime) -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
b.在resource文件下建立mapper文件夹用来放实现dao接口的xml文件。
c.在src/main/java文件夹下建立yjx.maven.dao包用来存放对数据库进行操作的接口类。
d.在resource文件夹下建立jdbc.properties。
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/seckill?useUnicode=true&characterEncoding=utf8
username=root
password=root
第二步配置Spring及整合mybatis:
a.在resource文件夹下建立spring文件夹并在改目录下建立spring-dao.xml
注:配置文件中详细介绍了整合Mybatis过程,主要是:引入数据库文件,dataSource,sqlSessionFactory,配置dao接口包
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 配置整合mybatis过程 -->
<!-- 1:配置数据库相关参数properties的属性:${url} -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 2:数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 配置连接池属性 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="${url}"/>
<property name="user" value="${username}"/>
<property name="password" value="${password}"/>
<!-- c3p0连接池的属性 -->
<!-- 最多保持30个数据库的连接对象,默认15 -->
<property name="maxPoolSize" value="30"/>
<!-- 最少保持15个数据库的连接对象,默认3 -->
<property name="minPoolSize" value="30"/>
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false"/>
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="1000"/>
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!-- 3:配置SqlSessionFactory/Mybatis最重要的api -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置Mybatis全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 扫描entity包 识别为别名 -->
<property name="typeAliasesPackage" value="yjx.maven.entity"/>
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 4:配置扫描Dao接口包,动态实现dao接口并注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlsessionfactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/><!-- 这个参数有别的方式,这个方式可以防止jdbc.properties还没有加载注入datasource就使用sqlsessionfactory -->
<!-- 给出扫描DAO接口包 -->
<property name="basePackage" value="yjx.maven.dao"></property>
</bean>
</beans>
b.在resource/spring文件夹下建立spring-service.xml文件
注:主要是事务管理器的配置,service扫描的配置
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 扫描service包下包括子包所有使用注解的类型 -->
<context:component-scan base-package="yjx.maven.service"/>
<!-- 配置事物管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置基于注解的声明式事物
默认使用注解管理事物行为
-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
3:springmvc的配置及整合spring
a.在resource/spring文件夹下建立spring-web.xml
注:主要是开启SpringMVC注解、静态资源Servlet配置、视图解析器的配置、web相关的bean扫描配置
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 配置SpringMVC -->
<!-- 1:开启SpringMVC注解 -->
<!-- 简化配置: (1):自动注册DefaultAnnotationHandlerMapping(url到handler映射采用注解映射,AnnotationMethodHandlerAdapter(注解方法适配器)
(2):默认提供一系列功能:数据绑定、数据和日期的format @NumberFormat,@DataTimeFormat,xml,json默认读写支持 -->
<mvc:annotation-driven />
<!-- 2"静态资源默认Servlet配置 1:加入对静态资源的处理:js,gif,png 2:允许使用"/"做整体映射 -->
<mvc:default-servlet-handler />
<!-- 3:配置为jsp 显示ViewResolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <!-- 提供jstl等表达式支持 -->
<property name="prefix" value="/WEB-INF/jsp" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 4:扫描web相关的bean -->
<context:component-scan base-package="yjx.maven.web" />
</beans>
b.在web.xml中配置SpringMVC
注:主要是SpringMVC中央控制器的配置,并在其中配置SpringMVC和spring的配置文件路径
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jsp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
metadata-complete="true">
<!-- 配置SpringMVC中央控制器DispatcherServlet -->
<servlet>
<servlet-name>seckill-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置SpringMVC需要加载的配置文件
spring-dao.xml,spring-service.xml,spring-web.xml
整合顺序:mybatis整合到spring中,spring整合到SpringMVC中
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>seckill-dispatcher</servlet-name>
<!-- 匹配所有的请求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>