天天看點

Mybatis整合Spring之Mapper動态代理開發

步驟

1.建立一個sqlMapConfig.xml檔案

<?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>
		<!-- 2. 指定掃描包,會把包内所有的類都設定别名,别名的名稱就是類名,大小寫不敏感 -->
		<package name="com.itheima.mybatis.pojo" />
	</typeAliases>
	
</configuration>
           

2.建立一個applicationContext.xml檔案

加載配置檔案db.properties  
配置資料庫連結池
配置sqlSessionFactory 工廠
	配置mybatis 的核心檔案
	配置資料源
           
<?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>

	<!-- 配置Mybatis的工廠 SqlSessionFactory -->
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 配置mybatis核心配置檔案 -->
		<property name="configLocation" value="classpath:sqlMapConfig.xml" />
		<!-- 配置資料源 -->
		<property name="dataSource" ref="dataSource" />
	</bean>

</beans>
           

3.建立pojo類(User)

public class User implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	private Integer id;
	private String username;
	private String sex;
	private Date birthday;
	private String address;
	
set\get....
           

4.實作UserMapper接口

public interface UserMapper {
	public User selectUserById(Integer id);
}
           

5.實作UserMapper.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.itheima.mybatis.mapper.UserMapper">
	<select id="selectUserById" parameterType="Integer" resultType="User">
		select * from user where id = #{id}
	</select>
</mapper>
           

6.在sqlMapConfig.xml中加載UserMapper.xml

<mappers>
	<package name="com.itheima.mybatis.mapper"/>
</mappers>
           

7.在applicationContext.xml中配置mapper

  • 法一:
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
	<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
	<property name="mapperInterface" value="com.itheima.mybatis.mapper.UserMapper"/>
</bean>
           
  • 法二:掃描(企業中經常使用)
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<!-- 指定基本包 -->
	<property name="basePackage" value="com.itheima.mybatis.mapper"/>
</bean>	
           

8.測試

public void testMapper() {

	ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		
	//法一
	UserMapper userMapper = (UserMapper) ac.getBean("userMapper");
	User selectUserById = userMapper.selectUserById(10);
	System.out.println(selectUserById);
	
	//法二
	UserMapper mapper = ac.getBean(UserMapper.class);
	User selectUserById2 = mapper.selectUserById(10);
	System.out.println(selectUserById2);
}