Reverse engineering (generator) -----> From Table to help us to generate dao, bean, xml

1) into a jar mybatis-generator

2) New generator.xml file (copy the contents of the official website in the project directory)

 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 <generatorConfiguration>
 6     <!-- 数据库驱动jar包所在的位置 -->
 7   <classPathEntry location="D:\\mybatis\\generator830\\lib\\mysql-connector-java-5.1.47.jar" />
 8   <context id="DB2Tables" targetRuntime="MyBatis3">
 9  <!--  去除注释 -->
10       <commentGenerator>
11       <property name="suppressAllComments" value="true" />
12         </commentGenerator>
13    <!--  数据源信息 -->
14     <jdbcConnection driverClass="com.mysql.jdbc.Driver"
15         connectionURL="jdbc:mysql://localhost:3306/mybatis"
16         userId="root"
17         password="root">
jdbcConnection</18     >
19     <javaTypeResolver >
20       <property name="forceBigDecimals" value="false" />
21     </javaTypeResolver>
22     <!-- 生成的实体类所在的位置 -->
23     <javaModelGenerator targetPackage="com.zhiyou.clg.bean" targetProject="./src">
24       <property name="enableSubPackages" value="true" />
25       <property name="trimStrings" value= "to true"  /> 
26 is      </ javaModelGenerator > 
27      <-! position where the generated map file -> 
28      < sqlMapGenerator targetPackage = "com.zhiyou.clg.mapper"   targetProject = "./ Resource" > 
29        < Property name = "enableSubPackages" value = "to true"  /> 
30      </ sqlMapGenerator > 
31 is      <-! position where generated dao -> 
32      < javaClientGenerator type = "XMLMAPPER"targetPackage = "com.zhiyou.clg.dao"  targetProject = "./ the src" > 
33 is        < Property name = "enableSubPackages" value = "to true"  /> 
34 is      </ javaClientGenerator > 
35      <-! objects and entity class table
 36          Schema: the database tables are located
 37 [          tableName : table
 38 is          domainObjectName: the entity class name
 39       -> 
40      < table Schema = "the DB2ADMIN" tableName = "User" domainObjectName = "the User"   
41 is       enableCountByExample = "to false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false" >
42       <property name="useActualColumnNames" value="true"/>
43       <generatedKey column="ID" sqlStatement="DB2" identity="true" />
44       <columnOverride column="DATE_FIELD" property="startDate" />
45       <ignoreColumn column="FRED" />
46       <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
47     </table>
48 
49   </context>
50 </generatorConfiguration>

3) In the new test package src directory, then the new Test classes, copy the contents of the official website at main, after running guide packet

 1 package com.zhiyou.clg.test;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.util.ArrayList;
 6 import java.util.List;
 7 
 8 import org.mybatis.generator.api.MyBatisGenerator;
 9 import org.mybatis.generator.config.Configuration;
10 import org.mybatis.generator.config.xml.ConfigurationParser;
11 import org.mybatis.generator.exception.XMLParserException;
12 import org.mybatis.generator.internal.DefaultShellCallback;
13 
14 public class TestGenerator {
15     public static void main(String[] args) throws Exception{
16         List<String> warnings = new ArrayList<String>();
17            boolean overwrite = true;
18            File configFile = new File("generator.xml");
19            ConfigurationParser cp = new ConfigurationParser(warnings);
20            Configuration config = cp.parseConfiguration(configFile);
21            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
22            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
23            myBatisGenerator.generate(null);
24     }
25 }

 

Guess you like

Origin www.cnblogs.com/lwgok1003/p/11442721.html