MyBatis逆向生成!

在开发项目中,适当的引用插件确实能提高工作效率,尤其是单人开发时候;

MybatisGenerator插件能节省单表操作的时间,能自动生成增删改查sql以及对应的dao(有的项目中叫mapper层);

SpringBoot开发时候引用MybatisGenerator插件步骤如下:

1.在pom.xml标签<build>中插入标签<plugin>,如下:


    
    
  1. <!--mg-->
  2. <plugin>
  3. <groupId>org.mybatis.generator </groupId>
  4. <artifactId>mybatis-generator-maven-plugin </artifactId>
  5. <version>1.3.6 </version>
  6. <dependencies>
  7. <dependency>
  8. <groupId> mysql </groupId>
  9. <artifactId> mysql-connector-java </artifactId>
  10. <version> 5.1.46 </version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.mybatis.generator </groupId>
  14. <artifactId>mybatis-generator-core </artifactId>
  15. <version>1.3.6 </version>
  16. </dependency>
  17. </dependencies>
  18. <executions>
  19. <execution>
  20. <id>Generate MyBatis Artifacts </id>
  21. <phase>package </phase>
  22. <goals>
  23. <goal>generate </goal>
  24. </goals>
  25. </execution>
  26. </executions>
  27. <configuration>
  28. <!--允许移动生成的文件 -->
  29. <verbose>true </verbose>
  30. <!-- 是否覆盖 -->
  31. <overwrite>true </overwrite>
  32. <!-- 自动生成的配置 -->
  33. <configurationFile>
  34. src/main/resources/mybatis-generator.xml </configurationFile>
  35. </configuration>
  36. </plugin>

保存后,maven会自动引入jar包;并且在Maven Project视图中,plugins目录中出现mybatis-generator目录,如下图:


2.导入配置文件,如pom.xml中标签<configurationFile>中配置路径,文件内容最好从官网下载,在模块XML Configuration ReferenceXML中把右侧的拷贝下来修改就行,如下图:


本人配置如下:


    
    
  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. <!--数据库链接URL,用户名、密码 -->
  7. <context id="MySQL" targetRuntime="MyBatis3">
  8. <commentGenerator>
  9. <property name="suppressDate" value="true"/>
  10. <!-- 是否去除自动生成的注释 true:是 : false:否 -->
  11. <property name="suppressAllComments" value="true"/>
  12. </commentGenerator>
  13. <jdbcConnection driverClass="com.mysql.jdbc.Driver"
  14. connectionURL= "jdbc:mysql://127.0.0.1/linda"
  15. userId= "root"
  16. password= "admin">
  17. </jdbcConnection>
  18. <!--是否启用java.math.BigDecimal-->
  19. <javaTypeResolver >
  20. <property name="forceBigDecimals" value="false" />
  21. </javaTypeResolver>
  22. <javaModelGenerator targetPackage="com.linda.modules.model" targetProject="D:\idea-projects\linda\src\main\java">
  23. <property name="enableSubPackages" value="true" />
  24. <property name="trimStrings" value="true" />
  25. </javaModelGenerator>
  26. <sqlMapGenerator targetPackage="sqlmappings" targetProject="D:\idea-projects\linda\src\main\resources">
  27. <property name="enableSubPackages" value="true" />
  28. </sqlMapGenerator>
  29. <javaClientGenerator type="XMLMAPPER" targetPackage="com.linda.modules.mapper" targetProject="D:\idea-projects\linda\src\main\java">
  30. <property name="enableSubPackages" value="true" />
  31. </javaClientGenerator>
  32. <table tableName="brand" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
  33. </table>
  34. <!-- <table tableName="persons" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
  35. </table>
  36. <table tableName="orders" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
  37. </table>-->
  38. </context>
  39. </generatorConfiguration>

3.运行maven命令,生成对应的文件;在maven试图中,双击mybatis-generator:generate,会自动生成,如果报错,根据控制台报错信息修改文件;如下图:





在开发项目中,适当的引用插件确实能提高工作效率,尤其是单人开发时候;

MybatisGenerator插件能节省单表操作的时间,能自动生成增删改查sql以及对应的dao(有的项目中叫mapper层);

SpringBoot开发时候引用MybatisGenerator插件步骤如下:

1.在pom.xml标签<build>中插入标签<plugin>,如下:


  
  
  1. <!--mg-->
  2. <plugin>
  3. <groupId>org.mybatis.generator </groupId>
  4. <artifactId>mybatis-generator-maven-plugin </artifactId>
  5. <version>1.3.6 </version>
  6. <dependencies>
  7. <dependency>
  8. <groupId> mysql </groupId>
  9. <artifactId> mysql-connector-java </artifactId>
  10. <version> 5.1.46 </version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.mybatis.generator </groupId>
  14. <artifactId>mybatis-generator-core </artifactId>
  15. <version>1.3.6 </version>
  16. </dependency>
  17. </dependencies>
  18. <executions>
  19. <execution>
  20. <id>Generate MyBatis Artifacts </id>
  21. <phase>package </phase>
  22. <goals>
  23. <goal>generate </goal>
  24. </goals>
  25. </execution>
  26. </executions>
  27. <configuration>
  28. <!--允许移动生成的文件 -->
  29. <verbose>true </verbose>
  30. <!-- 是否覆盖 -->
  31. <overwrite>true </overwrite>
  32. <!-- 自动生成的配置 -->
  33. <configurationFile>
  34. src/main/resources/mybatis-generator.xml </configurationFile>
  35. </configuration>
  36. </plugin>

保存后,maven会自动引入jar包;并且在Maven Project视图中,plugins目录中出现mybatis-generator目录,如下图:


2.导入配置文件,如pom.xml中标签<configurationFile>中配置路径,文件内容最好从官网下载,在模块XML Configuration ReferenceXML中把右侧的拷贝下来修改就行,如下图:


本人配置如下:


  
  
  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. <!--数据库链接URL,用户名、密码 -->
  7. <context id="MySQL" targetRuntime="MyBatis3">
  8. <commentGenerator>
  9. <property name="suppressDate" value="true"/>
  10. <!-- 是否去除自动生成的注释 true:是 : false:否 -->
  11. <property name="suppressAllComments" value="true"/>
  12. </commentGenerator>
  13. <jdbcConnection driverClass="com.mysql.jdbc.Driver"
  14. connectionURL= "jdbc:mysql://127.0.0.1/linda"
  15. userId= "root"
  16. password= "admin">
  17. </jdbcConnection>
  18. <!--是否启用java.math.BigDecimal-->
  19. <javaTypeResolver >
  20. <property name="forceBigDecimals" value="false" />
  21. </javaTypeResolver>
  22. <javaModelGenerator targetPackage="com.linda.modules.model" targetProject="D:\idea-projects\linda\src\main\java">
  23. <property name="enableSubPackages" value="true" />
  24. <property name="trimStrings" value="true" />
  25. </javaModelGenerator>
  26. <sqlMapGenerator targetPackage="sqlmappings" targetProject="D:\idea-projects\linda\src\main\resources">
  27. <property name="enableSubPackages" value="true" />
  28. </sqlMapGenerator>
  29. <javaClientGenerator type="XMLMAPPER" targetPackage="com.linda.modules.mapper" targetProject="D:\idea-projects\linda\src\main\java">
  30. <property name="enableSubPackages" value="true" />
  31. </javaClientGenerator>
  32. <table tableName="brand" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
  33. </table>
  34. <!-- <table tableName="persons" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
  35. </table>
  36. <table tableName="orders" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
  37. </table>-->
  38. </context>
  39. </generatorConfiguration>

3.运行maven命令,生成对应的文件;在maven试图中,双击mybatis-generator:generate,会自动生成,如果报错,根据控制台报错信息修改文件;如下图:





猜你喜欢

转载自blog.csdn.net/weixin_43710551/article/details/88713049