MyBatis SpringBoot 杂记
最近接了个xxx代码.
不能说人家不好, 因为必进年月久了.能用这么长时间, 不就说明还不错么?! 我们现在每天写的, 能超出人家的么~~~ 呵呵
Java项目中, 把动态数据源切换的框架整合进来. 嗯...这个有好有不好吧.
按Service层使用AOP来切换主从. 但是企业应用非常复杂. 如果再加一个别的数据库连接进来, 框架就显得不够用.
而框架的修改权又掌握在某一个部门手中, 所以,只能类似补丁地融入. 所以有了这一上午走读Spring/MyBatis的机会. 也挺好.
SpringBoot的一个annotation @SpringBootApplication, 使用其 exclude, 把框架中的某一些 @Configuration 排除掉.
然后自己写子类继承这些原有的@Configuration类, 并且加上@Configuration.
然后就可以初始化了.
原有容器初始化的时候, 以@Bean的方式生产了SqlSessionFactory, 默认执行Mapper的时候就会使用.
所以,如果用新加进来的DataSource也生成@Bean SqlSessionFactory, 那么Mapper就会迷茫,因为默认是按类型查找SqlSessionFactory.
解决这个问题, 改框架动静太大. 简单一点, 把原来的@Bean SqlSessionFactory加上@Primary,为默认. 然后新数据库连接使用的时候,可以使用
@Autowired @Quirified("xxx") 来注入SqlSessionFactory, 然后在@PostConstruct里, 把SqlSessionTemplate获取到,
然后,可以使用 sqlSessionTemplate.getMapper(Xxx.class) 获取到 Mapper.class接口的代理实例.
在这里踩了一个坑, 就是 getMapper总是说没有注册. 于是追踪了一下Mapper的注册过程.
有几篇blog写得挺好. 但是太冗长
Mybatis初始化加载流程————配置文件解析
https://blog.csdn.net/u011043551/article/details/80607050
Mybatis 源码学习(一) Mapper 注册
https://blog.csdn.net/tiantiandjava/article/details/80569451 Mybatis初始化加载流程————Mapper接口注册
https://blog.csdn.net/u011043551/article/details/80619319 关键点: SqlSessionFactoryBean 是建造者模式, 设置了一堆属性之后, 然后调用 getObject()的时候,
执行
afterPropertiesSet();-->buildSqlSessionFactory(); --> 一大段.
这大段代码的基本逻辑有3点:
1.注册Class, 基本上是Example, DTO 等等.
2.注册plugins, 比如翻页插件就在这里注册
3.注册handler, 这一段没特别了解, 后面用到再看看
4.注册xml, xmlConfigBuilder.parse()
这一段包含了这次的内容. 一大段.
private void parseConfiguration(XNode root) {
try {
//issue #117 read properties first
propertiesElement(root.evalNode("properties"));
Properties settings = settingsAsProperties(root.evalNode("settings"));
loadCustomVfs(settings);
typeAliasesElement(root.evalNode("typeAliases"));
pluginElement(root.evalNode("plugins"));
objectFactoryElement(root.evalNode("objectFactory"));
objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
reflectorFactoryElement(root.evalNode("reflectorFactory"));
settingsElement(settings);
// read it after objectFactory and objectWrapperFactory issue #631
environmentsElement(root.evalNode("environments"));
databaseIdProviderElement(root.evalNode("databaseIdProvider"));
typeHandlerElement(root.evalNode("typeHandlers"));
mapperElement(root.evalNode("mappers"));
} catch (Exception e) {
throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
}
}
基本是解析xml各个节点.
其中mapperElement 这个方法里,
把xml节点中mapper 进行解析.调用mapperParser.parse();(XMLMapperBuilder) ---> bindMapperForNamespace();
private void bindMapperForNamespace() {
String namespace = builderAssistant.getCurrentNamespace();
if (namespace != null) {
Class<?> boundType = null;
try {
boundType = Resources.classForName(namespace);
} catch (ClassNotFoundException e) {
//ignore, bound type is not required
}
if (boundType != null) {
if (!configuration.hasMapper(boundType)) {
// Spring may not know the real resource name so we set a flag
// to prevent loading again this resource from the mapper interface
// look at MapperAnnotationBuilder#loadXmlResource
configuration.addLoadedResource("namespace:" + namespace);
configuration.addMapper(boundType);
}
}
}
}
会把xml中namespace中写着的xxx.xxx.Mapper认为是类, 来加载注册!!! 藏得真是深. 参考了:
Spring读取mybatis在多个jar包下的的mapper文件
https://blog.csdn.net/zixuan0104/article/details/54601563
primary这个翻译过来是 首要的,首选的意思。
primary的值有true和false两个可以选择。默认为false。
当一个bean的primary设置为true,然后容器中有多个与该bean相同类型的其他bean,
此时,当使用@Autowired想要注入一个这个类型的bean时,就不会因为容器中存在多个该类型的bean而出现异常。而是优先使用primary为true的bean。
不过,如果容器中不仅有多个该类型的bean,而且这些bean中有多个的primary的值设置为true,那么使用byType注入还是会出错。
https://blog.csdn.net/qq_36951116/article/details/79130591
https://www.cnblogs.com/dongying/p/4142476.html
希望Mapper自动分了数据源, 需要在MapperScan上加上ref
@MapperScan(basePackages="com.XXX.bpaas.idm.mapper.dao", sqlSessionTemplateRef = "idmSqlSessionTemplate")
或
@MapperScan(basePackages="com.XXX.bpaas.idm.mapper.dao", sqlSessionFactoryRef = "xxxFactory"), 这样就可以在注册每个Mapper的时候,匹配到正确的数据源.
主从切换的数据源是在线程切换层次, 不会影响到这里.
MapperScan决定xxxMapper.select 使用 那个xxxxFactory, 执行时, 如果该数据源有主从切换,读写分离,在ThreadLocak<Holder>中切换变量值就OK了.
>>参照了
https://blog.csdn.net/mxw2552261/article/details/78640062 good
https://blog.csdn.net/weixin_40562217/article/details/82840764 good
https://www.cnblogs.com/dongying/p/4142476.html
https://www.cnblogs.com/dongying/p/4040435.html
下一步研究一下, 事务传播, 两个数据源的@Transactional 嵌套时有什么问题.
https://www.cnblogs.com/Kidezyq/p/8541199.html 采坑
https://www.cnblogs.com/zishengY/p/6850673.html 多个事务管理器
MyBatis SpringBoot 杂记的更多相关文章
- springboot(三 使用mybatis +springboot 完成简单的增删改查)
先说一些注解: @EnableAutoConfiguration 可以帮助SpringBoot应用将所有符合条件的@Configuration配置都加载到当前SpringBoot创建并使用的IoC容器 ...
- yml的mybatis的sql查看;Mybatis+Springboot 控制台查看日志,Mybatis结合springboot打印日志
1.配置如图 文件为yml mybatis: mapper-locations: classpath:com/springboot/transaction/mapper/*.xml configura ...
- IntelliJ IDEA 2017版 spring-boot2.0.4+mybatis反向工程;mybatis+springboot逆向工程
一.搭建环境 采用IDE自动建立项目方式 然后,next next,配置导入依赖包 项目就生成了,在项目下导入配置文件GeneratorMapper.xml(项目结构如图所示) 配置文档,建立数据库和 ...
- SpringBoot杂记
一.配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的: •application.properties •application.yml 配置文件的作用:修改SpringBoo ...
- Mybatis+SpringBoot 项目All elements are null
xml文件内容如下 查出来的集合长度是有的,但是会出现All elements are null 解决方案: 注意我的xml文件全部是这样的,并且我调用的sql返回值是 resultType=&qu ...
- SpringBoot+Mybatis集成搭建
本博客介绍一下SpringBoot集成Mybatis,数据库连接池使用alibaba的druid,使用SpringBoot微框架虽然集成Mybatis之后可以不使用xml的方式来写sql,但是用惯了x ...
- Springboot mybatis generate 自动生成实体类和Mapper
https://github.com/JasmineQian/SpringDemo_2019/tree/master/mybatis Springboot让java开发变得方便,Springboot中 ...
- SpringBoot集成MyBatis的Bean配置方式
SpringBoot集成MyBatis的Bean配置方式 SpringBoot是一款轻量级开发的框架,简化了很多原先的xml文件配置方式,接下来就介绍一下如何不适用XML来配置Mybatis spri ...
- MyBatis原理,Spring、SpringBoot整合MyBatis
1. MyBatis概述 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可 ...
随机推荐
- spring boot启动报错
Exception encountered during context initialization - cancelling refresh attempt: org.springframewor ...
- 4-cookie 简介
1.eclipse中tomcate镜像位置:D:\javaTools\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\ ...
- day25 Python四个可以实现自省的函数,反射
python面向对象中的反射:通过字符串的形式操作对象相关的属性.python中的一切事物都是对象(都可以使用反射) 四个可以实现自省的函数 下列方法适用于类和对象(一切皆对象,类本身也是一个对象) ...
- PostgreSQL安装和使用
青岛OJ系统用的关系型数据库是PostgreSQL,为此对PostgreSQL大致了解下. 今天的主要话题围绕下面两个方面: PostgreSQL安装 PostgreSQL使用 一.PostgreSQ ...
- PAT A1055 The World's Richest (25 分)——排序
Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...
- NOIP2018初赛游记
NOIP2018初赛游记 (编辑中)
- Ubuntu中libprotobuf版本冲突的解决方案
先说解决方法: 因为我出现这个比较奇特,我再下面环境中的第一个项目有这个问题,但是不知道怎么瞎折腾就搞定了,不报这个异常了 不论是Qt Creator直接运行Debug或者Release都没问题 但是 ...
- wpf、winform仿QQ靠边隐藏
先说下下面的代码和demo是wpf的,如果winform要用,改动不大的. 实现思路: 通过定时刷新鼠标位置 和 窗体坐标 进行计算 来控制窗体的隐藏 显示 代码都有详细的注释 //窗体状态 true ...
- 【原创】Mysql中select的正确姿势
引言 大家在开发中,还有很多童鞋在写查询语句的时候,习惯写下面这种不规范sql select * from table 而不写成下面的这种规范方式 select col1,col2,...,coln ...
- Java消息中间件入门笔记 - ActiveMQ篇
入门 消息中间件带来的好处: 1)解耦:系统解耦 2)异步:异步执行 3)横向扩展 4)安全可靠 5)顺序保证 栗子: 通过服务调用让其它系统感知事件发生 系统之间高耦合 程序执行效率低 通过消息中间 ...