zoukankan      html  css  js  c++  java
  • mybatis generator(MyBatis的逆向工程)

    1创建数据表

    如图所示:我的是在text数据库中创建了一个Student表,字段有id(int),   name(varchar),     age(int),    score(int)

    2创建项目

    1)加入jar特殊的jar包   mybatis-generator-core-x.x.x.jar 

    2)创建项目

    3)编写GeneratorXML.xml

    注意GeneratorXML.xml的位置,和src同级

      

       

    <?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>  
          
            <context id="DB2Tables" targetRuntime="MyBatis3">  
          
                <!-- 不生成注释 -->  
                <commentGenerator>  
                    <property name="suppressAllComments" value="true" />  
                </commentGenerator>  
          
          
                <!-- 连接的数据库的信息 -->  
                <jdbcConnection driverClass="com.mysql.jdbc.Driver"  
                    connectionURL="jdbc:mysql://localhost:3306/text" userId="root"  
                    password="root">  
                </jdbcConnection>  
          
                <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL   
                    和 NUMERIC 类型解析为java.math.BigDecimal -->  
                <javaTypeResolver>  
                    <property name="forceBigDecimals" value="false" />  
                </javaTypeResolver>  
          
          
                <!-- javaBean生成的位置 -->  
                <javaModelGenerator targetPackage="com.imooc.entity"  
                    targetProject=".src">  
                    <property name="enableSubPackages" value="true" />  
                    <property name="trimStrings" value="true" />  
                </javaModelGenerator>  
          
          
                <!-- sql映射文件的位置-->  
                <sqlMapGenerator targetPackage="com.imooc.mapper targetProject=".src">  
                    <property name="enableSubPackages" value="true" />  
                </sqlMapGenerator>  
          
                <!-- dao的位置位置 -->  
                <javaClientGenerator type="XMLMAPPER"  
                    targetPackage="com.imooc.dao</span>" targetProject=".src">  
                    <property name="enableSubPackages" value="true" />  
                </javaClientGenerator>  
          
                <!-- tableName对应数据库中的表名,domainObjectName对应实体类 -->  
                <table tableName="student" domainObjectName="Student"></table>  
          
            </context>  
        </generatorConfiguration> 

    4)编写程序执行上面的XML

    package com.imooc.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 MainGenerator {
        
        public static void main(String[] args) throws Exception {
               List<String> warnings = new ArrayList<String>();
               boolean overwrite = true;
                      File configFile = new File("GeneratorXML.xml");//就这一句需要注意,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);
               System.out.println("123123");
        }
     
    }

    5)查看结果

    如果是Oracle

    <?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>
     
     
     
        <context id="DB2Tables" targetRuntime="MyBatis3">
     
            <!-- 不生成注释 -->
            <commentGenerator>
                <property name="suppressAllComments" value="true" />
            </commentGenerator>
     
     
     
     
            <!-- 连接的数据库的信息 -->
            <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
                connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:Orcl" userId="user1"
                password="123456">
            </jdbcConnection>
     
            <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 
                和 NUMERIC 类型解析为java.math.BigDecimal -->
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>
     
     
            <!-- javaBean生成的位置 -->
            <javaModelGenerator targetPackage="entity"
                targetProject=".src">
                <property name="enableSubPackages" value="true" />
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
     
     
            <!-- sql映射文件的位置-->
            <sqlMapGenerator targetPackage="mapper" targetProject=".src">
                <property name="enableSubPackages" value="true" />
            </sqlMapGenerator>
     
            <!-- dao的位置位置 -->
            <javaClientGenerator type="XMLMAPPER"
                targetPackage="dao" targetProject=".src">
                <property name="enableSubPackages" value="true" />
            </javaClientGenerator>
     
            <!-- tableName对应数据库中的表名,domainObjectName对应实体类 -->
            <table tableName="TB_JOB" domainObjectName="Job"></table>
     
     
        </context>
    </generatorConfiguration>
  • 相关阅读:
    android29
    android28
    android27
    android26
    Dynamics CRM2011 MspInstallAction failed when installing an Update Rollup
    Dynamics CRM Import Solution Attribute Display Name description is null or empty
    The service cannot be activated because it does not support ASP.NET compatibility
    IIS部署WCF报 无法读取配置节“protocolMapping”,因为它缺少节声明
    Unable to access the IIS metabase.You do not have sufficient privilege
    LM算法与非线性最小二乘问题
  • 原文地址:https://www.cnblogs.com/alsf/p/9324221.html
Copyright © 2011-2022 走看看