• MyBatis Generator - 快速生成 实体类 和 映射文件


    目录

    一、MyBatis Generator 的使用

    1.1、生成类和映射文件

    1.1.1、在 pom.xml 中引入依赖

    1.1.2、根据 configurationFile 标签中配置的路径 创建 generatorConfig.xml 文件

    1.1.3、自动生成类 和 映射文件

    1.1.4、在 Insert 标签中添加获取主键值的选项

    1.1.5、扫描配置:添加 @Mapper 注解 / 添加扫描注解

    1.1.6、配置 mybatis

    1.1.7、测试


    一、MyBatis Generator 的使用


    1.1、生成类和映射文件

    1.1.1、在 pom.xml 中引入依赖

    在 properties 标签中加入版本号.

    <mybatis-generator-plugin-version>1.4.1mybatis-generator-plugin-version>

    在 build => plugins 标签中加入如下配置

    1. <plugin>
    2. <groupId>org.mybatis.generatorgroupId>
    3. <artifactId>mybatis-generator-maven-pluginartifactId>
    4. <version>${mybatis-generator-plugin-version}version>
    5. <executions>
    6. <execution>
    7. <id>Generate MyBatis Artifactsid>
    8. <phase>deployphase>
    9. <goals>
    10. <goal>generategoal>
    11. goals>
    12. execution>
    13. executions>
    14. <configuration>
    15. <verbose>trueverbose>
    16. <overwrite>trueoverwrite>
    17. <configurationFile>
    18. src/main/resources/mybatis/generatorConfig.xml
    19. configurationFile>
    20. configuration>
    21. plugin>

    上述配置中需要注意的是 “配置文件路径”,这个路径就是用来生成 实体类和映射文件 配置规则的位置.

    1.1.2、根据 configurationFile 标签中配置的路径 创建 generatorConfig.xml 文件

    这个文件就是用来描述生成规则的.

    根据路径(src/main/resources/mybatis),在 mybatis 目录下创建 generatorConfig.xml 文件.

    Ps:下述配置文件中需要修改的有 数据库连接、实体类和映射文件的路径、数据库表名

    1. "1.0" encoding="UTF-8"?>
    2. generatorConfiguration
    3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
    5. <generatorConfiguration>
    6. <classPathEntry location="D:\class\source\mysql-connector-java-5.1.49.jar"/>
    7. <context id="DB2Tables" targetRuntime="MyBatis3">
    8. <commentGenerator>
    9. <property name="suppressAllComments" value="true"/>
    10. <property name="suppressDate" value="true"/>
    11. commentGenerator>
    12. <jdbcConnection driverClass="com.mysql.jdbc.Driver"
    13. connectionURL="jdbc:mysql://127.0.0.1:3306/javanav_db?
    14. characterEncoding=utf8&useSSL=false"
    15. userId="root"
    16. password="1111">
    17. jdbcConnection>
    18. <javaTypeResolver>
    19. <property name="forceBigDecimals" value="false"/>
    20. javaTypeResolver>
    21. <javaModelGenerator targetPackage="com.example.cyk.model"
    22. targetProject="src/main/java">
    23. <property name="enableSubPackages" value="true"/>
    24. <property name="trimStrings" value="true"/>
    25. javaModelGenerator>
    26. <sqlMapGenerator targetPackage="mapper"
    27. targetProject="src/main/resources">
    28. <property name="enableSubPackages" value="true"/>
    29. sqlMapGenerator>
    30. <javaClientGenerator type="XMLMAPPER"
    31. targetPackage="com.example.cyk.mapper" targetProject="src/main/java">
    32. <property name="enableSubPackages" value="true"/>
    33. javaClientGenerator>
    34. <table tableName="j_article" domainObjectName="Article"
    35. enableSelectByExample="false"
    36. enableDeleteByExample="false" enableDeleteByPrimaryKey="false"
    37. enableCountByExample="false"
    38. enableUpdateByExample="false">
    39. <property name="useActualColumnNames" value="true"/>
    40. table>
    41. <table tableName="j_article_reply" domainObjectName="ArticleReply"
    42. enableSelectByExample="false"
    43. enableDeleteByExample="false" enableDeleteByPrimaryKey="false"
    44. enableCountByExample="false"
    45. enableUpdateByExample="false">
    46. <property name="useActualColumnNames" value="true"/>
    47. table>
    48. <table tableName="j_board" domainObjectName="Board"
    49. enableSelectByExample="false" enableDeleteByExample="false"
    50. enableDeleteByPrimaryKey="false" enableCountByExample="false"
    51. enableUpdateByExample="false">
    52. <property name="useActualColumnNames" value="true"/>
    53. table>
    54. <table tableName="j_message" domainObjectName="Message"
    55. enableSelectByExample="false"
    56. enableDeleteByExample="false" enableDeleteByPrimaryKey="false"
    57. enableCountByExample="false"
    58. enableUpdateByExample="false">
    59. <property name="useActualColumnNames" value="true"/>
    60. table>
    61. <table tableName="j_user" domainObjectName="User"
    62. enableSelectByExample="false" enableDeleteByExample="false"
    63. enableDeleteByPrimaryKey="false" enableCountByExample="false"
    64. enableUpdateByExample="false">
    65. <property name="useActualColumnNames" value="true"/>
    66. table>
    67. context>
    68. generatorConfiguration>

    注意:

    驱动包路径是自己本地仓库的路径

     

    但一定注意!! 需要在非中文的目录下,因此你可以把这个驱动包拷贝出来,放到一个非中文的目录中即可.

    1.1.3、自动生成类 和 映射文件

    重新加载Maven项⽬,在Plugins节点下出现mybatis-generator,双击运⾏,在对应的目录下⽣成相应的类与映射⽂件:

    接着你就可以看到对应的生成了

    1.1.4、在 Insert 标签中添加获取主键值的选项

    在生成的 xml 文件中,给每一个 insert 标签都添加以下属性:useGeneratedKeys="true" keyProperty="id"

    1. <insert id="insert" parameterType="com.example.cyk.model.User"
    2. useGeneratedKeys="true" keyProperty="id" >

    Ps:这个选项也可以自动生成,但是不理想(有些问题)

    1.1.5、扫描配置:添加 @Mapper 注解 / 添加扫描注解

    有两种方式配置扫描 Mapper 接口.

    1)给每个 mapper 包下的 mapper 接口都添加 @Mapper 注解.

    2)给启动类上 或者 新建一个配置类(有 @Configuration 注解)加上 @MapperScan("com.example.cyk.mapper") 注解.

    1.1.6、配置 mybatis

    在 yml 文件中配置

    1. mybatis:
    2. mapper-locations: classpath:mapper/**/*Mapper.xml

    1.1.7、测试

    1. @SpringBootTest
    2. public class TestMapper {
    3. @Autowired
    4. private UserMapper userMapper;
    5. @Test
    6. public void select() {
    7. User user = userMapper.selectByPrimaryKey(1L);
    8. System.out.println(user);
    9. }
    10. }


     

  • 相关阅读:
    java 大厂面试指南:性能优化 + 微服务 + 并发编程 + 开源框架 + 分布式
    orbslam2实验记录-----稠密建图
    Windows平台下私有云盘搭建
    JavaScript 面向对象的基本用法
    【外设篇】——显示器
    低代码是开发的未来吗?浅谈低代码平台
    为啥国内大厂都把云计算当成香饽饽,这个万亿市场你真的了解吗
    Leetcode154. 寻找旋转排序数组中的最小值(存在重复元素)
    【算法-回溯法】N皇后问题
    网络安全 — 零信任架构
  • 原文地址:https://blog.csdn.net/CYK_byte/article/details/133974232