chen888的gravatar头像
chen888 2017-08-23 15:57:58

spring MVC+mybatis代码自动生成器

这个代码生成器是我从Mybatis-Plus项目代码中整理出来的,整理出来的只支持mysql数据库。

  采用代码生成器可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用(比 Mybatis 官方的 Generator 更加强大!)

生成的目录结构图:

spring MVC+mybatis代码自动生成器spring MVC+mybatis代码自动生成器

主要代码:

/**
     * <p>
     * MySQL 生成演示
     * </p>
     */
    public static void main(String[] args) {
        // 自定义需要填充的字段
        List<TableFill> tableFillList = new ArrayList<>();
        tableFillList.add(new TableFill("ASDD_SS", FieldFill.INSERT_UPDATE));

        // 代码生成器
        AutoGenerator mpg = new AutoGenerator().setGlobalConfig(
                // 全局配置
                new GlobalConfig()
                        .setOutputDir("/develop/code/")//输出目录
                        .setFileOverride(true)// 是否覆盖文件
                        .setActiveRecord(true)// 开启 activeRecord 模式
                        .setEnableCache(false)// XML 二级缓存
                        .setBaseResultMap(true)// XML ResultMap
                        .setBaseColumnList(true)// XML columList
                        .setAuthor("chenguoji")
                // 自定义文件命名,注意 %s 会自动填充表实体属性!
                // .setMapperName("%sDao")
                // .setXmlName("%sDao")
                // .setServiceName("MP%sService")
                // .setServiceImplName("%sServiceDiy")
                // .setControllerName("%sAction")
        ).setDataSource(
                // 数据源配置
                new DataSourceConfig()
                        .setDbType(DbType.MYSQL)// 数据库类型
                        .setTypeConvert(new MySqlTypeConvert() {
                            // 自定义数据库表字段类型转换【可选】
                            @Override
                            public DbColumnType processTypeConvert(String fieldType) {
                                System.out.println("转换类型:" + fieldType);
                                // if ( fieldType.toLowerCase().contains( "tinyint" ) ) {
                                //    return DbColumnType.BOOLEAN;
                                // }
                                return super.processTypeConvert(fieldType);
                            }
                        })
                        .setDriverName("com.mysql.jdbc.Driver")
                        .setUsername("root")
                        .setPassword("newpasswd")
                        .setUrl("jdbc:mysql://127.0.0.1:3306/boke?characterEncoding=utf8")
        ).setStrategy(
                // 策略配置
                new StrategyConfig()
                        // .setCapitalMode(true)// 全局大写命名
                        // .setDbColumnUnderline(true)//全局下划线命名
                        .setTablePrefix(new String[]{"sys_", "tb_"})// 此处可以修改为您的表前缀
                        .setNaming(NamingStrategy.underline_to_camel)// 表名生成策略
                        // .setInclude(new String[] { "user" }) // 需要生成的表
                        // .setExclude(new String[]{"test"}) // 排除生成的表
                        // 自定义实体父类
                        // .setSuperEntityClass("com.baomidou.demo.TestEntity")
                        // 自定义实体,公共字段
                        .setSuperEntityColumns(new String[]{"test_id"})
                        .setTableFillList(tableFillList)
                // 自定义 mapper 父类
                // .setSuperMapperClass("com.baomidou.demo.TestMapper")
                // 自定义 service 父类
                // .setSuperServiceClass("com.baomidou.demo.TestService")
                // 自定义 service 实现类父类
                // .setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl")
                // 自定义 controller 父类
                // .setSuperControllerClass("com.baomidou.demo.TestController")
                // 【实体】是否生成字段常量(默认 false)
                // public static final String ID = "test_id";
                // .setEntityColumnConstant(true)
                // 【实体】是否为构建者模型(默认 false)
                // public User setName(String name) {this.name = name; return this;}
                // .setEntityBuilderModel(true)
                // 【实体】是否为lombok模型(默认 false)<a href="https://projectlombok.org/">document</a>
                // .setEntityLombokModel(true)
                // Boolean类型字段是否移除is前缀处理
                // .setEntityBooleanColumnRemoveIsPrefix(true)
                // .setRestControllerStyle(true)
                // .setControllerMappingHyphenStyle(true)
        ).setPackageInfo(
                // 包配置
                new PackageConfig()
                        .setModuleName("test")
                        .setParent("com.chen")// 自定义包路径
                        .setController("controller")// 这里是控制器包名,默认 web
        ).setCfg(
                // 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值
                new InjectionConfig() {
                    @Override
                    public void initMap() {
                        Map<String, Object> map = new HashMap<>();
                        map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
                        this.setMap(map);
                    }
                }.setFileOutConfigList(Collections.<FileOutConfig>singletonList(new FileOutConfig("/templates/mapper.xml.vm") {
                    // 自定义输出文件目录
                    @Override
                    public String outputFile(TableInfo tableInfo) {
                        return "/develop/code/xml/" + tableInfo.getEntityName() + ".xml";
                    }
                }))
        ).setTemplate(
                // 关闭默认 xml 生成,调整生成 至 根目录
                new TemplateConfig().setXml(null)
                // 自定义模板配置,模板可以参考源码 /mybatis-plus/src/main/resources/template 使用 copy
                // 至您项目 src/main/resources/template 目录下,模板名称也可自定义如下配置:
                // .setController("...");
                // .setEntity("...");
                // .setMapper("...");
                // .setXml("...");
                // .setService("...");
                // .setServiceImpl("...");
        );

        // 执行生成
        mpg.execute();

        // 打印注入设置,这里演示模板里面怎么获取注入内容【可无】
        System.err.println(mpg.getCfg().getMap().get("abc"));
    }

 

项目结构图:

spring MVC+mybatis代码自动生成器

运行截图:

spring MVC+mybatis代码自动生成器

生成后xml和实体;

spring MVC+mybatis代码自动生成器spring MVC+mybatis代码自动生成器

 

 

是不是很方便,项目开展前期简单的基础工作,直接可以使用这个工具生成呢。

如果还不满足你的需求可以自己自定义配置


打赏

已有2人打赏

已注销用户的gravatar头像 最代码官方的gravatar头像

文件名:springBoot.zip,文件大小:1657.013K 下载
最代码最近下载分享源代码列表最近下载
sunlea  LV17 2023年2月23日
wubinbin  LV11 2021年12月18日
yin出门买了吗了  LV9 2021年11月1日
xmz5351  LV1 2021年9月15日
hjc810794  LV8 2020年4月24日
710223431  LV2 2020年3月9日
houliukou  LV37 2020年2月14日
gaochanghong  LV14 2019年11月14日
0312wangchen  LV26 2019年9月20日
一把手  LV1 2019年4月17日
最代码最近浏览分享源代码列表最近浏览
zolscy  LV12 昨天
hyx666110  LV2 3月2日
3320151533  LV1 1月7日
WBelong  LV7 2023年12月28日
1529860026  LV24 2023年6月1日
tianyuboy111  LV3 2023年5月20日
guviva  LV6 2023年5月1日
李亮  LV19 2023年3月6日
sunlea  LV17 2023年2月23日
dengjunjun  LV15 2023年1月5日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友