因为 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;
}
}

 项目DemoMiniJavaWeb

Mybatis-Spring整合Spring的更多相关文章

  1. MyBatis之整合Spring

    MyBatis之整合Spring 整合思路: 1.SqlSessionFactory对象应该放到spring容器中作为单例存在 2.传统dao的开发方式中,应该从spring容器中获得sqlSessi ...

  2. 【Mybatis】MyBatis之整合Spring(八)

    创建环境 系统:macOS Java:1.8 软件:eclipse,maven,mysql 创建步骤 本例:创建一个Maven项目(SpringMVC+Spring+Mybatis),页面上展示员工列 ...

  3. spring 整合 spring mvc

    需要进行 Spring 整合 SpringMVC 吗 ? 还是否需要再加入 Spring 的 IOC 容器 ? 是否需要再 web.xml 文件中配置启动 Spring IOC 容器的 Context ...

  4. Mybatis整合Spring

    根据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对Mybatis3的支持.因此由Mybatis社区自己开发了一个My ...

  5. 框架整合——Spring与MyBatis框架整合

    Spring整合MyBatis 1. 整合 Spring [整合目标:在spring的配置文件中配置SqlSessionFactory以及让mybatis用上spring的声明式事务] 1). 加入 ...

  6. MyBatis - 6.Spring整合MyBatis

    1.查看不同MyBatis版本整合Spring时使用的适配包: http://www.mybatis.org/spring/ 2.下载整合适配包 https://github.com/mybatis/ ...

  7. Mybatis整合Spring -- typeAliasesPackage

    Mybatis整合Spring 根据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对Mybatis3的支持. 因此由M ...

  8. 160330、Mybatis整合Spring

    转自csdn文章 http://haohaoxuexi.iteye.com/blog/1843309 Mybatis整合Spring 根据官方的说法,在ibatis3,也就是Mybatis3问世之前, ...

  9. MyBatis 学习-与 Spring 集成篇

    根据官方的说法,在 ibatis3,也就是 Mybatis3 问世之前,Spring3 的开发工作就已经完成了,所以 Spring3 中还是没有对 Mybatis3 的支持.因此由 Mybatis 社 ...

  10. MyBatis学习(四)MyBatis和Spring整合

    MyBatis和Spring整合 思路 1.让spring管理SqlSessionFactory 2.让spring管理mapper对象和dao. 使用spring和mybatis整合开发mapper ...

随机推荐

  1. [转]hadoop运行mapreduce作业无法连接0.0.0.0/0.0.0.0:10020

    14/04/04 17:15:12 INFO mapreduce.Job:  map 0% reduce 0% 14/04/04 17:19:42 INFO mapreduce.Job:  map 4 ...

  2. java之字符

    参考http://how2j.cn/k/number-string/number-string-character/323.html 保存一个字符的时候使用char package character ...

  3. winform中的ComboBox同时设置text和value的方法

    winform中的ComboBox不能像webform中的dropdownlist控件一样,在属性中可以同时设置text和value值,可以通过编写一个新类来实现这个功能. 1.首先在form1中添加 ...

  4. 【ssm整合打印sql语句】

    #定义LOG输出级别log4j.rootLogger=INFO,Console,File #定义日志输出目的地为控制台log4j.appender.Console=org.apache.log4j.C ...

  5. VS(Visual Studio)中快速找出含中文的字符串

    环境:visual studio 2017 1.ctrl + shift + f 打卡全局查找 2.输入(".*[\u4E00-\u9FA5]+)|([\u4E00-\u9FA5]+.*&q ...

  6. [USACO09FEB]庙会班车Fair Shuttle 线段树维护maxx&&贪心

    题目描述 Although Farmer John has no problems walking around the fair to collect prizes or see the shows ...

  7. 关于unique去重

    嗯.... unique这个东西也是一个冷门知识..... 但是在有时候它还是比较好用的东西... 下面就在详细代码中看unique是如何实际应用的....它主要是用于数组去重 #include< ...

  8. EIGRP-8-路由器的邻接关系

    EIGRP路由器之间会建立并维护邻接关系.EIGRP在默认情况下会动态发现邻居路由器.也可以通过工程师手动配置(静态)发现邻居.   通过向目的组播组地址224.0.0.10或FF02: : A发送E ...

  9. ACM-ICPC 2018 徐州赛区网络预赛 B(dp || 博弈(未完成)

    传送门 题面: In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl n ...

  10. Java中23种设计模式--超快速入门及举例代码

    在网上看了一些设计模式的文章后,感觉还是印象不太深刻,决定好好记录记录. 原文地址:http://blog.csdn.net/doymm2008/article/details/13288067 注: ...