Mybatis |
您所在的位置:网站首页 › mybatisplus指定数据源 › Mybatis |
AutoGenerator
AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。 也可以自定义生成模板,生成更丰富的后台代码,简化开发。 默认风格案例 (1)引入依赖MyBatis-Plus 从 3.0.3 之后移除了代码生成器与模板引擎的默认依赖,需要手动添加相关依赖: 添加 代码生成器 依赖,3.5.0不兼容之前的版本。。。 com.baomidou mybatis-plus-generator 3.5.0 添加 模板引擎 依赖,MyBatis-Plus 支持 Velocity(默认)、Freemarker、Beetl,用户可以选择自己熟悉的模板引擎,如果都不满足您的要求,可以采用自定义模板引擎。 org.apache.velocity velocity-engine-core 2.3 (2)编写配置详细配置说明参考官网 配置项列表: 配置项说明数据源配置DataSourceConfig:指定需要生成代码的具体数据库数据库表配置StrategyConfig:指定需要生成哪些表或者排除哪些表包名配置PackageConfig:指定生成代码的包路径模板配置TemplateConfig:可自定义代码生成的模板,实现个性化操作全局策略配置GlobalConfig注入配置InjectionConfig:可注入自定义参数等操作以实现个性化操作 public class Generator { /** ** 读取控制台内容 * */ public static String scanner(String tip) { Scanner scanner = new Scanner(System.in); System.out.println("请输入" + tip + ":"); if (scanner.hasNext()) { String ipt = scanner.next(); if (StringUtils.isNotBlank(ipt)) { return ipt; } } throw new MybatisPlusException("请输入正确的" + tip + "!"); } public static void main(String[] args) { // 全局策略配置 GlobalConfig globalConfig = GeneratorBuilder.globalConfigBuilder() // 覆盖已生成文件 .fileOverride() // 生成后是否打开生成目录 .openDir(true) // 指定输出目录 默认值: windows:D:// linux or mac : /tmp .outputDir("D:\\output") // 生成swagger注解 .enableSwagger() // 作者名 .author("pearl") // 时间策略 .dateType(DateType.TIME_PACK) // 注释日期格式 .commentDate("yyyy-MM-dd") .build(); // 数据源配置 DataSourceConfig dataSourceConfig = new DataSourceConfig // 驱动连接的URL、数据库连接用户名、数据库连接密码 .Builder("jdbc:mysql://127.0.0.1:3306/angel_admin?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai", "root", "123456") // 类型转换,数据库=》JAVA类型 .typeConvert(new MySqlTypeConvert()) // 关键字处理 ,这里选取了mysql5.7文档中的关键字和保留字(含移除) .keyWordsHandler(new MySqlKeyWordsHandler()) // 数据库信息查询类,默认由 dbType 类型决定选择对应数据库内置实现 .dbQuery(new MySqlQuery()) // 数据库 schema name .schema("mybatis-plus") .build(); // 包配置 PackageConfig packageConfig = new PackageConfig.Builder() // 父包名。如果为空,将下面子包名必须写全部, 否则就只需写子包名 .parent(scanner("父包名")) // 父包模块名 .moduleName(scanner("模块名")) .build(); // 配置模板 //TemplateConfig templateConfig = new TemplateConfig.Builder().disable().build();//激活所有默认模板 // 添加以上配置到AutoGenerator中 AutoGenerator autoGenerator = new AutoGenerator(dataSourceConfig); // 数据源配置 autoGenerator.global(globalConfig); // 全局策略配置 autoGenerator.packageInfo(packageConfig); // 包配置 //autoGenerator.template(templateConfig); // 配置模板 // 生成代码 autoGenerator.execute(); } } (3)测试 启动main线程,输入![]() ![]() 使用默认模块可以生成代码,但是某些情况下,默认模板可能不满足当前需求,需要自定义模板,下面已 替换velocity引擎为freemarker org.freemarker freemarker 2.3.31复制源码中的模板到当前项目,只复制.ftl结尾的即可(freemarker模板)。 生成类添加模板引擎配置 // 配置模板 TemplateConfig templateConfig = new TemplateConfig.Builder() //指定自定义模板路径, 位置:/resources/templates/entity2.java.ftl(或者是.vm) //注意不要带上.ftl(或者是.vm), 会根据使用的模板引擎自动识别 .build(); // 添加以上配置到AutoGenerator中 AutoGenerator autoGenerator = new AutoGenerator(dataSourceConfig); // 数据源配置 autoGenerator.global(globalConfig); // 全局策略配置 autoGenerator.packageInfo(packageConfig); // 包配置 autoGenerator.template(templateConfig); // 配置模板 // 指定引擎 生成代码 autoGenerator.execute(new FreemarkerTemplateEngine()); 根据需求,修改模板,比如在controller添加注入对应的service层,并添加根据ID 查询实体类的接口 @Autowired ${table.serviceName} ${table.serviceName?uncap_first}; @GetMapping("/get${entity}") public Rentity}(Long id){ return R.ok(${table.serviceName?uncap_first}.getById(id)); }![]() |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |