使用旧的代码生成器

依赖:

<dependencies>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-test</artifactId>        <scope>test</scope>    </dependency>        <dependency>        <groupId>mysql</groupId>        <artifactId>mysql-connector-java</artifactId>        <version>8.0.32</version>    </dependency>        <dependency>        <groupId>org.projectlombok</groupId>        <artifactId>lombok</artifactId>    </dependency>    <dependency>        <groupId>com.baomidou</groupId>        <artifactId>mybatis-plus-boot-starter</artifactId>        <!--3.4.3-->        <version>3.0.3</version>    </dependency>    <dependency>        <groupId>org.apache.velocity</groupId>        <artifactId>velocity-engine-core</artifactId>        <version>2.0</version>    </dependency>        <dependency>        <groupId>io.springfox</groupId>        <artifactId>springfox-swagger2</artifactId>        <version>2.10.5</version>    </dependency>        <dependency>        <groupId>io.springfox</groupId>        <artifactId>springfox-swagger-ui</artifactId>        <version>2.10.5</version>    </dependency></dependencies>

代码:

public class CodeGenerator  {    public static void main(String[] args) {        // 需要构建一个代码自动生成器对象        AutoGenerator mpg = new AutoGenerator();        // 配置策略        // 1、全局配置        GlobalConfig gc = new GlobalConfig();        String projectPath = System.getProperty("user.dir");        gc.setOutputDir(projectPath + "/src/main/java");        gc.setAuthor("你的名字");        gc.setOpen(false);        gc.setFileOverride(false);        // 去掉Service的I前缀        gc.setServiceName("%sService");        gc.setIdType(IdType.ID_WORKER);        gc.setDateType(DateType.ONLY_DATE);        gc.setSwagger2(true);        mpg.setGlobalConfig(gc);        // 2、设置数据源        DataSourceConfig dsc = new DataSourceConfig();        dsc.setUrl("jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");        dsc.setDriverName("com.mysql.cj.jdbc.Driver");        dsc.setUsername("root");        dsc.setPassword("root");        dsc.setDbType(DbType.MYSQL);        mpg.setDataSource(dsc);        // 3、包的配置        PackageConfig pc = new PackageConfig();        pc.setModuleName("blog");        pc.setParent("cn.com.dwr");        pc.setEntity("pojo");        pc.setMapper("mapper");        pc.setService("service");        pc.setController("controller");        mpg.setPackageInfo(pc);        // 4、策略配置        StrategyConfig strategy = new StrategyConfig();        // 设置要映射的表名        strategy.setInclude("student");        strategy.setNaming(NamingStrategy.underline_to_camel);        strategy.setColumnNaming(NamingStrategy.underline_to_camel);        // 自动lombok        strategy.setEntityLombokModel(true);        strategy.setLogicDeleteFieldName("deleted");        // 自动填充配置        TableFill createTime = new TableFill("create_time", FieldFill.INSERT);        TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);        List<TableFill> tableFillList = new ArrayList<>();        tableFillList.add(createTime);        tableFillList.add(updateTime);        // 乐观锁        strategy.setVersionFieldName("version");        strategy.setRestControllerStyle(true);        strategy.setControllerMappingHyphenStyle(true);        mpg.setStrategy(strategy);        // 执行        mpg.execute();    }}

生成效果:

数据库:

Student类:

@Data@EqualsAndHashCode(callSuper = false)@Accessors(chain = true)@ApiModel(value="Student对象", description="")public class Student implements Serializable {    private static final long serialVersionUID = 1L;    @ApiModelProperty(value = "id")    @TableId(value = "id", type = IdType.ID_WORKER)    private Integer id;    @ApiModelProperty(value = "姓名")    private String name;    @ApiModelProperty(value = "年龄")    private Integer age;    @ApiModelProperty(value = "性别")    private Integer sex;    @ApiModelProperty(value = "邮箱")    private String email;    @ApiModelProperty(value = "版本")    @Version    private Integer version;    @ApiModelProperty(value = "逻辑删除")    @TableLogic    private Integer deleted;    @ApiModelProperty(value = "创建时间")    @TableField("createTime")    private Date createTime;    @ApiModelProperty(value = "更新时间")    @TableField("updateTime")    private Date updateTime;}

StudentMapper类:

public interface StudentMapper extends BaseMapper<Student> {}

StudentMapper.xml:

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="cn.com.dwr.blog.mapper.StudentMapper"></mapper>

StudentService类:

public interface StudentService extends IService<Student> {}

StudentServiceImpl类:

@Servicepublic class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {}

Controller类:

@RestController@RequestMapping("/blog/student")public class StudentController {}

需要时直接复制代码,修改数据源和要生成的表名即可。