Mybatis学习笔记---day04 mybatis与spring整合

一:Mybatis和Spring的整合

1.使用Eclipse创建动态项目


2.导入整合的jar包

3.创建spring的核心配置文件并配置头信息
<?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">
   		
</beans>
4.配置事务、数据源、扫描注解、读取properties文件、mybatis工厂等等
此处因为需要配置的文件信息较多,因此将各种配置文件拆分到不同的文件夹中,在application-context.xml中统一导入文件夹下的所有.xml文件
a、在application-context.xml中导入所有.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">
   		
   		
   		<!-- 导入事务、数据源、properties等所有配置文件 -->
   		<import resource="config/*.xml"/>
   		
</beans>
b、在src下创建config文件夹

c、配置数据库连接jdbc.xml文件
(1)、在config下创建jdbc.xml文件

(2)、找到要配置的数据源的类


(3)、在jdbc.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">
   		
   		
   		<!-- 配置连接池,本次使用c3p0 -->
   		<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
   			<!-- 驱动 -->
   			<property name="driverClass" value="${driverClass}"/>
   			<!-- url -->
   			<property name="jdbcUrl" value="${jdbcUrl}"/>
   			<!-- 用户名 -->
   			<property name="user" value="${user}"/>
   			<!-- 密码 -->
   			<property name="password" value="${pasword}"/>
   		</bean>
   		
</beans>

(4)、配置jdbc.properties文件


driverClass=com.mysql.jdbc.Driver

jdbcUrl=jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8

user=root

password=123

d、在config中配置properties.xml文件来读取*.properties文件


<?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">
   		
   		<!-- 配置读取项目中的.properties文件 -->
   		<!-- 为了使其扩展性好,应该使用集合的方式 -->
   		<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   			<!-- properties文件的集合 -->
   			<property name="locations">
   				<list>
   					<!-- 每一个properties文件的位置 -->
   					<value>classpath:jdbc.properties</value>
   				</list>
   			</property>
   		</bean>
   		
</beans>

e、配置扫描注解的.xml文件--anontation.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">
   		
   		<!-- 配置自动扫描注解 ,在使用SSM框架中,细分化时,
   			spring只负责扫描service层的注解,而Dao是接口,不需要扫描
   			controller层由springmvc负责扫描,因此,此处如果想更加细致一点,
   			应该将 @Controller 注解过滤-->
   			
   		<context:component-scan base-package="com.mybatis">
   			<!-- 选择过滤的注解,即 @Controller -->
   			<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
   		</context:component-scan>
   		
</beans>

f、配置事务的.xml文件--transaction.xml

前提:

找到控制jdbc数据源事务的类

在transaction.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">
   		
   		<!-- 配置数据源的事务,使用的是注解式 -->
   		<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   			<!-- 注入数据源 -->
   			<property name="dataSource" ref="dataSource"/>
   		</bean>
   		
   		<!-- 开启扫描事务的注解 -->
   		<tx:annotation-driven transaction-manager="transactionManager"/>
   		
</beans>

g、配置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>
	<typeAliases>
		<!-- 单个别名定义 -->
		<typeAlias alias="User" type="com.mybatis.pojo.User"/>
		<!-- 配置包下自动别名 -->
		<package name="com.mybatis.pojo"/>
	</typeAliases>
	
	<!-- 指定Mapper的位置 -->
	<mappers>
		<package name="com.mybatis.mapper"/>
	</mappers>
</configuration>
h、配置config中的mybatis.xml配置文件

文件位置:


找到spring中管理mybatis的sqlSessionFactory工厂的类


配置文件内容

<?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">
   		
   		<!-- 配置mybatis的工厂,sqlSessionFactory -->
   		<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
   			<!-- 注入数据源 -->
   			<property name="dataSource" ref="dataSource"/>
   			<!-- 指定mybatis核心配置文件位置 -->
   			<property name="configLocation" value="classpath:mybatis-config.xml"/>
   		</bean>
   		
</beans>

5.测试整合
a、使用原始Dao和Dao实现类进行测试

(1)、编写Dao接口和Dao接口实现类


package com.mybatis.dao;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.mybatis.pojo.User;
/**
 * 原始Dao开发的实现类
 * SqlSessionDaoSupport是用来产生sqlSessionFactory工厂的,其中已经包含
 * @author Administrator
 *
 */
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {

	
	public User selectUserById(Integer id) {
		return this.getSqlSession().selectOne("userDao.selectUserById",id);
	}

}

(2)、在mybaits.xml中初始化UserDaoImpl

<?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">
   		
   		<!-- 配置mybatis的工厂,sqlSessionFactory -->
   		<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
   			<!-- 注入数据源 -->
   			<property name="dataSource" ref="dataSource"/>
   			<!-- 指定mybatis核心配置文件位置 -->
   			<property name="configLocation" value="classpath:mybatis-config.xml"/>
   		</bean>
   		
   		<!-- 1.使用原始Dao开发的配置,即有接口有实现类 -->
   		<!-- 配置dao接口的实现类 -->
   		<bean id="userDao" class="com.mybatis.dao.UserDaoImpl">
   			<!-- 注入工厂,其实是注入到UserDaoImpl继承的SqlSessionDaoSupport父类中 -->
   			<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
   		</bean>
   		
</beans>

(3)、配置mybatis-config.xml文件中的mapper扫描方式

<?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>
	<typeAliases>
		<!-- 单个别名定义 -->
		<typeAlias alias="User" type="com.mybatis.pojo.User"/>
		<!-- 配置包下自动别名 -->
		<package name="com.mybatis.pojo"/>
	</typeAliases>
	
	<!-- 使用原始Dao开发 -->
	<mappers>
		<mapper resource="sqlMap/UserMapper.xml"/>
	</mappers>

(4)、编写Mapper映射文件


<?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="userDao">
	<!-- 根据ID获取用户对象 -->
	<select id="selectUserById" parameterType="Integer" resultType="com.mybatis.pojo.User">
		select * from user where id = #{id}
	</select>

(5)、编写Junit单元测试类

package com.mybatis.junit;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.mybatis.dao.UserDao;
import com.mybatis.pojo.User;

/**
 * sm整合的junit单元测试类
 * @author Administrator
 *
 */
//配置扫描spring核心配置文件
@RunWith(SpringJUnit4ClassRunner.class)
//指定spring核心配置文件位置,否则在junit单元测试时无法读取配置文件
@ContextConfiguration(locations = {"classpath:application-context.xml"})
public class SMJuint {
	
	@Autowired
	private UserDao userDao;
	@Test
	public void test1(){
		User user = userDao.selectUserById(29);
		System.out.println(user);
	}
}
b、使用mapper接口的动态代理进行测试 -- 只需要Dao接口,不需要实现类
(1)、在mybatis.xml中配置动态代理UserDao接口
<?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">
   		
   		<!-- 配置mybatis的工厂,sqlSessionFactory -->
   		<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
   			<!-- 注入数据源 -->
   			<property name="dataSource" ref="dataSource"/>
   			<!-- 指定mybatis核心配置文件位置 -->
   			<property name="configLocation" value="classpath:mybatis-config.xml"/>
   		</bean>
   		
   		<!-- 1.使用Dao接口的动态代理类进行开发 -->
   		<!-- 配置dao接口的动态代理 -->
   		<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
   			<!-- 指定需要代理的接口,即UserDao -->
   			<property name="mapperInterface" value="com.mybatis.dao.UserDao"/>
   			<!-- 注入工厂 -->
   			<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
   		</bean>
   	 	
</beans>
(2)、其他要求
Mapper映射文件的namespace是接口的全类路径
c、动态代理升级版
不需要指定接口,只需要将接口和Mapper映射文件放在同一个包下,通过扫描基础包即可,此时不需要再mybatis的核
心配置文件中配置Mapper映射文件的位置,因为在mybatis.xml文件中已经配置了扫描基础包
(1)、mybatis.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">
   		
   		<!-- 配置mybatis的工厂,sqlSessionFactory -->
   		<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
   			<!-- 注入数据源 -->
   			<property name="dataSource" ref="dataSource"/>
   			<!-- 指定mybatis核心配置文件位置 -->
   			<property name="configLocation" value="classpath:mybatis-config.xml"/>
   		</bean>
   		
   		<!-- 使用Dao接口的动态代理类升级版进行开发,企业中推荐 -->
   		<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
   			<!-- 配置要扫描的mapper映射文件的基本包 -->
   			<property name="basePackage" value="com.mybatis.dao"/>
   		</bean>
   	 	
   	 	
</beans>




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值