当前位置:   article > 正文

IDEA 集成Mybatis-Generator 代码自动生成器_idea mybatis代码生成器

idea mybatis代码生成器

开发环境说明

编辑器:IDEA

项目:Spring Boot项目

第一步:先创建一个SprinngBoot项目:

第二步:Spring Boot项目在插件中添加MyBatis-generator 插件相关依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>2.2.2.RELEASE</version>
  10. </parent>
  11. <groupId>org.zzg</groupId>
  12. <artifactId>SmartHouse</artifactId>
  13. <version>1.0-SNAPSHOT</version>
  14. <properties>
  15. <java.version>1.8</java.version>
  16. <maven.compiler.source>8</maven.compiler.source>
  17. <maven.compiler.target>8</maven.compiler.target>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-test</artifactId>
  27. <scope>test</scope>
  28. <exclusions>
  29. <exclusion>
  30. <groupId>org.junit.vintage</groupId>
  31. <artifactId>junit-vintage-engine</artifactId>
  32. </exclusion>
  33. </exclusions>
  34. </dependency>
  35. <dependency>
  36. <groupId>junit</groupId>
  37. <artifactId>junit</artifactId>
  38. <version>RELEASE</version>
  39. <scope>compile</scope>
  40. </dependency>
  41. <!--springboot 与 mybatis 集成 -->
  42. <dependency>
  43. <groupId>org.mybatis.spring.boot</groupId>
  44. <artifactId>mybatis-spring-boot-starter</artifactId>
  45. <version>1.3.2</version>
  46. </dependency>
  47. <!-- 数据库连接池druid -->
  48. <dependency>
  49. <groupId>com.alibaba</groupId>
  50. <artifactId>druid-spring-boot-starter</artifactId>
  51. <version>1.1.10</version>
  52. </dependency>
  53. <!--mysql 驱动程序 -->
  54. <dependency>
  55. <groupId>mysql</groupId>
  56. <artifactId>mysql-connector-java</artifactId>
  57. <version>8.0.12</version>
  58. </dependency>
  59. </dependencies>
  60. <build>
  61. <plugins>
  62. <plugin>
  63. <groupId>org.springframework.boot</groupId>
  64. <artifactId>spring-boot-maven-plugin</artifactId>
  65. <version>2.6.6</version>
  66. </plugin>
  67. <plugin>
  68. <groupId>org.mybatis.generator</groupId>
  69. <artifactId>mybatis-generator-maven-plugin</artifactId>
  70. <version>1.3.5</version>
  71. <configuration>
  72. <configurationFile>src/main/resources/mybatis-generator/mybatis-generator-cfg.xml
  73. </configurationFile>
  74. <verbose>true</verbose>
  75. <overwrite>true</overwrite>
  76. </configuration>
  77. <executions>
  78. </executions>
  79. <dependencies>
  80. <dependency>
  81. <groupId>org.mybatis.generator</groupId>
  82. <artifactId>mybatis-generator-core</artifactId>
  83. <version>1.3.5</version>
  84. </dependency>
  85. <!--mysql 驱动程序 -->
  86. <dependency>
  87. <groupId>mysql</groupId>
  88. <artifactId>mysql-connector-java</artifactId>
  89. <version>8.0.12</version>
  90. </dependency>
  91. </dependencies>
  92. </plugin>
  93. </plugins>
  94. </build>
  95. </project>

 第三步:添加MyBatis-Generator 代码生成器依赖的(mybatis-generator-cfg.xml)

在Spring Boot 项目的resource目录下新增mybatis-generator 文件夹,新增mybatis-generator-cfg.xml文件。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE 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. <!--defaultModelType="flat" 大数据字段,不分表 -->
  7. <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
  8. <property name="autoDelimitKeywords" value="true"/>
  9. <property name="beginningDelimiter" value="`"/>
  10. <property name="endingDelimiter" value="`"/>
  11. <property name="javaFileEncoding" value="utf-8"/>
  12. <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
  13. <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
  14. <!-- 注释 -->
  15. <commentGenerator>
  16. <property name="suppressAllComments" value="true"/><!-- 是否取消注释 -->
  17. <property name="suppressDate" value="true"/> <!-- 是否生成注释代时间戳-->
  18. </commentGenerator>
  19. <!--数据库链接地址账号密码-->
  20. <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
  21. connectionURL="jdbc:mysql://127.0.0.1:3306/house?serverTimezone=UTC"
  22. userId="root"
  23. password="123456">
  24. </jdbcConnection>
  25. <!-- 类型转换 -->
  26. <javaTypeResolver>
  27. <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
  28. <property name="forceBigDecimals" value="false"/>
  29. </javaTypeResolver>
  30. <!--生成Model类存放位置-->
  31. <javaModelGenerator targetPackage="com.zzg.model" targetProject="src/main/java">
  32. <property name="enableSubPackages" value="true"/>
  33. <property name="trimStrings" value="true"/>
  34. </javaModelGenerator>
  35. <!-- 生成mapxml文件 -->
  36. <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
  37. <property name="enableSubPackages" value="false"/>
  38. </sqlMapGenerator>
  39. <!-- 生成mapxml对应client,也就是接口dao -->
  40. <javaClientGenerator targetPackage="com.zzg.mapper" targetProject="src/main/java" type="XMLMAPPER">
  41. <property name="enableSubPackages" value="false"/>
  42. </javaClientGenerator>
  43. <table tableName="house_user" domainObjectName="User" enableCountByExample="false"
  44. enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
  45. selectByExampleQueryId="false">
  46. </table>
  47. </context>
  48. </generatorConfiguration>

 第四步:添加MyBatis-Generator 代码生成器的Maven指令:

 

核心指令码: 

mybatis-generator:generate -e

  第五步:基于MyBatis-Generator 代码指令生成model\mapper和xml 文件

 效果展示:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/307521
推荐阅读
相关标签