mybatis-spring整合过程

本文详细介绍如何在Spring环境中配置并使用MyBatis框架,包括数据源、SqlSessionFactory、Mapper扫描配置及事务管理等关键步骤,并提供具体示例。

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

  • 包含 mybatis-spring-x.x.x.jar 文 件
  • Spring XML配置文件
    配置dataSource
<!-- 1. 数据源 : DriverManagerDataSource -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
		<property name="username" value="root" />
		<property name="password" value="123" />
</bean>

配置sqlSessionFactory

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
</bean>

配置MapperFactoryBean

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
  <property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" />
  <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

上述可以升级成配置MapperScannerConfigurer

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn.neusoft.mapper"></property>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	<!--  	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>-->
</bean>

配置事务管理器

<bean id="txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
</bean>

使用声明式事务的注解方式,之后Service层使用@Transactional注解方式

<tx:annotation-driven transaction-manager="txManager" />

注意: 只有mybatis时,要在mybatis的配置文件中<mapper>指定mapper.xml。 Spring-Mybatis时,如果mapper.java和mapper.xml不在同一包下,要么在mybatis的配置文件中<mapper>指定,要么在<bean id="sqlSessionFactory">的mapperLocations属性中配置; 如果mapper.java和mapper.xml在同一包下,它会被MapperFactoryBean(如果升级成MapperScannerConfigurer一样的)自动解析。
个人使用的一份spring-common.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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context" 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.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

	<!-- 1. 数据源 : DriverManagerDataSource -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
		<property name="username" value="root" />
		<property name="password" value="123" />
	</bean>

	<!--
		2. mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:引用数据源

		MyBatis定义数据源,同意加载配置
	-->
	
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:config/mybatis-config.xml" /> 
	</bean>
 	
 	
	<!--
		3. mybatis自动扫描加载Sql映射文件/接口 : MapperScannerConfigurer sqlSessionFactory

		basePackage:指定sql映射文件/接口所在的包(自动扫描)
	-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn.neusoft.mapper"></property>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	<!--  	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>-->
	</bean>

	<!--
		4. 事务管理 : DataSourceTransactionManager dataSource:引用上面定义的数据源
	-->
	<bean id="txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

	<!-- 5. 使用声明式事务
		 transaction-manager:引用上面定义的事务管理器
	 -->
	<tx:annotation-driven transaction-manager="txManager" />

</beans>

  • Mybatis的配置文件相应的配置部分被Spring代替了,就不用再写了,只剩下一些简单的
<?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.neusoft.model.User" />-->
		<!--  <package name="com.neusoft.model"/>-->
		<typeAlias alias="Userinfo" type="cn.neusoft.pojo.Userinfo" />
	</typeAliases>
	<!-- 实体接口映射资源 -->
	<!--
		说明:如果xxMapper.xml配置文件放在和xxMapper.java统一目录下,mappers也可以省略,因为org.mybatis.spring.mapper.MapperFactoryBean默认会去查找与xxMapper.java相同目录和名称的xxMapper.xml(Spring 有的)
	-->
	<!--    
	 <mappers>
		<mapper resource="com/neusoft/mapper/userMapper.xml" />
		<mapper resource="com/neusoft/mapper/TUserMapper.xml" />
		<mapper resource="com/neusoft/mapper/TblAddressMapper.xml" />
		<mapper resource="com/neusoft/mapper/TblUserinfoMapper.xml" />
	</mappers>
	-->
</configuration>  
  • 然后是在Service层
    (1)当使用MapperFactoryBean时:
    Spring XML配置文件中
<bean id="fooService" class="org.mybatis.spring.sample.mapper.FooServiceImpl">
  <property name="userMapper" ref="userMapper" />
</bean>

Service层使用mapper代码(使用Sevice bean的属性mapper 或者@Autowired)

public class FooServiceImpl implements FooService {

  private UserMapper userMapper;

  public void setUserMapper(UserMapper userMapper) {
    this.userMapper = userMapper;
  }

  public User doSomeBusinessStuff(String userId) {
    return this.userMapper.getUser(userId);
  }
}

(2)当使用MapperScannerConfigurer时:应该Service层@Autowired注解使用mapper.

@Service("userService")
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;
 
	@Override
	public User getUser(User user) {
		return userMapper.getUser(user);
	}
}
  • 当执行的操作要提交事务时(不是每种操作都要提交事务的,但是写肯定没错):
@Service
@Transactional    				  
public class UserinfoServiceImpl implements UserinfoService {

	@Resource		
	private UserinfoMapper userinfoMapper;//把Mapper资源引入
	
	public List<Userinfo> findAllUsers() {
		// 查询所有用户信息
		List<Userinfo> users = userinfoMapper.listAll();
		return users;
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值