MyBatis ----- 6. Reverse engineering (generator)

1. What is reverse engineering
a major feature of MyBatis is the need for the programmer to write sql, so if the table is too much, there will inevitably be a lot of trouble, so mybatis official provides a reverse engineering can be automatically generated mybatis execute against a single table code requires (including mapper.xml, mapper.java, po ..). Usually in development, the reverse engineering approach is used to generate the code table by the database.
2. The use of reverse engineering

2.1 Creating a new project

2.2 Import jar package

 

 

 mybatis-generator-core-1.3.5.jar address:

https://github.com/mybatis/generator/releases

 

2.3 Definitions generator.xml file

<?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="E:/Jar/mysql-connector-java-5.1.46.jar" />

  <context id="DB2Tables" targetRuntime="MyBatis3">
      <commentGenerator>
        <!-- 是否去除自动生成的注释 -->
        <property name="suppressAllComments"value = "to true" /> 
    </ commentGenerator > 
    <-! Information Mysql database connection: driver class, connection address, user name, password -> 
    < JdbcConnection driverClass = "com.mysql.jdbc.Driver" 
        the connectionURL = " JDBC: MySQL: // localhost: 3306 / MyBatis " 
        the userId =" the root " 
        password =" 123456 " > 
    </ JdbcConnection > 
    
    <-! default is false, the JDBC type NUMERIC DECIMAL and resolved to Integer, to true 
    the JDBC DECIMAL and NUMERIC type resolution is java.math.BigDecimal -> 
    < javaTypeResolver > 
      <property name="forceBigDecimals"value = "to false"  /> 
    </ javaTypeResolver > 
    
    <-! targetProject: generating location entity class -> 
    < javaModelGenerator targetPackage = "com.zhiyou.zyl.bean" targetProject = "./ the src" > 
      <-! enableSubPackages: whether to allow schema package as a suffix -> 
      < Property name = "enableSubPackages" value = "to true"  /> 
      <-! returned from the database value spaces before and after being cleaned -> 
      < Property name = "trimStrings" value = "to true"  /> 
    </javaModelGenerator>
    
    <!-- targetProject:mapper映射文件生成的位置 -->
    <sqlMapGenerator targetPackage="com.zhiyou.zyl.mapper"  targetProject="./src">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
    
    <!-- targetProject:mapper接口生成的的位置 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.zhiyou.zyl.dao"  targetProject="./src">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
    
    <!-- 指定数据表 -->
    <table schema="mybatis" tableName="users" domainObjectName="Users" >
      <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>

2.4 Create class performs xml file

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 Generator {
     public void generator() throws Exception{

            List<String> warnings = new ArrayList<String>();
            boolean overwrite = true;
            //指定 逆向工程配置文件
            File configFile = new File("generoter.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) throws Exception {
            try {
                Generator generatorSqlmap = new Generator();
                generatorSqlmap.generator();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
}

2.5 execution results

 

Guess you like

Origin www.cnblogs.com/zyl187110/p/11442825.html