Spring整合mybatis (二)
来源:互联网 发布:粤语入门软件 编辑:程序博客网 时间:2024/06/05 20:13
Spring整合mybatis有很多种方式,博主之前文章写了一种方式,是用SqlSessionTemplate,现在介绍其他的几种方式。配置文件中的dataSource配置就略了
第二种方式,使用SqlSessionDaoSupport(这种方式比较新,需要新版的整合jar包)
这种方式不需要管理SqlSessionTemplate,在Dao的实现中需要继承SqlSessionDaoSupport
dao实现
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{ @Override public List<User> selectUser() { return getSqlSession().selectList("cn.sxt.entity.user.mapper.selectAll"); }}
配置文件
<!-- dataSource的配置略了 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis.cfg.xml"/> </bean> <bean id="userDao" class="cn.sxt.dao.impl.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean>
观看源码可以得知SqlSessionDaoSupport本质还是和SqlSessionTemplate一样,只不过封装在了里面,减少了bean的配置。DAO中也不用写成员,直接this.getSqlSession()得到session
第三种 方式
mybatis使用注解
userMapper
public interface UserMapper { @Select("select * from user") public List<User> selectUser(); }
UserServiceImpl
public class UserServiceImpl implements UserService{ private UserMapper userMapper = null; public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } @Override public List<User> selectUser() { return userMapper.selectUser(); }}
beans.xml
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis.cfg.xml"/> </bean> <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="cn.sxt.dao.UserMapper"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> <bean id="userService" class="cn.sxt.service.impl.UserServiceImpl"> <property name="userMapper" ref="userMapper"/> </bean>
第四中方式 不需要mybatis的配置文件,将所有的配置信息配置到spring的配置文件中
因为原来额的mybatis中dataSource已经给Spring管理了,别名可以不设置,那么mybatis.cfg.xml里面只剩下
<mappers><mapper resource="cn/sxt/entity/user.mapper.xml"/></mappers>
而这个完全可以由spring来管理
改进后配置如下,dataSource的配置略了
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:cn/sxt/vo/*.mapper.xml"></property> </bean> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> <bean id="userDao" class="cn.sxt.dao.impl.UserDaoImpl"> <property name="sqlSession" ref="sqlSessionTemplate"/> </bean>
阅读全文
0 0
- Spring整合mybatis (二)
- (二)Mybatis整合Spring
- 二、Mybatis-Spring整合配置
- Spring整合mybatis方式二
- Spring MVC+Mybatis 框架整合(二)
- Spring整合MyBatis(二)源码分析
- Spring整合MyBatis(二)源码分析
- mybatis与spring整合(方式二)
- Mybatis -- Mybatis整合Spring
- 【Mybatis】Mybatis整合spring
- mybatis系列二:Spring与MyBatis的整合实例
- mybatis系列教程(二)——spring整合mybatis
- Mybatis源码(二)之Spring整合mybatis创建SqlSession
- spring mvc+spring+mybatis+maven整合篇(二)
- spring mvc高级篇(二):Spring+SpringMVC+Mybatis整合
- 【Spring+Mybatis】Spring整合Mybatis
- Java知识总结---整合SpringMVC+Mybatis+Spring(二)
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(二)牛刀小试
- /proc/sys/net/ipv4/配置Linux网络参数
- 利用arcgis实现经纬度和平面坐标互转
- MySQL update语句嵌套select语句同一张表
- mysql 正则匹配内容查询、拼接sql语句
- 视图解析器(7)
- Spring整合mybatis (二)
- 机器人领域几大国际会议
- 请接好这份关于流程图怎么画的攻略
- QPropertyAnimation移动动画小Demo
- 《图解HTTP》整理(3)
- android 获取当前系统的语言
- Google死磕Facebook_想知道AI巨头为何钟情于游戏?
- 代码干货 | vue.js列表渲染
- rhel 下docker的安装配置与使用