文/朱季谦
1.环境:SpringBoot
2.在pom.xml文件里引入相关依赖:
1 <plugin> 2 <groupId>org.mybatis.generator</groupId> 3 <artifactId>mybatis-generator-maven-plugin</artifactId> 4 <version>1.3.6</version> 5 <configuration> 6 <verbose>true</verbose> 7 <overwrite>true</overwrite> 8 </configuration> 9 <dependencies> 10 <dependency> 11 <groupId>mysql</groupId> 12 <artifactId>mysql-connector-java</artifactId> 13 <version>5.1.36</version> 14 </dependency> 15 </dependencies> 16 17 </plugin> 18 19 20 21 <dependency> 22 <groupId>org.mybatis.generator</groupId> 23 <artifactId>mybatis-generator-core</artifactId> 24 <version>1.3.6</version> 25 </dependency>
3.创建generatorConfig.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <!DOCTYPE generatorConfiguration 4 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 5 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 6 7 <generatorConfiguration> 8 <context id="testTables" targetRuntime="MyBatis3"> 9 <commentGenerator> 10 <!-- 是否去除自动生成的注释 true:是 : false:否 --> 11 <property name="suppressAllComments" value="true" /> 12 </commentGenerator> 13 <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> 14 <jdbcConnection driverClass="com.mysql.jdbc.Driver" 15 connectionURL="jdbc:mysql://127.0.0.1:3306/example?useUnicode=true&characterEncoding=utf8" 16 userId="root" 17 password="123456"> 18 </jdbcConnection> 19 20 <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 21 和 NUMERIC 类型解析为java.math.BigDecimal --> 22 <javaTypeResolver> 23 <property name="forceBigDecimals" value="false" /> 24 </javaTypeResolver> 25 26 <!-- targetProject:生成PO类的位置 --> 27 <javaModelGenerator targetPackage="com.zhu.text.domain" 28 targetProject=".srcmainjava"> 29 <!-- enableSubPackages:是否让schema作为包的后缀 --> 30 <property name="enableSubPackages" value="false" /> 31 <!-- 从数据库返回的值被清理前后的空格 --> 32 <property name="trimStrings" value="true" /> 33 </javaModelGenerator> 34 <!-- targetProject:mapper映射文件生成的位置 --> 35 <sqlMapGenerator targetPackage="com.zhu.text.dao" 36 targetProject=".srcmainjava"> 37 <!-- enableSubPackages:是否让schema作为包的后缀 --> 38 <property name="enableSubPackages" value="false" /> 39 </sqlMapGenerator> 40 <!-- targetPackage:mapper接口生成的位置 --> 41 <javaClientGenerator type="XMLMAPPER" 42 targetPackage="com.zhu.text.dao" targetProject=".srcmainjava"> 43 <!-- enableSubPackages:是否让schema作为包的后缀 --> 44 <property name="enableSubPackages" value="false" /> 45 </javaClientGenerator> 46 <!-- 指定数据库表 --> 47 <table tableName="text_product" domainObjectName="Product"></table> 48 </context> 49 </generatorConfiguration>
4.创建main类
1 import java.io.File; 2 import java.util.ArrayList; 3 import java.util.List; 4 import org.mybatis.generator.api.MyBatisGenerator; 5 import org.mybatis.generator.config.Configuration; 6 import org.mybatis.generator.config.xml.ConfigurationParser; 7 import org.mybatis.generator.internal.DefaultShellCallback; 8 9 public class MBGenerator { 10 public static void main(String[] args) throws Exception{ 11 12 List<String> warnings = new ArrayList<String>(); 13 boolean overwrite = true; 14 File configFile = new File("例:generatorConfig.xml的存放路径"); 15 ConfigurationParser cp = new ConfigurationParser(warnings); 16 Configuration config = cp.parseConfiguration(configFile); 17 DefaultShellCallback callback = new DefaultShellCallback(overwrite); 18 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); 19 myBatisGenerator.generate(null); 20 } 21 }
5.执行maven方法里的mybatis-generator:generate
完成以上步骤,就可以自定生成需要的xml文件与类以及对应的java bean类。