博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MyBatis-Spring 使用总结
阅读量:7096 次
发布时间:2019-06-28

本文共 3366 字,大约阅读时间需要 11 分钟。

说明:Java-based Config。

不是通过 mybatis 的 SqlSessionFactoryBuilder 来创建 SqlSessionFactory ,而是通过 mybatis-spring 的 SqlSessionFactoryBean 来获取。

1、首先要有一个
DataSource
需要注意,事务管理器也在这里注册。(
mybatis-spring插件会自动调用该事务管理器)  
@Bean(name = "transactionManager")    public DataSourceTransactionManager dataSourceTransactionManager() {        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();        dataSourceTransactionManager.setDataSource(this.dataSource());        return dataSourceTransactionManager;    }
 
2、然后,注册
SqlSessionFactoryBean
或者
SqlSessionFactory,二选一,内容一致)。如下:
@Bean(name = "sqlSessionFactory")    public SqlSessionFactory sqlSessionFactory() throws Exception {        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();        sqlSessionFactoryBean.setDataSource(this.dataSource());        // sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml")); // 这里可以通过mybatis-config.xml 来设置 typeAliasPackage和mapper。        // Resource[] mapperLocations = new Resource[] { new ClassPathResource("com.expert.dao") }; // 这个和@MapperScan冲突吗?这个设置有问题。        // sqlSessionFactoryBean.setMapperLocations(mapperLocations);//
sqlSessionFactoryBean.setTypeAliasesPackage(PojoBasePackage); // sqlSessionFactoryBean.setCache(cache); SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBean.getObject(); // It can be specified a Configuration instance directly without MyBatis XML configuration file. sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);// 开启驼峰映射 sqlSessionFactory.getConfiguration().setCacheEnabled(true); sqlSessionFactory.getConfiguration().setLazyLoadingEnabled(true); sqlSessionFactory.getConfiguration().setAggressiveLazyLoading(false); // Class
logImpl = sqlSessionFactory.getConfiguration().getTypeAliasRegistry().resolveAlias("SLF4J"); sqlSessionFactory.getConfiguration().setLogImpl(Slf4jImpl.class);// logImpl sqlSessionFactory.getConfiguration().setLogPrefix("###SPRING_BOOT###MYBATIS###"); sqlSessionFactory.getConfiguration().setDefaultExecutorType(ExecutorType.REUSE); sqlSessionFactory.getConfiguration().setUseGeneratedKeys(true); return sqlSessionFactory; }
这里还设置了一堆参数。需要注意的是,
①设置了 
TypeAliasesPackage 
②设置了 
Configuration 。
③mybatis-spring会自动创建 
Configuration 对象,所以通过 
sqlSessionFactory.getConfiguration() 即可获取并进行设置。
 
3、再注册一个
SqlSessionTemplate,这是 mybatis-spring 的
核心
@Bean    @Scope(BeanDefinition.SCOPE_PROTOTYPE) // 多例?    public SqlSessionTemplate sqlSessionTemplate() throws Exception {        return new SqlSessionTemplate(this.sqlSessionFactory());    }
 
4、设置mapper的位置,有两种方法。
①推荐这种,简单。
@Configuration@MapperScan(basePackages = { "com.expert.dao" })public class DruidDataSourceConfig{    // ...}
@Bean    public MapperScannerConfigurer mapperScannerConfigurer() {        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();        mapperScannerConfigurer.setBasePackage(DaoBasePackage);        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");        return mapperScannerConfigurer;    }

 

至此,已可以成功运行。
 
但是,还有
更简单的方法,那就是 
MyBatis-Spring-Boot-Starter 。使用该Starter时,会自动查找DataSource,并自动创建SqlSessionFactoryBean 和 SqlSessionTemplate。所以,只需要设置mapper所在的位置和别名所在的包即可。
 
见 
。 
 
 
 
 

转载地址:http://snhql.baihongyu.com/

你可能感兴趣的文章
Java线程池
查看>>
ArrayList,LinkedList,Vector,Stack之间的区别
查看>>
Freemarker常用技巧(二)
查看>>
2.C#中通过委托Func消除重复代码
查看>>
[转] 基于PHP Stream Wrapper开发有趣应用场景
查看>>
JS获取屏幕大小
查看>>
hdu2222-Keywords Search 【AC自动机】
查看>>
Jsp使用HttpSessionBindingListener实现在线人数记录
查看>>
SQL中的等号、IN、LIKE三者的比较
查看>>
JSPatch 成长之路
查看>>
vuejs学习网站推荐
查看>>
如何在Fedora或CentOS上使用Samba共享
查看>>
乐视mysql面试题
查看>>
常用文件扩展名
查看>>
如何让Linux定时任务crond以秒为单位执行(如每隔3秒)
查看>>
二叉树的构造
查看>>
使用密码记录工具keepass来保存密码
查看>>
linux中线程池【转】
查看>>
php通过字符串生存hashCode
查看>>
SQL Server memory – Internals
查看>>