springboot用mybatis-generator自动生成mapper和model

时间:2022-07-11 17:03:33

转:http://blog.csdn.net/u011493599/article/details/53928379

1、在pom.xml里添加maven插件

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

2、在resources下创建mybatis-generator.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. <context id="DB2Tables"    targetRuntime="MyBatis3">
  7. <commentGenerator>
  8. <property name="suppressDate" value="true"/>
  9. <property name="suppressAllComments" value="true"/>
  10. </commentGenerator>
  11. <!--数据库链接地址账号密码-->
  12. <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.3.155:3308/nonobank_app" userId="stb_nono_app" password="csstbnonobank@2016">
  13. </jdbcConnection>
  14. <javaTypeResolver>
  15. <property name="forceBigDecimals" value="false"/>
  16. </javaTypeResolver>
  17. <!--生成Model类存放位置-->
  18. <javaModelGenerator targetPackage="com.demo.model" targetProject="src/main/java">
  19. <property name="enableSubPackages" value="true"/>
  20. <property name="trimStrings" value="true"/>
  21. </javaModelGenerator>
  22. <!--生成映射文件存放位置-->
  23. <sqlMapGenerator targetPackage="com.demo.mapper" targetProject="src/main/java">
  24. <property name="enableSubPackages" value="true"/>
  25. </sqlMapGenerator>
  26. <!--生成Dao类存放位置-->
  27. <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
  28. type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
  29. type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
  30. type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
  31. -->
  32. <javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="com.demo.dao" targetProject="src/main/java">
  33. <property name="enableSubPackages" value="true"/>
  34. </javaClientGenerator>
  35. <!--生成对应表及类名-->
  36. <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  37. </context>
  38. </generatorConfiguration>

3、在src/main/java/com/demo/下创建dao、model、mapper文件夹。

4、在maven project下双击mybatis-generator插件下的mybatis-generator:generate

springboot用mybatis-generator自动生成mapper和model

springboot用mybatis-generator自动生成mapper和model