Mybatis的逆向工程(generator)
由表来帮我们生成dao,bean,xml映射文件
参考http://www.mybatis.org/generator/
1、首先要引入mybatis-generator的jar包
2、创建generator的配置文件
进入http://www.mybatis.org/generator/网站,找到配置的xml代码如下
进入上图红色框中的 XML Configuration.....看到如下图网页中的代码
就如下所示,下面是我配置好的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> <!--mysql架包路径 --> <classPathEntry location="E://maven//apache-maven-3.6.1-bin//apache-maven-3.6.1//repository//mysql//mysql-connector-java//5.1.30//mysql-connector-java-5.1.30-sources.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3"> <!--去掉注释 --> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--连接数据库 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password="zjc"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!--bean文件的路径 --> <javaModelGenerator targetPackage="com.zhiyou100.zjc.bean" targetProject=".src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--mapper文件的路径 --> <sqlMapGenerator targetPackage="com.zhiyou100.zjc.mapper" targetProject=". esources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!--dao文件的路径 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.zhiyou100.zjc.dao" targetProject=".src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!--schema:数据库名 domainObjectName:实体类名 --> <table schema="mybatis" tableName="users" domainObjectName="User" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> <property name="useActualColumnNames" value="true"/> <generatedKey column="ID" sqlStatement="DB2" identity="true" /> <columnOverride column="DATE_FIELD" property="startDate" /> <ignoreColumn column="FRED" /> <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> </table> </context> </generatorConfiguration>
3、运行generator
完成上面的1和2的配置后,就可以运行generator了,
进入官网找到generator的Java运行代码,如下图所示
进入红框中的Java program,找到如下代码:
如下代码
package com.zhiyou100.zjc.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 GeneratorTest { public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException { // TODO Auto-generated method stub List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File("generator.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); } }