MyBatis3——mybatis与spring整合(动态代理方式)

1. mybatis核心配置

<?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>
		<package name="com.bjc.pojo"/>
	</typeAliases> -->
</configuration>

2. spring配置

<?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:jddb.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>
	
	<!-- 加载mybatis核心配置文件 -->
	<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 核心配置文件 -->
		<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
		<!-- 配置别名包扫描  name为typeAliasesPackage  value为pojo的包名-->
		<property name="typeAliasesPackage" value="com.bjc.pojo"></property>
	</bean>

</beans>

3. 配置mapper接口

3.1 方式一:单个接口配置MapperFactoryBean

         在mybatis与spring的整合包mybatis-spring-1.2.2.jar中有一个org.mybatis.spring.mapper.MapperFactoryBean<T>,我们只需要将它交给spring管理就可以了。

         怎么配置了,我们打开该类,有一个例子

<pre class="code">
 * {@code
 *   <bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true" lazy-init="true">
 *     <property name="sqlSessionFactory" ref="sqlSessionFactory" />
 *   </bean>
 *
 *   <bean id="oneMapper" parent="baseMapper">
 *     <property name="mapperInterface" value="my.package.MyMapperInterface" />
 *   </bean>
 *
 *   <bean id="anotherMapper" parent="baseMapper">
 *     <property name="mapperInterface" value="my.package.MyAnotherMapperInterface" />
 *   </bean>
 * }
 * </pre>

我们直接将其复制到spring的配置文件中,进行改造即可,改造之后如下

<!-- 配置MapperFactoryBean -->
<bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true" lazy-init="true">
   <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
 
<bean parent="baseMapper">
   <property name="mapperInterface" value="com.bjc.mapper.UserMapper" />
</bean>

注意:

1. 每一个mapper的配置需要有一个parent属性,其属性值为baseMapper

2. property的name属性值是mapperInterface是,因为在MapperFactoryBean中有一个叫做mapperInterface的属性,其有一各set方法。

3. value的属性值即为对应的mapper接口的全称。

完整的配置如下:

<?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:jddb.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>
	
	<!-- 加载mybatis核心配置文件 -->
	<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 核心配置文件 -->
		<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
		<!-- 配置别名包扫描  name为typeAliasesPackage  value为pojo的包名-->
		<property name="typeAliasesPackage" value="com.bjc.pojo"></property>
	</bean>
	
	<!-- 配置MapperFactoryBean -->
	<bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true" lazy-init="true">
      <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
 
    <bean parent="baseMapper">
      <property name="mapperInterface" value="com.bjc.mapper.UserMapper" />
    </bean>

</beans>

接口映射文件:

<?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.bjc.mapper.UserMapper">
	<select id="getUser" resultType="user">
		SELECT 	
			t.id, 
			t.username, 
			t.birthday, 
			t.sex, 
			t.address, 
			t.uuid2
		FROM `user` t
	</select>
</mapper>

测试类:

package com.bjc.test;

import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bjc.mapper.UserMapper;
import com.bjc.pojo.User;

public class TestDemo {
	private ApplicationContext  applicationContext;
	
	@Before
	public void init(){
		applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
	}
	
	@Test
	public void test1(){
		UserMapper bean = applicationContext.getBean(UserMapper.class);
		List<User> user = bean.getUser();
		System.out.println(user.toString());
	}
	
}

3.2 方式二:配置包扫描器

在整合包中还有一个类叫做MapperScannerConfigurer,该类就是用于配置包扫描的。

配置方式:只需要在spring的配置文件中配置包扫描即可,其他的不便

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 	<property name="basePackage" value="com.bjc.mapper" />
 </bean>

注意:value为mapper的包名,不要写成了接口名。

完整的配置如下:

<?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:jdbc.properties" />

	<!-- 数据库连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClass}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.user}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="10" />
		<property name="maxIdle" value="5" />
	</bean>
	
	<!-- 加载mybatis核心配置文件 -->
	<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 核心配置文件 -->
		<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
		<!-- 配置别名包扫描  name为typeAliasesPackage  value为pojo的包名-->
		<property name="typeAliasesPackage" value="com.bjc.pojo"></property>
	</bean>

 
 	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 		<property name="basePackage" value="com.bjc.mapper" />
 	</bean>
 
</beans>
发布了128 篇原创文章 · 获赞 6 · 访问量 3269

猜你喜欢

转载自blog.csdn.net/weixin_43318134/article/details/102771664