1.准备配置文件
<?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="D:/mysql-connector-java-5.1.40-bin.jar" /> <context id="cosmetic" targetRuntime="MyBatis3"> <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/cosmetic" userId="root" password="0000"> </jdbcConnection> <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver" connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:orcl" userId="scott" password="0000"> </jdbcConnection> --> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--生成pojo类存放位置--> <javaModelGenerator targetPackage="cn.pojo" targetProject="src"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--生成映射文件mapper接口存放位置--> <sqlMapGenerator targetPackage="cn.dao" targetProject="src"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--生成mapper接口、mapper.xml类存放位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="cn.dao" targetProject="src"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!--生成对应表及类名,需要记住的一点是逆向工程无法生成关联关系,只能生成单表操作--> <table tableName="adminuser" domainObjectName="User" /> <table tableName="messageboard" domainObjectName="Messageboard" /> <table tableName="picture" domainObjectName="Picture"></table> <table tableName="product" domainObjectName="Product"></table> <table tableName="tourist" domainObjectName="Tourist"></table> <table tableName="type" domainObjectName="Type"></table> </context> </generatorConfiguration>
2.下载jar包,大家如果找不到,可以去maven repository去下载:
mybatis-generator-core-1.3.2.jar
3.编写java程序代码,在开发文档首页,粘贴一下就好了
package cn.test; import java.io.File; 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.internal.DefaultShellCallback; public class MybatisGen { public static void generator() throws Exception { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; // 项目根路径不要有中文,我的有中文,所以使用绝对路径 File configFile = new File("D:/Program Files (x86)/MyEclipseWorkSpace/makeups/config/db.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } public static void main(String[] args) { try { generator(); } catch (Exception e) { e.printStackTrace(); } } }