复制上一节中的项目MyBatis_Spring_DAO,命名为MyBatis_Spring_Mapper。删除dao包及包内所有文件、com.test/test/UserDaoTest.java,新建动态代理接口类mapper/UserMapper.java、测试类com.test/test/MapperTest.java,整理项目目录如下所示:
![](https://img.haomeiwen.com/i12413187/a82a6e64c37f7e06.png)
修改Spring主配置文件applicationContext.xml。
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd ">
<!-- 读取db.properties -->
<context:property-placeholder location="db.properties"/>
<!-- 配置c3p0连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置mybatis sqlSessionFactory -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 告诉spring mybatis的核心配置文件 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
</bean>
<!-- mapper动态代理开发 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 注入 sqlSessionFactory -->
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
<!-- 配置接口 -->
<property name="mapperInterface" value="com.test.mapper.UserMapper"/>
</bean>
</beans>
给出其他新建文件的代码。
UserMapper.java:
package com.test.mapper;
import com.test.bean.User;
public interface UserMapper {
//通过id查询一个用户
public User selectUserById(Integer id);
}
MapperTest.java:
package com.test.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.bean.User;
import com.test.mapper.UserMapper;
public class MapperTest {
@Test
public void Test1() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper mapper = (UserMapper) ac.getBean("userMapper");
User user = mapper.selectUserById(1);
System.out.println(user);
}
}
注意修改UserMapper.xml和sqlMapConfig.xml的小细节。
UserMapper.xml中,
<mapper namespace="UserMapper">
改为
<mapper namespace="com.test.mapper.UserMapper">
。
sqlMapConfig.xml中,
<mappers>
<mapper resource="com/test/mapper/UserMapper.xml"/>
</mappers>
改为
<mappers>
<package name="com.test.mapper"/>
</mappers>
。
进行测试。
![](https://img.haomeiwen.com/i12413187/36e4bdcd26c207f3.png)
网友评论