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

 




[java] view plain copy

 


  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

 

 




[java] view plain copy

 


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


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

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

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

 

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