H__D  

一、使用

  参考:代码生成器(新) | MyBatis-Plus (baomidou.com)

  参考:代码生成器配置新 | MyBatis-Plus (baomidou.com)

  1、准备一个数据库,本例使用的是mysql数据库,建一张员工表(员工表),并且插入一条数据,sql如下:

 1 CREATE TABLE `t_employee` (
 2   `id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
 3   `last_name` varchar(50) DEFAULT NULL COMMENT '姓名',
 4   `email` varchar(50) DEFAULT NULL COMMENT '邮箱',
 5   `age` int(11) DEFAULT NULL COMMENT '年龄',
 6     `brithday` DATE DEFAULT NULL COMMENT '生日',
 7     `salary`  DECIMAL(16,6) DEFAULT '0' COMMENT '薪资',
 8   `create_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP  COMMENT '创建时间',
 9   `create_by` VARCHAR(128) DEFAULT ''  COMMENT '创建者',
10     `update_time` TIMESTAMP DEFAULT  CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP  COMMENT '更新时间',
11   `update_by` VARCHAR(128) DEFAULT ''  COMMENT '更新者',
12   PRIMARY KEY (`id`)
13 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='员工表';
14 
15 INSERT INTO `t_employee` (`id`, `last_name`, `email`, `age`, `brithday`, `salary`, `create_time`, `create_by`, `update_time`, `update_by`) VALUES (1, '小明', 'xx@xx.com', 16, '2024-01-21', 2000.210000, '2024-01-21 15:54:32', 'sys', '2024-01-21 15:54:32', 'sys');

  2、新建一个SpringBoot工程,引入mybatis-plus依赖以及连接mysql数据库依赖

 1 <!-- mybatis-plus-generator,逆向生成代码 -->
 2 <dependency>
 3     <groupId>com.baomidou</groupId>
 4     <artifactId>mybatis-plus-generator</artifactId>
 5     <version>3.5.2</version>
 6 </dependency>
 7 <!-- SpringBoot中包含freemarker,逆向生成代码模板 -->
 8 <dependency>
 9     <groupId>org.freemarker</groupId>
10     <artifactId>freemarker</artifactId>
11 </dependency>
12 <!-- mysql -->
13 <dependency>
14     <groupId>mysql</groupId>
15     <artifactId>mysql-connector-java</artifactId>
16 </dependency>

  3、测试类

 1 package com.test.mybatisplus;
 2 
 3 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 4 import com.baomidou.mybatisplus.generator.FastAutoGenerator;
 5 import com.baomidou.mybatisplus.generator.config.OutputFile;
 6 import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
 7 import com.sun.org.apache.xpath.internal.operations.Plus;
 8 
 9 import java.util.Collections;
10 
11 /**
12  * @author chengheng
13  * @description TODO
14  * @date 2022/7/8
15  */
16 public class Generator {
17 
18     public static void main(String[] args) {
19         FastAutoGenerator.create("jdbc:mysql://127.0.01:3306/test_mybatis_plus?allowPublicKeyRetrieval=true&useSSL=false",
20                         "root", "123456")
21                 .globalConfig(builder -> {
22                     builder.author("baomidou") // 设置作者
23 //                            .enableSwagger() // 开启 swagger 模式
24                             .fileOverride() // 覆盖已生成文件
25                             .outputDir("C:\\\\Users\\\\H__D\\\\Desktop"); // 指定输出目录
26                 })
27                 .packageConfig(builder -> {
28                     builder.parent("com") // 设置父包名
29                             .moduleName("system") // 设置父包模块名
30                             .pathInfo(Collections.singletonMap(OutputFile.xml, "C:\\\\Users\\\\H__D\\\\Desktop")); // 设置mapperXml生成路径
31                 })
32                 .strategyConfig(builder -> {
33                     builder.addInclude("t_employee") // 设置需要生成的表名
34 //                            .addTablePrefix("t_", "c_"); // 设置过滤表前缀
35 
36                             .entityBuilder()  // 实体策略配置
37 //                            .enableColumnConstant()  // 开启生成字段常量    默认值:false, 生成:public static final String LAST_NAME = "last_name";
38 //                            .enableChainModel() // 开启链式模型    默认值:false 生成:@Accessors(chain = true)
39 //                            .enableFileOverride() // 覆盖已生成文件    默认值:false
40                             .enableLombok() // 开启 lombok 模型    默认值:false
41 //                            .enableTableFieldAnnotation()    // 开启生成实体时生成字段注解    默认值:false, 生成:@TableField("last_name")
42                             .mapperBuilder() // mapper 策略配置
43                             .superClass(BaseMapper.class) // 开启 @Mapper 注解    默认值:false
44                             .enableMapperAnnotation() // 启用 BaseResultMap 生成    默认值:false
45                             .enableBaseResultMap() // 启用 BaseColumnList    默认值:false
46                             .enableBaseColumnList()
47                     ;
48 
49                 })
50 
51                 .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
52                 .execute();
53     }
54 
55 
56 }

二、运行测试

  1、运行效果如下:

15:55:02.141 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================准备生成文件...==========================
15:55:02.452 [main] DEBUG com.baomidou.mybatisplus.generator.config.querys.MySqlQuery - 执行SQL:show table status WHERE 1=1 AND NAME IN ('t_employee')
15:55:02.490 [main] DEBUG com.baomidou.mybatisplus.generator.config.querys.MySqlQuery - 返回记录数:1,耗时(ms):36
15:55:02.518 [main] DEBUG com.baomidou.mybatisplus.generator.config.querys.MySqlQuery - 执行SQL:show full fields from `t_employee`
15:55:02.538 [main] DEBUG com.baomidou.mybatisplus.generator.config.querys.MySqlQuery - 返回记录数:10,耗时(ms):19
15:55:02.699 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================文件生成完成!!!==========================

  2、生成TEmployeeController

 1 package com.system.controller;
 2 
 3 import org.springframework.web.bind.annotation.RequestMapping;
 4 import org.springframework.stereotype.Controller;
 5 
 6 /**
 7  * <p>
 8  * 员工表 前端控制器
 9  * </p>
10  *
11  * @author baomidou
12  * @since 2024-01-21
13  */
14 @Controller
15 @RequestMapping("/system/tEmployee")
16 public class TEmployeeController {
17 
18 }

  3、生成 ITEmployeeService 

 1 package com.system.service;
 2 
 3 import com.system.entity.TEmployee;
 4 import com.baomidou.mybatisplus.extension.service.IService;
 5 
 6 /**
 7  * <p>
 8  * 员工表 服务类
 9  * </p>
10  *
11  * @author baomidou
12  * @since 2024-01-21
13  */
14 public interface ITEmployeeService extends IService<TEmployee> {
15 
16 }

  4、生成TEmployeeServiceImpl 

 1 package com.system.service.impl;
 2 
 3 import com.system.entity.TEmployee;
 4 import com.system.mapper.TEmployeeMapper;
 5 import com.system.service.ITEmployeeService;
 6 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 7 import org.springframework.stereotype.Service;
 8 
 9 /**
10  * <p>
11  * 员工表 服务实现类
12  * </p>
13  *
14  * @author baomidou
15  * @since 2024-01-21
16  */
17 @Service
18 public class TEmployeeServiceImpl extends ServiceImpl<TEmployeeMapper, TEmployee> implements ITEmployeeService {
19 
20 }

  5、生成TEmployeeMapper 

 1 package com.system.mapper;
 2 
 3 import com.system.entity.TEmployee;
 4 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 5 import org.apache.ibatis.annotations.Mapper;
 6 
 7 /**
 8  * <p>
 9  * 员工表 Mapper 接口
10  * </p>
11  *
12  * @author baomidou
13  * @since 2024-01-21
14  */
15 @Mapper
16 public interface TEmployeeMapper extends BaseMapper<TEmployee> {
17 
18 }

  6、生成  TEmployee 

 1 package com.system.entity;
 2 
 3 import com.baomidou.mybatisplus.annotation.IdType;
 4 import com.baomidou.mybatisplus.annotation.TableId;
 5 import com.baomidou.mybatisplus.annotation.TableName;
 6 import java.io.Serializable;
 7 import java.math.BigDecimal;
 8 import java.time.LocalDate;
 9 import java.time.LocalDateTime;
10 import lombok.Getter;
11 import lombok.Setter;
12 
13 /**
14  * <p>
15  * 员工表
16  * </p>
17  *
18  * @author baomidou
19  * @since 2024-01-21
20  */
21 @Getter
22 @Setter
23 @TableName("t_employee")
24 public class TEmployee implements Serializable {
25 
26     private static final long serialVersionUID = 1L;
27 
28     /**
29      * ID
30      */
31     @TableId(value = "id", type = IdType.AUTO)
32     private Long id;
33 
34     /**
35      * 姓名
36      */
37     private String lastName;
38 
39     /**
40      * 邮箱
41      */
42     private String email;
43 
44     /**
45      * 年龄
46      */
47     private Integer age;
48 
49     /**
50      * 生日
51      */
52     private LocalDate brithday;
53 
54     /**
55      * 薪资
56      */
57     private BigDecimal salary;
58 
59     /**
60      * 创建时间
61      */
62     private LocalDateTime createTime;
63 
64     /**
65      * 创建者
66      */
67     private String createBy;
68 
69     /**
70      * 更新时间
71      */
72     private LocalDateTime updateTime;
73 
74     /**
75      * 更新者
76      */
77     private String updateBy;
78 
79 
80 }

  7、生成 XML 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 <mapper namespace="com.system.mapper.TEmployeeMapper">
 4 
 5     <!-- 通用查询映射结果 -->
 6     <resultMap id="BaseResultMap" type="com.system.entity.TEmployee">
 7         <id column="id" property="id" />
 8         <result column="last_name" property="lastName" />
 9         <result column="email" property="email" />
10         <result column="age" property="age" />
11         <result column="brithday" property="brithday" />
12         <result column="salary" property="salary" />
13         <result column="create_time" property="createTime" />
14         <result column="create_by" property="createBy" />
15         <result column="update_time" property="updateTime" />
16         <result column="update_by" property="updateBy" />
17     </resultMap>
18 
19     <!-- 通用查询结果列 -->
20     <sql id="Base_Column_List">
21         id, last_name, email, age, brithday, salary, create_time, create_by, update_time, update_by
22     </sql>
23 
24 </mapper>

 

posted on 2024-01-19 00:16  H__D  阅读(245)  评论(0编辑  收藏  举报