文/朱季谦
1.环境:SpringBoot
2.在pom.xml文件里引入相关依赖:
<plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.6</version><configuration><verbose>true</verbose><overwrite>true</overwrite></configuration><dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.36</version></dependency></dependencies></plugin><dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.3.6</version></dependency>
3.创建generatorConfig.xml文件
<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEgeneratorConfigurationPUBLIC"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration><contextid="testTables"targetRuntime="MyBatis3"><commentGenerator><!--是否去除自动生成的注释true:是:false:否--><propertyname="suppressAllComments"value="true"/></commentGenerator><!--数据库连接的信息:驱动类、连接地址、用户名、密码--><jdbcConnectiondriverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://127.0.0.1:3306/example?useUnicode=true&characterEncoding=utf8"userId="root"password="123456"></jdbcConnection><!--默认false,把JDBCDECIMAL和NUMERIC类型解析为Integer,为true时把JDBCDECIMAL和NUMERIC类型解析为java.math.BigDecimal--><javaTypeResolver><propertyname="forceBigDecimals"value="false"/></javaTypeResolver><!--targetProject:生成PO类的位置--><javaModelGeneratortargetPackage="com.zhu.text.domain"targetProject=".\src\main\java"><!--enableSubPackages:是否让schema作为包的后缀--><propertyname="enableSubPackages"value="false"/><!--从数据库返回的值被清理前后的空格--><propertyname="trimStrings"value="true"/></javaModelGenerator><!--targetProject:mapper映射文件生成的位置--><sqlMapGeneratortargetPackage="com.zhu.text.dao"targetProject=".\src\main\java"><!--enableSubPackages:是否让schema作为包的后缀--><propertyname="enableSubPackages"value="false"/></sqlMapGenerator><!--targetPackage:mapper接口生成的位置--><javaClientGeneratortype="XMLMAPPER"targetPackage="com.zhu.text.dao"targetProject=".\src\main\java"><!--enableSubPackages:是否让schema作为包的后缀--><propertyname="enableSubPackages"value="false"/></javaClientGenerator><!--指定数据库表--><tabletableName="text_product"domainObjectName="Product"></table></context></generatorConfiguration>
4.创建main类
importjava.io.File; importjava.util.ArrayList; importjava.util.List; importorg.mybatis.generator.api.MyBatisGenerator; importorg.mybatis.generator.config.Configuration; importorg.mybatis.generator.config.xml.ConfigurationParser; importorg.mybatis.generator.internal.DefaultShellCallback; publicclassMBGenerator { publicstaticvoidmain(String[] args) throwsException{ List<String>warnings=newArrayList<String>(); booleanoverwrite=true; FileconfigFile=newFile("例:generatorConfig.xml的存放路径"); ConfigurationParsercp=newConfigurationParser(warnings); Configurationconfig=cp.parseConfiguration(configFile); DefaultShellCallbackcallback=newDefaultShellCallback(overwrite); MyBatisGeneratormyBatisGenerator=newMyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } }
5.执行maven方法里的mybatis-generator:generate
完成以上步骤,就可以自定生成需要的xml文件与类以及对应的java bean类。