Skip to content

Commit

Permalink
针对Mysql优化,主键获得策略使用useGeneratedKeys。
Browse files Browse the repository at this point in the history
  • Loading branch information
slankka committed Jun 7, 2018
1 parent 074e54b commit 3dffffe
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,18 @@ public void generate() throws Exception {

//添加GeneratedKey主键生成
if (StringUtils.isNoneEmpty(generatorConfig.getGenerateKeys())) {
tableConfig.setGeneratedKey(new GeneratedKey(generatorConfig.getGenerateKeys(), selectedDatabaseConfig.getDbType(), true, null));
String dbType = selectedDatabaseConfig.getDbType();
if (DbType.MySQL.name().equals(dbType)) {
dbType = "JDBC";
//dbType为JDBC,且配置中开启useGeneratedKeys时,Mybatis会使用Jdbc3KeyGenerator,
//使用该KeyGenerator的好处就是直接在一次INSERT 语句内,通过resultSet获取得到 生成的主键值,
//并很好的支持设置了读写分离代理的数据库
//例如阿里云RDS + 读写分离代理
//无需指定主库
//当使用SelectKey时,Mybatis会使用SelectKeyGenerator,INSERT之后,多发送一次查询语句,获得主键值
//在上述读写分离被代理的情况下,会得不到正确的主键
}
tableConfig.setGeneratedKey(new GeneratedKey(generatorConfig.getGenerateKeys(), dbType, true, null));
}

if (generatorConfig.getMapperName() != null) {
Expand Down

0 comments on commit 3dffffe

Please sign in to comment.