8. Reverse Engineering

1.  Mybatis reverse engineering (generator). Table help us to generate dao, bean, xml mapping file.

http://www.mybatis.org/generator/index.html。

  1.  Introducing mybatis-generator jar package

 2. Create generator configuration file.

        3. Run the generator

 

        2. Create generator configuration file.

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE generatorConfiguration
 3   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
 4   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
 5 <!-- generator的配置文件 -->
 6 <generatorConfiguration>
 7  <!-- mysql驱动jar所在的位置 -->
 8   <classPathEntry location="C:\\javaHouse\\mybatis04\\lib\\mysql-connector-java-5.1.47.jar" />
 9   
10   <!- ->data source information
11   <context id="DB2Tables" targetRuntime="MyBatis3">
12       <!-- 默认不加注释 -->
13       <commentGenerator>
14           <property name="suppressAllComments" value="true" />
15     </commentGenerator>
16   
17     <jdbcConnection driverClass="com.mysql.jdbc.Driver"
18         connectionURL="jdbc:mysql://localhost:3306/mybatis"
19         userId="root"
20         password="root">
21     </jdbcConnection>
22 
23     <javaTypeResolver >
24       <property name="forceBigDecimals" value="false" />
25     </javaTypeResolver>
26     
27     <!-- 生产的实体类所在的位置 -->
28     <javaModelGenerator targetPackage="com.zhiyou100.wc.bean" targetProject="./src">
29       <property name="enableSubPackages" value="true" />
30       <property name="trimStrings" value="true" />
31     </javaModelGenerator>
32     <!-- 生成的映射文件所在的位置 -->
33     <sqlMapGenerator targetPackage="com.zhiyou100.wc.mapper"  targetProject="./resources">
34       <property name="enableSubPackages" value="true" />
35     </sqlMapGenerator>
36     <!- <37->location of the generated file resides dao
     javaClientGenerator type = "XMLMAPPER" targetPackage = "com.zhiyou100.wc.dao"   targetProject = "./ the src" > 
38 is        < Property name = "enableSubPackages" value = "to true"  /> 
39      </ javaClientGenerator > 
40  
41 is      <! - - correspondence relationship with an entity class tables
 42 is            Schema: the database table resides
 43 is            tableName: table
 44 is            domainObjectName: the entity class name
 45       -> 
46 is      < table Schema = "MyBatis" tableName="users" domainObjectName="Users" 
47     enableCountByExample="false" enableSelectByExample="false" enableDeleteByExample="false" enableUpdateByExample="false">
48       <property name="useActualColumnNames" value="true"/>
49       <generatedKey column="ID" sqlStatement="DB2" identity="true" />
50       <columnOverride column="DATE_FIELD" property="startDate" />
51       <ignoreColumn column="FRED" />
52       <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
53     </table>
54 
55   </context>
56 </generatorConfiguration>

  3. Run the generator

 1 package com.zhiyou100.wc.test;
 2 
 3 import java.io.File;
 4 import java.util.ArrayList;
 5 import java.util.List;
 6 
 7 import org.mybatis.generator.api.MyBatisGenerator;
 8 import org.mybatis.generator.config.Configuration;
 9 import org.mybatis.generator.config.xml.ConfigurationParser;
10 import org.mybatis.generator.internal.DefaultShellCallback;
11 //Ctrl+Shift+o 导包的快捷键
12 public class TestGRT {
13     public static void main(String[] args) throws Exception{
14        List<String> warnings = new ArrayList<String>();
15        boolean overwrite = true;
16        File configFile = new File("generator.xml");
17        ConfigurationParser cp = new ConfigurationParser(warnings);
18        Configuration config = cp.parseConfiguration(configFile);
19        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
20        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
21        myBatisGenerator.generate(null);
22     }
23 
24 }

Guess you like

Origin www.cnblogs.com/banzhuanlaowang/p/11455369.html