Mybatis-Plus(簡稱MP)是一個 Mybatis 的加強工具,在 Mybatis 的基礎上只作加強不作改變,爲簡化開發、提升效率而生。html
<!-- mysql驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- mybatis-plus 代碼自動生成--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.1</version> </dependency>
# 鏈接數據庫 spring.datasource.username=*** spring.datasource.password=*** spring.datasource.url=jdbc:mysql://localhost:3306/***?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # 配置日誌 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl mybatis-plus.global-config.db-config.logic-delete-value=1 mybatis-plus.global-config.db-config.logic-not-delete-value=0
@Configuration // 配置類 @EnableTransactionManagement @MapperScan("com.***.mapper") public class MybatisPlusConfig { // 註冊分頁 @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } }
public class CodeGenerator { /** * <p> * 讀取控制檯內容 * </p> */ public static String scanner(String tip) { Scanner scanner = new Scanner(System.in); StringBuilder help = new StringBuilder(); help.append("請輸入" + tip + ":"); System.out.println(help.toString()); if (scanner.hasNext()) { String ipt = scanner.next(); if (StringUtils.isNotEmpty(ipt)) { return ipt; } } throw new MybatisPlusException("請輸入正確的" + tip + "!"); } public static void main(String[] args) { // 須要構建一個 代碼自動生成器 對象 AutoGenerator mpg = new AutoGenerator(); // 配置策略 // 一、全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/src/main/java"); gc.setAuthor("DMFlySky"); 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("jdbc:mysql://localhost:3306/***?useUnicode=true&useSSL=false&allowPublicKeyRetrieval=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("***"); dsc.setPassword("***"); dsc.setDbType(DbType.MYSQL); mpg.setDataSource(dsc); //三、包的配置 PackageConfig pc = new PackageConfig(); pc.setParent("com.***"); pc.setEntity("entity"); pc.setMapper("mapper"); pc.setService("service"); pc.setController("controller"); mpg.setPackageInfo(pc); //四、策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setInclude("***", "***", "***");// 設置要映射的表名 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); ArrayList<TableFill> tableFills = new ArrayList<>(); tableFills.add(gmtCreate); strategy.setTableFillList(tableFills); strategy.setRestControllerStyle(true); strategy.setControllerMappingHyphenStyle(true); mpg.setStrategy(strategy); mpg.execute(); //執行 } }