首先,新建springboot文件,引入pom文件
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.4.0</version> </dependency>
修改,maven_jar的插件版本
<properties> <java.version>1.8</java.version> <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version> </properties>
第二步,编写java文件
只需要编写test文件,不用主入口文件
package cn.taotao; import java.io.File; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.Test; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.exception.InvalidConfigurationException; import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class MybatisGeneratorApplicationTests { @Test void contextLoads() { } @Test public void MybatisGenerator() { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File("MybatisGenerator.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); try { Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
第三步,编写xml文件,xml文件放到项目根目录,不是类路径。和pom.xml 同级。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 配置数据库连接 --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="xxxxxx"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 指定javaBean生成的位置 --> <javaModelGenerator targetPackage="cn.taotao.bean" targetProject="C:UsersOwnereclipse-workspaceTransactionDemosrcmainjava"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--指定sql映射文件生成的位置 --> <sqlMapGenerator targetPackage="mapper" targetProject="C:UsersOwnereclipse-workspaceTransactionDemosrcmain esources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 指定dao接口生成的位置,mapper接口 --> <javaClientGenerator type="XMLMAPPER" targetPackage="cn.taotao.dao" targetProject="C:UsersOwnereclipse-workspaceTransactionDemosrcmainjava"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- table指定每个表的生成策略 --> <table tableName="tbl_user" domainObjectName="User" enableCountByExample="false" enableDeleteByExample="false" enableUpdateByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="tbl_dept" domainObjectName="Dept" enableCountByExample="false" enableDeleteByExample="false" enableUpdateByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" > <columnOverride column="deptinfo" javaType="java.lang.String" jdbcType="VARCHAR"></columnOverride> <!--text类型的,转为varchar类型--> </table> </context> </generatorConfiguration>