mybatis框架——spring整合

1、整合思路

spring整合一个框架的两个重要思想就是IOC和AOP,mybatis的执行流程是通过配置文件sqlMapConfig.xml生成sqlSessionFactory,再由sqlSessionFactory生成sqlSession,并且在Mapper代理方式中,在整合前是通过gerMapper(Class<T> clazz)方法获取Mapper接口的动态代理对象,但是与spring整合后我们需要从spring中获取这个动态代理对象。基于以上的思路我们可以归纳成以下四点:

  • SqlSessionFactory对象应该放到spring容器中作为单例存在。
  • 传统dao的开发方式中,应该从spring容器中获得sqlsession对象。
  • Mapper代理形式中,应该从spring容器中直接获得mapper的代理对象。
  • 数据库的连接以及数据库连接池事务管理都交给spring容器来完成。

2、整合步骤

2.1 SqlMapperConfig.xml配置

由于事务管理和数据库连接都会交给spring管理,SqlSessionFactory的创建也会交给spring,所以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="product" type="com.liu.po.Product" />
		<typeAlias alias="user" type="com.liu.po.User" />
	</typeAliases>
</configuration>

这里只配置了别名

2.2 applicationContext.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	<!-- 加载配置文件 -->
	<context:property-placeholder location="classpath:db.properties" />
	<!-- 数据库连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<!-- 最大连接数据库连接数 -->
		<property name="maxActive" value="10" />
		<!-- 最大等待连接中的数量 -->
		<property name="maxIdle" value="5" />
	</bean>
	<!-- sqlSessonFactory的配置 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 配置数据库连接池 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 加载配置文件 -->
		<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
	</bean>
	
	<!-- mapper代理形式dao的配置 -->
	<!-- 配置包扫描器 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 配置要扫描的包 ,如果扫描多个包使用半角逗号分隔-->
        <!-- 扫描Mapper接口所在的包-->
		<property name="basePackage" value="com.liu.mapper"/>
	</bean>
</beans>

不过这里需要注意的是映射文件必须和Mapper接口在统一路径下,并且名称必须相同。

2.3 映射文件的配置

<?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.liu.mapper.ProductMapper">
	<resultMap type="product" id="productListMap">
		<id column="id" property="id"/>
		<result column="p_name" property="name"/>
		<result column="price" property="price"/>
		<association property="user" javaType="user">
			<id column="id" property="id"/>
			<result column="name" property="name"/>
			<result column="sex" property="sex"/>
			<result column="address" property="address"/>
		</association>
	</resultMap>
    //对应Mapper接口中的getProductByname方法
	<select id="getProductByname" parameterType="product" resultType="product" >
		select * from product where p_name like '%${name}%'
	</select>
    //对应Mapper接口中的findProductWithProductMa方法
	<select id="findProductWithProductMap" resultMap="productListMap">
		select p.id,p.p_name,p.price,u.name,u.address 
		from product p,user u
		where p.id=u.id 
	</select>
</mapper>

2.4 测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations=("classpath:spring/applicationContext.xml"))
public class ProductMapperTest {

    //注入Mapper接口的代理对象
	@Autowired
	private ProductMapper productMapper;
	
	@Test
	public void test1() {
        //用代理对象执行接口中的方法
		List<Product> products = productMapper.findProductWithProductMap();
		for (Product product : products) {
			System.out.println(product);	
		}
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值