使用mybatis-generator-core實現(xiàn)逆向工程
-
添加jar包
- lib/mybatis-generator-core-1.3.2.jar
- mybatis-3.4.4.jar
- mysql-connector-java-5.1.44.jar或者mysql-connector-java-8.0.13.jar
-
添加generatorConfig.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> <!-- 數(shù)據(jù)庫驅(qū)動包位置 --> <!-- <classPathEntry location="D:\software\lib\mysql-connector-java-5.1.21.jar" /> --> <!-- <classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" /> --> <context id="DB2Tables" targetRuntime="MyBatis3"> <!-- 序列化model --> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- 數(shù)據(jù)庫鏈接URL村刨、用戶名幢痘、密碼 --> <!-- <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/school?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true" userId="root" password="123456"> </jdbcConnection> --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3307/school?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true" userId="root" password="970809"> </jdbcConnection> <!-- <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="scott" password="tiger"> </jdbcConnection> --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置 --> <javaModelGenerator targetPackage="com.pojo" targetProject=".\src"> <property name="enableSubPackages" value="false"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成的映射文件包名和位置 --> <sqlMapGenerator targetPackage="com.mapper" targetProject=".\src"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成DAO的包名和位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.mapper" targetProject=".\src"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要生成那些表(更改tableName和domainObjectName就可以) --> <table tableName="student" schema=""/> <table tableName="subject" schema=""/> <table tableName="score" schema=""/> </context> </generatorConfiguration>
-
添加測試類
import java.io.File; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.api.ShellCallback; 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; public class GenerateUtils { public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException { File f = new File("target/classes/generatorConfig.xml"); List<String> warnings = new ArrayList<String>(); ConfigurationParser parser = new ConfigurationParser(warnings); Configuration configuration = parser.parseConfiguration(f); boolean overwrite = true; ShellCallback shellCallback = new DefaultShellCallback(overwrite); MyBatisGenerator generator = new MyBatisGenerator(configuration, shellCallback, warnings); generator.generate(null); System.out.println("OK!"); } }