• MyBatis-Plus代码自动生成


    前言

    这是一段使用 MyBatis-Plus 自动生成代码的示例代码。MyBatis-Plus 是一个优秀的 ORM 框架,它在 MyBatis 的基础上进行了扩展和增强,提供了更加便捷、高效的数据库操作方式。在实际开发中,我们通常需要编写大量的 CRUD 操作代码,使用 MyBatis-Plus 可以帮助我们快速生成这些代码,提高开发效率。

    一、添加依赖

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-data-redisartifactId>
    4. dependency>
    5. <dependency>
    6. <groupId>com.baomidougroupId>
    7. <artifactId>mybatis-plus-boot-starterartifactId>
    8. <version>3.4.2version>
    9. dependency>
    10. <dependency>
    11. <groupId>com.baomidougroupId>
    12. <artifactId>mybatis-plus-generatorartifactId>
    13. <version>3.5.3version>
    14. dependency>
    15. <dependency>
    16. <groupId>org.apache.velocitygroupId>
    17. <artifactId>velocity-engine-coreartifactId>
    18. <version>2.3version>
    19. dependency>
    20. <dependency>
    21. <groupId>org.freemarkergroupId>
    22. <artifactId>freemarkerartifactId>
    23. dependency>
    24. <dependency>
    25. <groupId>mysqlgroupId>
    26. <artifactId>mysql-connector-javaartifactId>
    27. <version>8.0.33version>
    28. dependency>
    29. <dependency>
    30. <groupId>org.projectlombokgroupId>
    31. <artifactId>lombokartifactId>
    32. dependency>

    二 、application.properties

    1. # Redis 配置
    2. spring.redis.host=192.168.67.34
    3. spring.redis.password=yyl
    4. spring.redis.database=5
    5. # 数据库配置
    6. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    7. spring.datasource.url=jdbc:mysql:///qq?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
    8. spring.datasource.username=root
    9. spring.datasource.password=123456789
    10. # Jackson 配置
    11. spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
    12. spring.jackson.time-zone=GMT+8
    13. spring.jackson.serialization.write-date-keys-as-timestamps=false
    14. # MyBatis-Plus 配置
    15. mybatis-plus.configuration.map-underscore-to-camel-case=true # 是否开启下划线命名转驼峰命名的映射规则
    16. mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl # 将日志输出到控制台
    17. # mybatis-plus.configuration.log-impl=
    18. mybatis-plus.mapper-locations=classpath:/mapper/*.xml
    19. # MyBatis-Plus 全局配置
    20. mybatis-plus.global-config.db-config.logic-not-delete-value=0
    21. mybatis-plus.global-config.db-config.logic-delete-value=1

    三、在测试类创建MyTest

    我这里是使用的springboot

    1. package com.aaa;
    2. import com.baomidou.mybatisplus.annotation.FieldFill;
    3. import com.baomidou.mybatisplus.generator.FastAutoGenerator;
    4. import com.baomidou.mybatisplus.generator.config.OutputFile;
    5. import com.baomidou.mybatisplus.generator.fill.Column;
    6. import java.util.Arrays;
    7. import java.util.Collections;
    8. import java.util.List;
    9. public class MyTest {
    10. public static void main(String[] args) {
    11. // 数据库连接
    12. FastAutoGenerator.create("jdbc:mysql:///qq","root","123456789")
    13. // 全局配置
    14. .globalConfig((scanner, builder) -> builder
    15. .author("小宇")
    16. .outputDir("F:\\redisSpringboot\\02\\src\\main\\java")
    17. )
    18. // 包配置
    19. .packageConfig(
    20. (scanner, builder) ->
    21. builder
    22. .parent("com.aaa") // 存放位置
    23. .pathInfo(Collections.singletonMap(OutputFile.xml, "F:\\redisSpringboot\\02\\src\\main\\resources\\mapper")))
    24. // 策略配置
    25. .strategyConfig((scanner, builder) -> builder.addInclude(getTables(scanner.apply("请输入表名,多个英文逗号分隔?所有输入 all")))
    26. .controllerBuilder().enableRestStyle().enableHyphenStyle()
    27. .entityBuilder().enableLombok().addTableFills(
    28. new Column("create_time", FieldFill.INSERT)
    29. ).build())
    30. /*
    31. 模板引擎配置,默认 Velocity 可选模板引擎 Beetl 或 Freemarker
    32. .templateEngine(new BeetlTemplateEngine())
    33. .templateEngine(new FreemarkerTemplateEngine())
    34. */
    35. .execute();
    36. // 处理 all 情况
    37. }
    38. protected static List getTables(String tables) {
    39. return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    40. }
    41. }

  • 相关阅读:
    keycloak~AbstractJsonUserAttributeMapper的作用
    Java中17个提高开发效率的小工具代码
    年复一年,为什么打破数据孤岛还是企业发展的首要任务
    Unity 3D 基础——Coroutine 协同程序
    debian10 arm芯片安装.net6
    【PCB学习】几种接地符号
    【App自动化测试】(十四)Android WebView测试方法
    [附源码]java毕业设计镐京学院教务管理系统
    el-table动态新增行及校验规则
    WEB代码审计
  • 原文地址:https://blog.csdn.net/weixin_54546701/article/details/134063575