实现步骤
- Spring中配置dataSource以及SessionFactory
- Spring中配置dao的代理类的bean
- 测试
- 可以看出和映射有很多代码是可以复用的
- 同时我们需要注意,代理类我们需要同名同包
代码结构
Spring配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="password" value="${password}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="maxActive" value="10"></property>
<property name="maxIdle" value="5"></property>
</bean>
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="mapper.UserMapper"></property>
<property name="sqlSessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
- 这里的代码和映射代码相差不多,只是多了个代理类的配置
-
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
这胡娟就是配置的我们的代理类了
-
<property name="mapperInterface" value="mapper.UserMapper"></property>
这里是映射的我们的接口类所在的位置
-
<property name="sqlSessionFactory" value="sessionFactory"></property>
这里是映射的我们的sqlsessionfactory
Mapper
<?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="mapper.UserMapper">
<select id="findById" parameterType="int" resultType="domain.User">
select * from tb_user where id=#{id}
</select>
</mapper>
public interface UserMapper {
public User findById(int id);
}
测试类
public class Text {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
UserMapper userMapper=(UserMapper) ac.getBean("userMapper");
User user=userMapper.findById(1);
System.out.println(user);
}
}
总结
- 这个代码整体来说就比刚刚的简单的多了
- 更多的是靠接口和配置的代理bean完成
- 那么如果我们的mapper有点多,怎么办呢?
基于扫描包的配置
<!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="mapper.UserMapper"></property>
<property name="sqlSessionFactory" ref="sessionFactory"></property>
</bean> -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
</bean>
- 这里还是很简单的,就是吧前面的注释点,然后加上
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- 测试类不用更改