一、下载jar包导入新的项目中:
配置文件generator.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> <!-- 本地硬盘数据库驱动包 --> <classPathEntry location="C:UsershpDesktopmysql-connector-java-5.1.6-bin.jar" /> <context id="Mybatis3Context" targetRuntime="MyBatis3"> <property name="javaFileEncoding" value="UTF-8"/> <commentGenerator> <!-- 去掉生成日期那行注释 --> <property name="suppressDate" value="true"/> <!-- 是否去除自动生成所有的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/student?characterEncoding=UTF-8" userId="root" password=""> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 模型的包名和位置--> <javaModelGenerator targetPackage="org.ruangong.model" targetProject=".src"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 映射文件的包名和位置--> <sqlMapGenerator targetPackage="org.ruangong.mapperxml" targetProject=".src"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- DAO的包名和位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="org.ruangong.mapper" targetProject=".src"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <table tableName="person" domainObjectName="Student" enableCountByExample="false" enableDeleteByExample="false" enableUpdateByExample="false" enableSelectByExample="false"> </table> <table tableName="person_card" domainObjectName="Student" enableCountByExample="false" enableDeleteByExample="false" enableUpdateByExample="false" enableSelectByExample="false"> </table> <table tableName="person_class" domainObjectName="Student" enableCountByExample="false" enableDeleteByExample="false" enableUpdateByExample="false" enableSelectByExample="false"> </table> </context> </generatorConfiguration>
在测试类中测试:
package org.ruangong.test; 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.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 Test { public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException{ File file = new File("src/generator.xml"); List<String> warnings = new ArrayList<>(); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(file); DefaultShellCallback callBack = new DefaultShellCallback(true); MyBatisGenerator generator = new MyBatisGenerator(config,callBack,warnings); generator.generate(null); } }
通过设计数据库可生成mapper,entity等文件。