一 : jar包简单列举 :
1, mybatis官方提供与mybatis与spring整合jar包
2, spring相关jar包 3,mybatis相关包 4,c3p0连接池 5, mysql数据库驱动
二 : mybatis配置文件
在classpath下创建mybatis-config.xml
在与spring整合前, mybatis配置文件中配置 数据库连接池 , 事物管理器, 映射文件 等 . 在与spring整合后其中数据库连接池, 事物管理器交给spring进行管理
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper .//EN" "http://www.mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="userMapper">
<select id="findUserById" parameterType="int" resultType="cn.sang.model.User">
SELECT * FROM USER WHERE id = #{id}
</select>
</mapper>
三 : spring配置文件
在classpath下创建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:mvc="http://www.springframework.org/schema/mvc"
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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!-- 注解式扫描基本包实例化类 spring扫描器只扫描Service层,Controller层由springMvc负责扫描,Dao层有Mybatis复制扫描 -->
<context:component-scan base-package="cn.sang.service.imp"/>
<!-- 读取properties配置文件 扩展性不好的方式 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<!-- 驱动 -->
<property name="driverClass" value="${driverClass}"/>
<!-- url -->
<property name="jdbcUrl" value="${jdbcUrl}"/>
<!-- 用户名 -->
<property name="user" value="${user}"/>
<!-- 密码 -->
<property name="password" value="${password}"/>
</bean>
<!-- 让spring管理sqlSessionfactory 使用myBatis和spring整合包中的 ,在定义sqlSessionFactory时指定数据源dataSource和myBatis的配置文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载myBatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<!-- 配置数据源事物 注解式开发 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- Dao实现类开发 -->
<!-- 为什么要在此处配置dao的实例化,因为spring扫描包实例化并没有扫描dao层,需要mybatis进行扫描,所以此处需要进行实例化的配置 -->
<!-- 此处配置后,dao实现类被实例化,可以在srevice能扫描到地方使用注解autowraid的方式注入使用-->
<bean id="userDao" class="cn.sang.dao.imp.UserDaoImp">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
需要配置内容如下 :
1, 事务管理
1.1 此处使用注解式事物管理, 分为两步 , (1配置数据源事物 2 开启注解驱动)
<!--配置数据源事物 注解式开发 -->
<beanclass="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="transactionManager"ref="dataSource"/>
</bean>
<!--开启注解驱动 -->
<tx:annotation-driven/>
2, 读取jdbc.properties文件 , 两种方式
2.1扩展性不好的方式
<!--加载properties配置文件 -->
<context:property-placeholderlocation="classpath:jdbc.properties"/>
2.2 扩展性好的方式
<!-- 读取properties文件 -->
<beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<propertyname="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
3, 数据库连接池
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<!-- 驱动 -->
<property name="driverClass" value="${driverClass}"/>
<!-- url -->
<property name="jdbcUrl" value="${jdbcUrl}"/>
<!-- 用户名 -->
<property name="user" value="${user}"/>
<!-- 密码 -->
<property name="password" value="${password}"/>
</bean>
作用 : 在未使用spring管理sqlSessionFactory之前,每次使用sqlSessionFactory都要在代码中读取mybatis配置文件使用sqlSessionFactoryBuilder类创建sqlSessionFactory,获取sqlSession . 使用spring配置文件管理sqlSessionFactory的创建后就可以省去重复创建sqlSessionFactory的代码
<!--让spring管理sqlSessionfactory使用myBatis和spring整合包中的 ,在定义sqlSessionFactory时指定数据源dataSource和myBatis的配置文件 -->
<bean id="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<propertyname="dataSource"ref="dataSource"/>
<!--加载myBatis的全局配置文件 -->
<propertyname="configLocation"value="classpath:mybatis-config.xml"/>
</bean>
5, 使用注解式实例化Service
注意 : spring的扫描器只扫描Service层 , Dao层由Mybatis负责扫描 , 如果Dao层是接口则不需要扫描 .Controller层由SpringMvc负责扫描 , 所以在spring配置文件的扫描器要排除扫描Controller层
<!-- 注解式扫描基本包实例化类 spring扫描器只扫描Service层,Controller层由springMvc负责扫描,Dao层有Mybatis复制扫描 -->
<context:component-scan base-package="cn.sang.service.imp"/>
6, 使用dao实现类开发
注解式开发中, spring的扫描器只扫描service层, springmvc扫描器扫描controller层 , mybatis扫描dao层 , 如果dao实现类没有被扫描 , 可以在spring配置文件中通过配置bean的方式, 进行实例化 , 然后在service层可以使用autowraid注解进行注入使用
<!-- Dao实现类开发 -->
<!-- 为什么要在此处配置dao的实例化,因为spring扫描包实例化并没有扫描dao层,需要mybatis进行扫描,所以此处需要进行实例化的配置 -->
<!-- 此处配置后,dao实现类被实例化,可以在srevice能扫描到地方使用注解autowraid的方式注入使用-->
<bean id="userDao" class="cn.sang.dao.imp.UserDaoImp">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>