Mybatis-plus-generator代码自动生成工具,自动生成实体类 使用Mybatis-plus-generator可以自动生成 Controller Service Mapper DAO层的基本代码,免去自己去写实体类映射数据库的繁琐操作。
步骤如下 添加依赖
1 2 3 4 5 6 7 8 9 10 11 <dependency > <groupId > com.baomidou</groupId > <artifactId > mybatis-plus-generator</artifactId > <version > 3.4.1</version > </dependency > <dependency > <groupId > org.apache.velocity</groupId > <artifactId > velocity-engine-core</artifactId > <version > 2.0</version > </<dependency > <groupId > com.baomidou</groupId > <artifactId > mybatis-plus-generator</artifactId > <version > 3.4.1</version > </dependency > <dependency > <groupId > org.apache.velocity</groupId > <artifactId > velocity-engine-core</artifactId > <version > 2.0</version > </dependency >
实现代码
按注释修改自己的配置
可将下面代码放到test包运行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 package com.example.myspringboot;import com.baomidou.mybatisplus.annotation.DbType;import com.baomidou.mybatisplus.annotation.IdType;import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;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.DateType;import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;import lombok.extern.slf4j.Slf4j;import org.apache.commons.lang3.StringUtils;import org.springframework.boot.test.context.SpringBootTest;import java.util.Scanner;@Slf4j public class MyBatisPlusGenerator { public static void main (String[] args) { GlobalConfig config = new GlobalConfig (); config.setActiveRecord(true ) .setAuthor("Evan" ) .setOutputDir("C:\\Users\\root\\Desktop\\myspringboot\\src\\main\\java" ) .setFileOverride(true ) .setIdType(IdType.AUTO) .setDateType(DateType.ONLY_DATE) .setServiceName("%Service" ) .setEntityName("" ) .setBaseResultMap(true ) .setActiveRecord(false ) .setBaseColumnList(true ); DataSourceConfig dsConfig = new DataSourceConfig (); dsConfig.setDbType(DbType.MYSQL) .setDriverName("com.mysql.cj.jdbc.Driver" ) .setUrl("jdbc:mysql://127.0.0.1:3306/practice?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC" ) .setUsername("root" ) .setPassword("123456" ); StrategyConfig stConfig = new StrategyConfig (); stConfig.setCapitalMode(true ) .setNaming(NamingStrategy.underline_to_camel) .setEntityLombokModel(true ) .setRestControllerStyle(true ) .setInclude("student" ); PackageConfig pkConfig = new PackageConfig (); pkConfig.setParent("com.example.myspringboot" ) .setMapper("mapper" ) .setService("service" ) .setController("controller" ) .setEntity("pojo" ) .setXml("mapper" ); AutoGenerator ag = new AutoGenerator (); ag.setGlobalConfig(config) .setDataSource(dsConfig) .setStrategy(stConfig) .setPackageInfo(pkConfig); ag.execute(); System.out.println("======= Done 相关代码生成完毕 ========" ); }
点击运行即可