mybatis plus 代碼生成器
阿新 • • 發佈:2018-05-25
clas enc myba plus line public conf police als
<!-- 模板引擎 --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>最新版本</version> </dependency> <!-- 模板引擎,需要指定 mpg.setTemplateEngine(new FreemarkerTemplateEngine()); --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>最新版本</version> </dependency> <!-- MP 核心庫 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>最新版本</version> </dependency>
package com.management.manage; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.config.DataSourceConfig; import com.baomidou.mybatisplus.generator.config.GlobalConfig; import com.baomidou.mybatisplus.generator.config.PackageConfig;import com.baomidou.mybatisplus.generator.config.StrategyConfig; import com.baomidou.mybatisplus.generator.config.rules.DbType; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import com.management.manage.converts.OracleTypeConvert; import org.junit.Test; /** * <p>f * 測試生成代碼 * </p> * * @author K神 * @date 2017/12/18 */ public class GeneratorServiceEntity { @Test public void generateCode() { String packageName = "com.baomidou.springboot"; boolean serviceNameStartWithI = false;//user -> UserService, 設置成true: user -> IUserService generateByTables(serviceNameStartWithI, packageName, "POLICE_QA_QBANK"); } private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) { GlobalConfig config = new GlobalConfig(); String dbUrl = "jdbc:oracle:thin:@101.37.149.47:11521:orcl"; DataSourceConfig dataSourceConfig = new DataSourceConfig(); dataSourceConfig.setDbType(DbType.ORACLE) .setUrl(dbUrl) .setUsername("js_gongan") .setPassword("HLKJ#jsga2018") .setTypeConvert(new OracleTypeConvert())//自定義數據庫類型轉換 .setDriverName("oracle.jdbc.driver.OracleDriver"); StrategyConfig strategyConfig = new StrategyConfig(); strategyConfig .setCapitalMode(true) .setEntityLombokModel(false) .setDbColumnUnderline(false) .setNaming(NamingStrategy.underline_to_camel) .setRestControllerStyle(true) .entityTableFieldAnnotationEnable(true)//是否生成實體時,生成字段註解 .setInclude(tableNames);//修改替換成你需要的表名,多個表名傳數組 config.setActiveRecord(false) .setAuthor("趙炎") .setOutputDir("E:\\codeGen") .setFileOverride(true); if (!serviceNameStartWithI) { config.setServiceName("%sService"); } new AutoGenerator().setGlobalConfig(config) .setDataSource(dataSourceConfig) .setStrategy(strategyConfig) .setPackageInfo( new PackageConfig() .setParent(packageName) .setController("controller") .setEntity("entity") ).execute(); } private void generateByTables(String packageName, String... tableNames) { generateByTables(true, packageName, tableNames); } }
package com.management.manage.converts; import com.baomidou.mybatisplus.generator.config.ITypeConvert; import com.baomidou.mybatisplus.generator.config.rules.DbColumnType; //@Component public class OracleTypeConvert implements ITypeConvert { @Override public DbColumnType processTypeConvert(String s) { String t = s.toUpperCase(); if (t.contains("CHAR")) { return DbColumnType.STRING; } else if (t.contains("DATE") || t.contains("TIMESTAMP")) { return DbColumnType.DATE; } else if (t.contains("NUMBER")) { if (t.matches("NUMBER\\(+\\d\\)")) { return DbColumnType.INTEGER; } else if (t.matches("NUMBER\\(+\\d{2}+\\)")) { return DbColumnType.LONG; } return DbColumnType.INTEGER; } else if (t.contains("FLOAT")) { return DbColumnType.FLOAT; } else if (t.contains("clob")) { return DbColumnType.CLOB; } else if (t.contains("BLOB")) { return DbColumnType.OBJECT; } else if (t.contains("binary")) { return DbColumnType.BYTE_ARRAY; } else if (t.contains("RAW")) { return DbColumnType.BYTE_ARRAY; } return DbColumnType.STRING; } }
package com.management.manage.common.handler; import com.baomidou.mybatisplus.mapper.MetaObjectHandler; import org.apache.ibatis.reflection.MetaObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Date; import java.util.Objects; /** * 註入公共字段自動填充,任選註入方式即可 */ //@Component public class MyMetaObjectHandler extends MetaObjectHandler { protected final static Logger logger = LoggerFactory.getLogger(MyMetaObjectHandler.class); @Override public void insertFill(MetaObject metaObject) { Object isDelete = getFieldValByName("isdelete", metaObject); if(Objects.isNull(isDelete)){ setFieldValByName("isdelete",0, metaObject);//mybatis-plus版本2.0.9+ } } @Override public void updateFill(MetaObject metaObject) { logger.info("更新的時候幹點不可描述的事情"); setFieldValByName("updatetime", new Date(), metaObject); } }
mybatis plus 代碼生成器