• 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基础之单例设计模式[18]
    MindSpore新型轻量级神经网络GhostNet,在ImageNet分类、图像识别和目标检测等多个应用场景效果优异!
    重生奇迹mu宠物带来不一样的体验
    react-native实现 TextInput 键盘显示搜索按钮并触发回调
    [好题][曼哈顿距离][二分]Taxi 2022杭电多校第3场 1011
    笔记本怎么录屏?这3个方法请你收好
    DAMOYOLO windows 单卡训练
    PySide6精简教程
    第5章 AOP中的代理模式应用
    使用VS2022连接openEuler虚拟机失败
  • 原文地址:https://blog.csdn.net/CYK_byte/article/details/133974232