频道栏目
首页 > 资讯 > 其他综合 > 正文

Mybatis代码生成器(Mybatis-plus)

17-08-16        来源:[db:作者]  
收藏   我要投稿

Mybatis Generator代码生成器可以对数据库逆向生成对应的entity、dao、xml。本文将使用网上开源的mybatis-plus代码生成器直接生成含有service、controller层的代码工具。

mybatis-plus 开源网址:http://mp.baomidou.com/#/

在使用代码生成工具之前,需要配置resources文件下面的相关配置。

一:generatorConfig.xml文件

二:template 模板文件

(ps:相关文件可以参考mybatis-plus开源项目)

使用 代码生成方式如下,需要mysql-connector-java包,根据自己情况配置好生成方式即可

package trade.axht;  
import com.baomidou.mybatisplus.generator.AutoGenerator;  
import com.baomidou.mybatisplus.generator.InjectionConfig;  
import com.baomidou.mybatisplus.generator.config.*;  
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;  
import com.baomidou.mybatisplus.generator.config.po.TableInfo;  
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;  
import com.baomidou.mybatisplus.generator.config.rules.DbType;  
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;  
  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
  
/** 
 * <p> 
 * 代码生成器演示 
 * </p> 
 * 
 */  
public class MpGenerator {  
  
    /** 
     * <p> 
     * MySQL 生成演示 
     * </p> 
     */  
    public static void main(String[] args) {  
        AutoGenerator mpg = new AutoGenerator();  
  
        // 全局配置  
        GlobalConfig gc = new GlobalConfig();  
        gc.setOutputDir("E://freemarker//src//main//java");  
        gc.setFileOverride(true);  
        gc.setActiveRecord(true);  
        gc.setEnableCache(false);// XML 二级缓存  
        gc.setBaseResultMap(true);// XML ResultMap  
        gc.setBaseColumnList(false);// XML columList  
        gc.setAuthor("Brozer");  
  
        // 自定义文件命名,注意 %s 会自动填充表实体属性!  
        // gc.setMapperName("%sDao");  
        // gc.setXmlName("%sDao");  
        // gc.setServiceName("MP%sService");  
        // gc.setServiceImplName("%sServiceDiy");  
        // gc.setControllerName("%sAction");  
        mpg.setGlobalConfig(gc);  
  
        // 数据源配置  
        DataSourceConfig dsc = new DataSourceConfig();  
        dsc.setDbType(DbType.MYSQL);  
        dsc.setTypeConvert(new MySqlTypeConvert(){  
            // 自定义数据库表字段类型转换【可选】  
            @Override  
            public DbColumnType processTypeConvert(String fieldType) {  
                System.out.println("转换类型:" + fieldType);  
                return super.processTypeConvert(fieldType);  
            }  
        });  
        dsc.setDriverName("com.mysql.jdbc.Driver");  
        dsc.setUsername("root");  
        dsc.setPassword("brozer");  
        dsc.setUrl("jdbc:mysql://127.0.0.1:3306/renren-security?characterEncoding=utf8");  
        mpg.setDataSource(dsc);  
  
        // 策略配置  
        StrategyConfig strategy = new StrategyConfig();  
        strategy.setTablePrefix(new String[]{"plus"});// 此处可以修改为您的表前缀  
  
        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略  
        // strategy.setInclude(new String[] { "user" }); // 需要生成的表  
        strategy.setExclude(new String[]{"hibernate_sequence","quartz_%","quartz_*"}); // 排除生成的表  
        // 字段名生成策略  
        strategy.setFieldNaming(NamingStrategy.underline_to_camel);  
        // 自定义实体父类  
        // strategy.setSuperEntityClass("com.baomidou.demo.TestEntity");  
        // 自定义实体,公共字段  
        // strategy.setSuperEntityColumns(new String[] { "test_id", "age" });  
        // 自定义 mapper 父类  
        // strategy.setSuperMapperClass("com.baomidou.demo.TestMapper");  
        // 自定义 service 父类  
        // strategy.setSuperServiceClass("com.baomidou.demo.TestService");  
        // 自定义 service 实现类父类  
        // strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl");  
        // 自定义 controller 父类  
        // strategy.setSuperControllerClass("com.baomidou.demo.TestController");  
        // 【实体】是否生成字段常量(默认 false)  
        // public static final String ID = "test_id";  
        // strategy.setEntityColumnConstant(true);  
        // 【实体】是否为构建者模型(默认 false)  
        // public User setName(String name) {this.name = name; return this;}  
        // strategy.setEntityBuliderModel(true);  
        mpg.setStrategy(strategy);  
  
        // 包配置  
        PackageConfig pc = new PackageConfig();  
        pc.setParent("trade.axht.shiro");  
       /* pc.setModuleName("freemarker");*/  
        mpg.setPackageInfo(pc);  
  
        // 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值  
        InjectionConfig cfg = new InjectionConfig() {  
            @Override  
            public void initMap() {  
                Map<String, Object> map = new HashMap<String, Object>();  
                map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");  
                this.setMap(map);  
            }  
        };  
        // 自定义 xxList.jsp 生成  
  
        List<FileOutConfig> focList = new ArrayList<FileOutConfig>();  
        focList.add(new FileOutConfig("/template/list.html.vm") {  
            @Override  
            public String outputFile(TableInfo tableInfo) {  
                // 自定义输入文件名称  
                return "E://freemarker//src//main//webapp//" + tableInfo.getEntityName() + ".jsp";  
            }  
        });  
        cfg.setFileOutConfigList(focList);  
        mpg.setCfg(cfg);  
  
        // 自定义模板配置,可以 copy 源码 mybatis-plus/src/main/resources/template 下面内容修改,  
        // 放置自己项目的 src/main/resources/template 目录下, 默认名称一下可以不配置,也可以自定义模板名称  
        TemplateConfig tc = new TemplateConfig();  
        tc.setController("/template/controller.java.vm");  
        tc.setEntity("/template/entity.java.vm");  
        tc.setMapper("/template/mapper.java.vm");  
        tc.setXml("/template/mapper.xml.vm");  
        tc.setService("/template/service.java.vm");  
        tc.setServiceImpl("/template/serviceImpl.java.vm");  
        mpg.setTemplate(tc);  
  
        // 执行生成  
        mpg.execute();  
  
        // 打印注入设置  
        System.err.println(mpg.getCfg().getMap().get("abc"));  
    }  
  
}  
相关TAG标签
上一篇:sql语句快速清空表
下一篇:HTTPS:编程开发
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站