Spring和Mybatis整合的两种实现方式

Spring和Mybatis整合

使用配置方式开发

整合思路
  1. 需要spring通过单例方式管理sqlSessionFactory
  2. spring和mybatis整合生成mapper接口代理对象使用sqlSessionFactory创建SqlSession
  3. 持久层的mapper需要spring进行管理
准备jar包
  1. 核心包 :mybatis-spring.jar
  2. spring jar包
  3. mybatis jar包
  4. c3p0数据库连接池
  5. 数据库驱动
编写核心配置文件
  1. spring的配置文件:applicationContext.xml
  2. mybatis的配置文件:SqlMapConfig.xml
  3. 日志文件:log4j.properties
  4. db.properties
代码实例

Mapper接口

public interface UserMapper {
	// 根据id查询用户信息
	User selectUserById(Integer id);
}

Mapper.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="com.hpe.mapper.UserMapper">
	<!-- 根据id查询用户信息 -->
	<select id="selectUserById" parameterType="int" resultType="User">
		SELECT * FROM user WHERE id = #{id}
	</select>
</mapper>

编写UserService接口和其实现类

public interface UserService {
	
	// 根据id查询用户信息
	User selectUserById(Integer id);
}
public class UserServiceImpl implements UserService{

	// 注入UserMapper的动态代理对象
	private UserMapper userMapper;

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

	@Override
	public User selectUserById(Integer id) {
		return userMapper.selectUserById(id);
	}

}

配置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" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
	<!-- 加载外部资源文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<!-- 配置c3p0数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver}"></property>
		<property name="jdbcUrl" value="${jdbc.url}"></property>
		<property name="user" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	
	<!-- 通过Spring管理SqlSessionFactory mapper接口 -->
	<!-- 配置SqlSessionFactory为了使用 Spring来管理SqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 配置mybatis核心配置文件的路径 -->
		<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
		<!-- 指定数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 指定批量创建别名的包 -->
		<property name="typeAliasesPackage" value="com.hpe.po"></property>
	</bean>
	
	<!-- 配置UserService -->
	<bean id="userService" class="com.hpe.service.UserServiceImpl">
		<!-- 注入UserMapper属性 -->
		<property name="userMapper" ref="userMapper"></property>
	</bean>
	
	<!-- 配置UserMapper接口实现类的Bean(Mapper代理类) -->
	<!-- 通过MapperFactoryBean生成Mapper代理对象
		 mapperInterface:指定接口
		 sqlSessionFactory:指定会话工厂
	 -->
	<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<!-- 指定创建mapper接口对应的实现类 -->
		<property name="mapperInterface" value="com.hpe.mapper.UserMapper"></property>
		<!-- 注入SqlSessionFactory:为了创建sqlSession -->
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>
	
</beans>

此时Mybatis的核心配置文件只有声明,没有编写任何内容。

使用注解方式开发

代码实例

此时Mapper接口、XML文件、Service接口及测试Test方法均未发生变化(相比较于配置方式),此处不再贴重复代码
ServiceImpl.java

@Service(value="userService")
public class UserServiceImpl implements UserService{

	// 注入UserMapper的动态代理对象
	//@Resource(name="userMapper")
	// 自动装配:因为加载容器的时候就创建了UserMapper实现类的Bean,这里可以自动装配
	@Autowired
	private UserMapper userMapper;

	@Override
	public User selectUserById(Integer id) {
		return userMapper.selectUserById(id);
	}
}

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" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
	<!-- 开启注解扫描 -->
	<context:component-scan base-package="com.hpe"></context:component-scan>
	
	<!-- 加载外部资源文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<!-- 配置c3p0数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver}"></property>
		<property name="jdbcUrl" value="${jdbc.url}"></property>
		<property name="user" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	
	<!-- 通过Spring管理SqlSessionFactory mapper接口 -->
	<!-- 配置SqlSessionFactory为了使用 Spring来管理SqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 配置mybatis核心配置文件的路径 -->
		<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
		<!-- 指定数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 指定批量创建别名的包 -->
		<property name="typeAliasesPackage" value="com.hpe.po"></property>
	</bean>
	
	<!-- 批量创建Mapper接口实现类的Bean,可以不指定id
		 默认Bean是由id的,是Mapper接口的名称且首字母小写-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 指定需要创建实现类的Mapper接口所在的包
			 可以指定多个包,使用逗号隔开即可-->
		<property name="basePackage" value="com.hpe.mapper"></property>
		<!-- 需要指定SqlSessionFactory,使用sqlSessionFactoryBeanName
			 通过这种方式就可以等到Spring初始化完成后,再去构造SqlSessionFactory,否则无法连接数据库 -->
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>
	
</beans>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值