<!-- mybatis plus 代码生成器引擎依赖-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<!-- mybatis plus 代码生成器依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
<!-- 多数据源-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
public class MpCode {
public static void main(String[] args) {
generate();
}
private static void generate(){
FastAutoGenerator.create("jdbc:mysql://192.168.10.231:3307/gd_common_caiji?serverTimezone=UTC", "bj", "bj2016")
.globalConfig(builder -> {
builder.author("zha") // 设置作者
/*.enableSwagger() */// 开启 swagger 模式
.fileOverride() // 覆盖已生成文件
.outputDir("D:\\my_code\\sg_three_schedule\\src\\main\\java"); // 指定输出目录 和自己项目src一一对应
})
.packageConfig(builder -> {
builder.parent("com.wg") // 设置父包名
.moduleName(null) // 设置父包模块名 解决路径冲突
.serviceImpl("service.impl") //自定义包名imp 官方的是impi
.entity("entity") //设置实体包为pojo
.pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D:\\my_code\\sg_three_schedule\\src\\main\\java\\com\\wg\\mapper")); // 设置mapperXml生成路径
})
.strategyConfig(builder -> {
builder.entityBuilder().enableLombok();//使用lombok
builder.controllerBuilder().enableHyphenStyle()
.enableRestStyle();//开启RestController
builder.addInclude("all_twitter_users"); // 设置需要生成的表名
})
// .templateEngine(new VelocityTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
}
}
该自动生成器在使用的时候发现,Mapper上不会自动加入注释,需要手动加@Mapper。例如:
@Mapper
public interface AccountAnalysisMapper extends BaseMapper<AccountAnalysis> {
}
@Mapper
public interface AllFacebookUsersMapper extends BaseMapper<AllFacebookUsers> {
}
多数据源配置如下:
spring.datasource.dynamic.primary=gd_data
spring.datasource.dynamic.strict=false
spring.datasource.dynamic.datasource.gd_data.url=jdbc:mysql://aaaa:3307/gd_data?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&failOverReadOnly=false
spring.datasource.dynamic.datasource.gd_data.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.dynamic.datasource.gd_data.username=bj
spring.datasource.dynamic.datasource.gd_data.password=zxc
spring.datasource.dynamic.datasource.gd_common_caiji.url=jdbc:mysql://aaaa:3307/gd_common_caiji?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&failOverReadOnly=false
spring.datasource.dynamic.datasource.gd_common_caiji.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.dynamic.datasource.gd_common_caiji.username=bj
spring.datasource.dynamic.datasource.gd_common_caiji.password=qazx
然后再ServiceImpl上加上注解即可:
@DS("gd_data")
@Service
public class AccountAnalysisServiceImpl extends ServiceImpl<AccountAnalysisMapper, AccountAnalysis> implements IAccountAnalysisService {
}
@DS("gd_common_caiji")
@Service
public class AllFacebookUsersServiceImpl extends ServiceImpl<AllFacebookUsersMapper, AllFacebookUsers> implements IAllFacebookUsersService {
}