【Mybatis从0到1-016】Spring与MyBatis整合mapper开发DAO(推荐使用)
来源:互联网 发布:ubuntu使用管理员账户 编辑:程序博客网 时间:2024/06/05 15:08
在之前的文章中已经提及开发dao的方式,当初是单纯的mybatis开发,分为原始开发方式和基于mapper代理的方式,与Spring整合之后的开发同样是这两种方式,原始的开发方式需要程序员书写dao接口和实现类,而且会存在很多问题,比如【005】提到的。
本章主要介绍使用mapper代理进行dao的开发。
【1】早在mapper文件下新建UserMapper.java接口文件。关键代码:
public interface UserMapper { //根据id查询用户信息 User findUserById(int id) throws Exception;}
【2】修改配置文件resources\applicationContext.xml,加入如下代码:
<!--原始dao接口--><!--<bean id="userDao" class="dao.UserDaoImpl">--> <!--<property name="sqlSessionFactory" ref="sqlSessionFactory"/>--><!--</bean>--><!--配置mapper--><!--<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">--> <!--<property name="mapperInterface" value="mapper.UserMapper"/>--> <!--<property name="sqlSessionFactory" ref="sqlSessionFactory"/>--><!--</bean>--><!--通过mapperScannerConfigure进行mapper扫描--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/></bean>
【3】修改映射文件resources\sqlMap\User.xml
<mapper namespace="test"> <!-- 在 映射文件中配置很多sql语句 --> <!-- 需求:通过id查询用户表的记录 --> <!-- 通过 select执行数据库查询 id:标识 映射文件中的 sql 将sql语句封装到mappedStatement对象中,所以将id称为statement的id parameterType:指定输入 参数的类型,这里指定int型 #{}表示一个占位符号 #{id}:其中的id表示 接收输入 的参数,参数名称就是id,如果输入参数是简单类型,#{}中的参数名可以任意,可以value或其它名称 resultType:指定sql输出结果 的所映射的java对象类型,select指定resultType表示将单条记录映射成的java对象。--> <select id="findUserById" parameterType="int" resultType="po.User"> SELECT *FROM USER WHERE id=#{value} </select></mapper>
【4】测试程序
public class UserMapperTest { private ApplicationContext applicationContext; //在setUp这个方法得到spring容器 @Before public void setUp() throws Exception { applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); } @Test public void testFindUserById() throws Exception { UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper"); User user = userMapper.findUserById(1); System.out.println(user); }}
【5】结果如下:
阅读全文
1 0
- 【Mybatis从0到1-016】Spring与MyBatis整合mapper开发DAO(推荐使用)
- 【Mybatis从0到1-015】Spring与MyBatis整合原始开发DAO(不推荐使用)
- Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合
- mybatis整合spring(mapper+原始dao)
- 【Mybatis从0到1-006】mybatis开发DAO之基于mapper代理
- Mybatis和Spring整合(原始Dao)非mapper代理
- mybatis与spring3.1整合(org.mybatis.spring.mapper.MapperFactoryBean)
- mybatis与spring3.1整合(org.mybatis.spring.mapper.MapperFactoryBean)
- spring+springmvc+mybatis(2)--mapper代替dao开发
- spring+springmvc+mybatis(1)--整合和原始dao开发
- Mybatis-Spring整合Mapper代理开发
- Spring和Mybatis整合(mapper代理开发)
- Spring和Mybatis整合-mapper开发
- mybatis和spring整合开发(mapper代理)
- mybatis-spring Dao整合
- Spring整合MyBatis (使用扫描包配置mapper代理)
- Spring和Mybatis整合-原生dao开发
- MyBatis 实践 -Mapper与DAO
- 简明 VIM 练级攻略
- 无法加载指定的元数据资源
- Android的消息机制深度分析
- 成为Java GC专家(4)—Apache的MaxClients参数详解及其在Tomcat执行FullGC时的影响
- java中的关键字
- 【Mybatis从0到1-016】Spring与MyBatis整合mapper开发DAO(推荐使用)
- tensorflow使用中的坑:module'tensorflow'has no attribute 'sub'
- 开发框架汇总
- 2017年迄今最新人工智能资源盘点 [转载]
- 学习基础Java(一)
- 1068. 万绿丛中一点红(20)
- (HDU
- 自动行为操控Steering(三)—远离(单体操控)
- JavaWeb核心技术学习笔记(二)——Http协议