Mybatis之Spring整合Mapper代理
来源:互联网 发布:1683维也纳战役 知乎 编辑:程序博客网 时间:2024/06/05 11:18
- 配置Mapper代理有两种方式,一种通过MapperFactoryBean创建代理对象,另一种通过MapperScannerConfigurer批量扫描创建代理对象,现在就来比较一下两者的区别。
- 捋一下思路,我就通过一个id查询来测试一下整合的效果,先在数据库建一张user表,另外用java代码建一个po类
public class User { private int id; private String username; private String sex; private Date birthday; private String address;
因为要用到mapper的代理,所以我们还得遵循一些mapper代理的一些规范–指定mapper接口的包名,mybatis将自动扫描包下的所有接口进行加载,前提是mapper接口类和mapper.xml文件在一个目录下且文件名两者需要相同。
public interface UserMapper { public User findUserById(int id);}
<mapper namespace="com.itsky.mapper.UserMapper"> <select id="findUserById" parameterType="int" resultType="com.itsky.ms.po.User"> SELECT * FROM USER WHERE id =#{id} </select> </mapper>
中的namespace属性指定接口的路径。
这样就把java代码及基本的mapper.xml文件搭好了
- 现在搭建一个配置mapper.xml的xml文件,我将其命名为sqlMapConfig.xml
<configuration> <!-- 加载映射文件 --> <mappers> <package name="com.itsky.mapper"/> </mappers></configuration>
- 最后需要一个全局配置文件applicationContext.xml
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 设置代理类的接口 <property name="mapperInterface" value="com.itsky.mapper.UserMapper"></property> 依赖注入SqlSessionFactory <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean>
这里需要配置一个代理对象,用到的是MapperFactoryBean,现在还需要把代理类的接口和SqlSessionFactory传进去。
整合的框架就已经搭建完成,最后来测试一些
private ApplicationContext ctx; @Before public void setUp() throws Exception { ctx = new ClassPathXmlApplicationContext( "applicationContext.xml"); } @Test public void testMapperFindUserById() { // 创建mapper对象 UserMapper userMapper = (UserMapper) ctx.getBean("userMapper"); // 调用mapper对象的方法 User user = userMapper.findUserById(1); System.out.println(user); }
结果
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).log4j:WARN Please initialize the log4j system properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.User [id=1, username=rose, sex=girl, birthday=Sun Mar 22 00:00:00 CST 2015, address=chnia]
现在用另一个方式来配置–通过MapperScannerConfigurer批量扫描创建代理对象
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- // 配置扫描的包 --> <property name="basePackage" value="com.itsky.mapper"></property> </bean>
通过MapperScannerConfigurer配置简单一句话就可以把前面的所有工作给代替,只需要配一个扫描和加载需要扫描的包即可。
测试的结果和上面的一致。(菜鸟学习养成记)
0 0
- Mybatis之Spring整合Mapper代理
- Mybatis-Spring整合Mapper代理开发
- Mybatis和Spring整合mapper代理
- Spring和Mybatis整合(mapper代理开发)
- Spring与Mybatis的整合之Mapper代理的整合方式
- Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合
- (五)mybatis学习之mapper代理开发方式及与spring整合
- mybatis和spring整合开发(mapper代理)
- Mybatis和Spring整合(原始Dao)非mapper代理
- Spring整合MyBatis (使用扫描包配置mapper代理)
- Spring 整合Mybatis mapper问题
- Spring+SpringMVC+Mybatis大整合(SpringMVC采用REST风格、mybatis采用Mapper代理)
- MyBatis之Mapper动态代理
- Mybatis之mapper代理方法
- MyBatis之 mapper代理方式
- Mybatis之Mapper动态代理
- Mybatis之Mapper动态代理
- 基于Maven的Mybatis+spring+springMVC框架整合(mapper代理方式)
- 详解ABBYY FineReader 12内置的自动化任务
- C编程规范的心得分享
- Window环境下 Django 创建数据库
- Json字符串转换Map类型
- 【模拟】洛谷 P1058 立体图
- Mybatis之Spring整合Mapper代理
- ABBYY FineReader操作技巧
- oracle/mysql/sql_server对于主键生成策略的代码展示
- 【高并发简单解决方案】redis队列缓存 + mysql 批量入库 + php离线整合
- 设备管理不停刷新,usb不停刷新
- Java虚拟机--无锁(十五)
- 三种菜单控件的兼容性问题处理集锦
- TPC-DS建表sql
- 每日一句:Effort