java 集成 Mybatis-plus

现在比较流行的代码生成器使我们最基础的crud 不在需要写了,以下是详情实现代码

@MapperScan("") //设置你的mapper 扫描包
@EnableTransactionManagement
@Configuration // 配置类
public class MybatisConfig {

    public class MyBatisPlusConfig {
        // 注册乐观锁插件
        @Bean
        public OptimisticLockerInterceptor optimisticLockerInterceptor() {
            return new OptimisticLockerInterceptor();
        }
    }

}

当我们有了config之后就可以编写我们的业务层代码,首先我们得构建一个代码生成器对象

     // 需要构建一个 代码自动生成器 对象
        AutoGenerator mpg = new AutoGenerator();

        // 设置其详细属性
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("");
        gc.setOpen(false);
        gc.setFileOverride(false); // 是否覆盖
        gc.setServiceName("%sService"); // 去Service的I前缀
        gc.setIdType(IdType.ID_WORKER);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setSwagger2(true);
        mpg.setGlobalConfig(gc);

接着配置数据源

 /*数据源*/
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("");
       //mysql 高版本之后 用的cj.jbdc.Driver 低版本不管

   dsc.setDriverName("com.mysql.cj.jdbc.Driver"); 
        dsc.setUsername(" ");
        dsc.setPassword(" ");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

设置包的配置 需要把代码生成在哪儿的模块,用什么package 来装

 //包的配置
        PackageConfig pc = new PackageConfig();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的模块名字");
        String nextName = scanner.next();
        pc.setModuleName(nextName);
        pc.setParent("com.my.custom");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        mpg.setPackageInfo(pc);

配置策略

 //4、策略配置
        StrategyConfig strategy = new StrategyConfig();
        System.out.println("请输入你的表名");
        String next = scanner.next();

        strategy.setInclude(next);

        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true); // 自动lombok;
        strategy.setLogicDeleteFieldName("deleted");

配置自动填充 和加乐观锁

        TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT);
        TableFill gmtModified = new TableFill("gmt_modified",
                FieldFill.INSERT_UPDATE);

        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(gmtCreate);
        tableFills.add(gmtModified);
        strategy.setTableFillList(tableFills);
        // 乐观锁
        strategy.setVersionFieldName("version");
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true);
        mpg.setStrategy(strategy);

自定义配置

     // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        //使用默认的velocity模板引擎
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute(); //执行

如果有模板的话 在执行之前降模板set进去

       //绑定自定义模板
        TemplateConfig tc = new TemplateConfig();
        tc.setController("/templates/myController.java");
        tc.setService("/templates/myService.java");
        tc.setServiceImpl("/templates/myServiceImpl.java");
        tc.setMapper("/templates/myMapper.java");
        tc.setEntity("/templates/myEntity.java");
        tc.setXml("/templates/myMapper.xml");
        mpg.setTemplate(tc);

以上这些完了 就已经可以完美生成crud了 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值