Mybatis-Spring整合Spring
因为 MyBatis 用 SqlSessionFactory 来创建 SqlSession ,SqlSessionFactoryBuilder 创建 SqlSessionFactory ,而在 Mybatis-Spring 中提供了继承自 Spring 接口 FactoryBean 的 SqlSessionFactoryBean 类,它提供 getObject() 方法来获取 SqlSessionFactory 。所以可以用基于Java配置的方式配置 SqlSessionFactory 。
MybatisDataConfig配置类配置了MyBatis的dataSource、sql语句xml配置文件路径、数据库类型包路径
@Configuration
public class MybatisDataConfig { private DataSource dataSource; @Autowired
public MybatisDataConfig(DataSource dataSource) {
this.dataSource = dataSource;
} /**
* 必须指定名称 sqlSessionFactory.
*/
@Bean("sqlSessionFactory")
public SqlSessionFactory getBean() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
factoryBean.setMapperLocations(resolver.getResources("classpath*:mapper/*.xml"));
factoryBean.setTypeAliasesPackage("com.test.entity");
return factoryBean.getObject();
} }
为了减少xml配置,Mybatis-Spring还提供了 MapperScannerConfigurer 类,它可以扫描映射类,自动创建 MapperFactoryBean 来吧dao注入给service,也就是说service可以直接通过@Autowired等方式,直接注入dao层接口实例,而dao层的接口不需要其他额外的配置。
/**
* <p>MybatisScannerConfig 和 MybatisDataConfig 需要分开写.</p>
* <p>而 MybatisScannerConfig 比 MybatisDataConfig 加载早.</p>
* <p>所以要定义@AutoConfigureAfter.</p>
* <p>否则 MybatisDataConfig 先加载会找不到 dataSource.</p>
* @see MybatisDataConfig
*/
@Configuration
@AutoConfigureAfter(MybatisDataConfig.class)
public class MybatisScannerConfig { /**
* 基于Java配置sqlSessionFactory,扫描dao层.
* @return 配置实例
*/
@Bean
public MapperScannerConfigurer getConfig() {
MapperScannerConfigurer configurer = new MapperScannerConfigurer();
configurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
configurer.setBasePackage("com.test.dao");
return configurer;
}
}
项目Demo:MiniJavaWeb
Mybatis-Spring整合Spring的更多相关文章
- MyBatis之整合Spring
MyBatis之整合Spring 整合思路: 1.SqlSessionFactory对象应该放到spring容器中作为单例存在 2.传统dao的开发方式中,应该从spring容器中获得sqlSessi ...
- 【Mybatis】MyBatis之整合Spring(八)
创建环境 系统:macOS Java:1.8 软件:eclipse,maven,mysql 创建步骤 本例:创建一个Maven项目(SpringMVC+Spring+Mybatis),页面上展示员工列 ...
- spring 整合 spring mvc
需要进行 Spring 整合 SpringMVC 吗 ? 还是否需要再加入 Spring 的 IOC 容器 ? 是否需要再 web.xml 文件中配置启动 Spring IOC 容器的 Context ...
- Mybatis整合Spring
根据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对Mybatis3的支持.因此由Mybatis社区自己开发了一个My ...
- 框架整合——Spring与MyBatis框架整合
Spring整合MyBatis 1. 整合 Spring [整合目标:在spring的配置文件中配置SqlSessionFactory以及让mybatis用上spring的声明式事务] 1). 加入 ...
- MyBatis - 6.Spring整合MyBatis
1.查看不同MyBatis版本整合Spring时使用的适配包: http://www.mybatis.org/spring/ 2.下载整合适配包 https://github.com/mybatis/ ...
- Mybatis整合Spring -- typeAliasesPackage
Mybatis整合Spring 根据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对Mybatis3的支持. 因此由M ...
- 160330、Mybatis整合Spring
转自csdn文章 http://haohaoxuexi.iteye.com/blog/1843309 Mybatis整合Spring 根据官方的说法,在ibatis3,也就是Mybatis3问世之前, ...
- MyBatis 学习-与 Spring 集成篇
根据官方的说法,在 ibatis3,也就是 Mybatis3 问世之前,Spring3 的开发工作就已经完成了,所以 Spring3 中还是没有对 Mybatis3 的支持.因此由 Mybatis 社 ...
- MyBatis学习(四)MyBatis和Spring整合
MyBatis和Spring整合 思路 1.让spring管理SqlSessionFactory 2.让spring管理mapper对象和dao. 使用spring和mybatis整合开发mapper ...
随机推荐
- Gulp的学习和使用
Gulp是一种直观.自动化构建的工具. Gulp是基于Node和NPM,安装教程点这里. 什么是Gulp? Gulp使用了node.js的流控制系统,使其(Gulp)构建更快,因为它不需要将临时文件/ ...
- 高考是最后一次拼智商的事了。(beacuse 大多数人的努力程度之低根本轮不到拼天赋!)
高考是最后一次拼智商的事. —因为大多数人的努力程度之低 根本轮不到拼天赋 在这个不起眼的小公司实习也有两周了,周四经理说说为了增加IOS开发小组和安卓开发小组之间的交流,准备每周开一次这种报告会. ...
- matlab求定积分和不定积分
matlab求定积分与不定积分 创建于2018-03-21 22:42 求定积分与不定积分是一件比较繁琐的事,但是我们可以借助matlab,下面与大家分享解决方法 材料/工具 matlab 求不定积分 ...
- [hdu 1671] Phone List - Trie
Given a list of phone numbers, determine if it is consistent in the sense that no number is the pref ...
- Unity技术支持团队性能优化经验分享
https://mp.weixin.qq.com/s?__biz=MzU5MjQ1NTEwOA==&mid=2247490321&idx=1&sn=f9f34407ee5c5d ...
- linux系统elementray os的环境搭建
因为我在使用过程中为了改变终端的外表,结果把/ect/psswd,以及/ect/profile中的文件配置修改之后,我把gnome-terminal的python脚本打包放在/bin/目录下,修改了/ ...
- Maven项目骨架搭建
1. 如何使用Maven的archetype快速生成一个新项目 2. Maven之自定义archetype生成项目骨架(一) 3. 使用maven3 创建自定义的archetype 4. 使用mave ...
- express-http-proxy 的基础使用
const app = express() app.use(matchPath, proxy(serverAddress, { proxyReqPathResolver: function(req) ...
- centos6.6上安装beef
https://rvm.io/https://rvm.io/rvm/securityhttps://www.runoob.com/ruby/ruby-installation-unix.htmlhtt ...
- ios 检测是否安装微信异常
解决方法 在info.plist 添加LSApplicationQueriesSchemes 类型是Array weixin wechat