mybatis-generator-spring-boot-starter

Parent pom providing dependency and plugin management for applications built with Maven

License

License

Categories

Categories

Spring Boot Container Microservices MyBatis Data ORM
GroupId

GroupId

io.github.itfinally
ArtifactId

ArtifactId

mybatis-generator-spring-boot-starter
Last Version

Last Version

0.1.2.RELEASE
Release Date

Release Date

Type

Type

jar
Description

Description

mybatis-generator-spring-boot-starter
Parent pom providing dependency and plugin management for applications built with Maven
Project Organization

Project Organization

Pivotal Software, Inc.

Download mybatis-generator-spring-boot-starter

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.itfinally/mybatis-generator-spring-boot-starter/ -->
<dependency>
    <groupId>io.github.itfinally</groupId>
    <artifactId>mybatis-generator-spring-boot-starter</artifactId>
    <version>0.1.2.RELEASE</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.itfinally/mybatis-generator-spring-boot-starter/
implementation 'io.github.itfinally:mybatis-generator-spring-boot-starter:0.1.2.RELEASE'
// https://jarcasting.com/artifacts/io.github.itfinally/mybatis-generator-spring-boot-starter/
implementation ("io.github.itfinally:mybatis-generator-spring-boot-starter:0.1.2.RELEASE")
'io.github.itfinally:mybatis-generator-spring-boot-starter:jar:0.1.2.RELEASE'
<dependency org="io.github.itfinally" name="mybatis-generator-spring-boot-starter" rev="0.1.2.RELEASE">
  <artifact name="mybatis-generator-spring-boot-starter" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.itfinally', module='mybatis-generator-spring-boot-starter', version='0.1.2.RELEASE')
)
libraryDependencies += "io.github.itfinally" % "mybatis-generator-spring-boot-starter" % "0.1.2.RELEASE"
[io.github.itfinally/mybatis-generator-spring-boot-starter "0.1.2.RELEASE"]

Dependencies

compile (9)

Group / Artifact Type Version
io.github.itfinally : mybatis-generator jar 0.1.2.RELEASE
org.mybatis.spring.boot : mybatis-spring-boot-starter jar 1.3.2
org.springframework.boot : spring-boot-starter-log4j2 jar 1.5.14.RELEASE
com.lmax : disruptor jar 3.3.4
org.springframework.boot : spring-boot-configuration-processor Optional jar 1.5.14.RELEASE
javax.persistence : persistence-api jar 1.0.2
com.zaxxer : HikariCP-java7 jar 2.4.13
mysql : mysql-connector-java jar 5.1.46
com.google.guava : guava jar 25.1-android

Project Modules

There are no modules declared in this project.

Mybatis-Helper

该项目是 Mybatis 插件包, 用于扩展 Mybatis 的特性, 其中包含 生成器( generator ) / 分页插件( paging ) / JPA ( 对象查询 ).

该项目基于 Java7, Mybatis3.4.6 开发, 并且使用 HikariCP-java7 作为连接池, log4j2 作为日志系统, 如若需要使用其他组件, 请自行定义 exclusion.

此处是各组件均使用的配置或操作, 组件详细的文档请查阅各自的文件夹.

多数据源配置

如果需要各组件支持多数据源, 首先需要自行定义多个 DataSource

@Bean( "masterDataSource" )
public DataSource() {
  // maybe master dataSource?
}

@Bean( "replicaDataSource" )
public DataSource() {
  // maybe replica dataSource?
}

然后初始化 DynamicDataSourceRouter, 这是动态数据源路由, 继承了 spring 提供的 AbstractRoutingDataSource, 而该类又间接实现了 DataSource, 因此该类可以直接作为数据源使用.

@Primary
@Bean( "dataSourceRouter" )
public DataSource( ApplicationContext context ) {
  DynamicDataSourceRouter dataSourceRouter = new DynamicDataSourceRouter( context );
  dataSourceRouter.setDefaultTargetDataSource( "dataSourceMaster" );
  
  Map<Object, Object> dataSourceMap = new HashMap<>();
  
  // key 是数据源的别名, 可以通过该别名动态转变使用的数据源
  // value 是真实数据源实例在 spring 的别名, 当然 value 也可以是 DataSource 实例
  dataSourceMap.put( "master", "dataSourceMaster" );
  dataSourceMap.put( "replica", "dataSourceReplica" );
  dataSourceRouter.setTargetDataSources( dataSourceMap );
  
  return dataSourceRouter;
}

首先注意这里是用了 @Primary 标记该数据源路由, 后续所有使用数据源均使用该路由, 而路由本身是通过抽象方法 determineCurrentLookupKey() 获取当前使用的数据源的 key, 也就是上面变量 dataSourceMap 所配置的 key.

当需要改变数据源时, 通过 DynamicDataSourceRouter.setDataSourceName( String name ) 给出数据源的别名, 即可改变后续操作使用的数据源.

当然这种做法虽然是最简单的, 但是可能无法在同一个函数作用域内使用不同的数据源. 除非使用 AOP 拦截 Mapper 并强制使用JDK代理, 但这样亦会导致无法在同一事务内操作, 因为各个 Mapper 的数据库连接都是不相同的.

因此对于主从分离这类场景, 最好还是使用诸如 sharding-jdbc 等第三方解决方案.

Versions

Version
0.1.2.RELEASE
0.1.1.RELEASE
0.1.0.RELEASE