MyBatis与Spring之间的整合

本文介绍了如何在Spring框架中整合MyBatis,包括配置SqlSessionFactoryBean和数据映射器类,通过示例展示了如何在Spring应用上下文中定义SqlSessionFactory,并提供了一段测试代码验证整合效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring-MyBatis

Spring 3.0仅支持iBatis2。那么, 我们就想将MyBatis3的支持添加到Spring3.0
(参考 Spring 的 Jira的问题)中。不幸的是,Spring 3.0 的开发在 MyBatis 3.0 官方发布前就
结束了。因为Spring开发团队不想发布一个基于非发行版的MyBatis的整合支持,那么 Spring
官方的支持就不得不继续等待了。要在 Spring 中支持 MyBatis,MyBatis社区认为现在应该
是自己团结贡献者和有兴趣的人一起来开始将Spring的整合作为MyBatis社区的子项目的时
候了。

要求:

 要使用 MyBatis-Spring 模块,需要包含 mybatis-spring-1.x.jar文件,并在类路径中加入依赖关系。
要和 Spring 一起使用 MyBatis,你需要在 Spring 应用上下文中定义至少两样东西:一个SqlSessionFactory和至少一个数据映射器类。
在 MyBatis-Spring 中,SqlSessionFactoryBean是用于创建 SqlSessionFactory的。要配置这个工厂 bean,放置下面的代码在 Spring 的 XML 配置文件中:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>  

 

SqlSessionFactoryBean

在基本的 MyBatis中,session 工厂可以使用 SqlSessionFactoryBuilder.来创建。在myBatis-Spring 中,使用了SqlSessionFactoryBean来替代。
SqlSessionFactory有一个单独的必须属性,就是 JDBC 的 DataSource。
一个通用的属性是 configLocation,它是用来指定 MyBatis的 XML 配置文件路径的。
mapperLocations属性使用一个资源位置的list。这个属性可以用来指定MyBatis的XML
映射器文件的位置。它的值可以包含 Ant 样式来加载一个目录中所有文件,或者从基路径下递归搜索所有路径。
示例:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
<property name="dataSource" ref="dataSource" /> 
<property  name="mapperLocations"  value="classpath*:sample/config/mappers/**/*.xml"  />
</bean> 
这会从类路径下加载在 sample.config.mappers 包和它的子包中所有的 MyBatis 映射器XML 文件。 


canMybatisSpring

 

用的包:

 

类:

 

TestMyBatisSpring.java

package cn.hncu.test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.hncu.domain.User;

public class TestMyBatisSpring {
	
	//测试在Spring框架下数据库是否能够成功连接
	@Test
	public void test1() throws SQLException{
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		DataSource ds=ctx.getBean("dataSource",DataSource.class);
		Connection con=ds.getConnection();
		System.out.println("con: "+con);
	}
	
	//测试利用Spring框架下sqlSessionFactory-----bean进行数据库查询是否成功
	@Test
	public void test2() throws SQLException{
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		SqlSessionFactory factory=ctx.getBean("sqlSessionFactory",SqlSessionFactory.class);
		SqlSession ss=factory.openSession();
		List<User> users=ss.selectList("users.all");
		System.out.println(users);
	}
}


加载的配置文件: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:context="http://www.springframework.org/schema/context"
		xmlns:aop="http://www.springframework.org/schema/aop"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
				http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/mybatis?characterEncoding=UTF-8"></property>
		<property name="user" value="hncu"></property>
		<property name="password" value="1234"></property>
	</bean>
	<bean id="txM" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 配置事务 -->
	<tx:advice id="txAdvice" transaction-manager="txM">
		<tx:attributes>
			<tx:method name="*" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut expression="execution(* cn..*Service.*(..))" id="cut"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="cut"/>
	</aop:config>
	
	
	<!-- 以下配置把mybatis 架到spring上 -->
	<!-- 用spring创建一个sqlSessionFactory的bean -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<!-- mybatis配置文件的指定方式1: 先加载mybatis - config.xml配置文件,然后由该配置文件中<mappers>元素配置加载哪些映射文件 
		<property name="configLocation">
			<value>classpath:mybatis-config.xml</value>
		</property>	
		-->
		<!-- mybatis配置文件的指定方式2: 丢弃mybatis - config.xml配置文件,然后直接注入dataSource且由下面直接指定Mapper文件列表 -->
		<property name="mapperLocations">
			<list>
				<value>
					classpath:cn/hncu/domain/*.xml
				</value>
			</list>
		</property>	
	</bean>
	
	
</beans>


值对象:User.java

package cn.hncu.domain;
public class User {
	private String id;
	private String name;
	private String pwd;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + "]";
	}
	
}


MyBatis所需值对象的映射文件:User.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="users">
	
	<select id="all" resultType="cn.hncu.domain.User">
		select * from users
	</select>
</mapper>


经测试:MyBatis与Spring整合成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值