zoukankan      html  css  js  c++  java
  • 学习Mybatis中的逆向工程

    一、下载jar包导入新的项目中:

     配置文件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>
        <!-- 本地硬盘数据库驱动包 -->
        <classPathEntry location="C:UsershpDesktopmysql-connector-java-5.1.6-bin.jar" />
    
        <context id="Mybatis3Context" targetRuntime="MyBatis3">
    
            <property name="javaFileEncoding" value="UTF-8"/>
    
            <commentGenerator>
                <!-- 去掉生成日期那行注释 -->
                <property name="suppressDate" value="true"/>
                <!-- 是否去除自动生成所有的注释 true:是 : false:否 -->
                <property name="suppressAllComments" value="true"/>
            </commentGenerator>
    
            <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                            connectionURL="jdbc:mysql://localhost:3306/student?characterEncoding=UTF-8"
                            userId="root"
                            password="">
            </jdbcConnection>
    
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false"/>
            </javaTypeResolver>
    
            <!-- 模型的包名和位置-->
            <javaModelGenerator targetPackage="org.ruangong.model" targetProject=".src">
                <property name="enableSubPackages" value="true"/>
                <property name="trimStrings" value="true"/>
            </javaModelGenerator>
    
            <!-- 映射文件的包名和位置-->
            <sqlMapGenerator targetPackage="org.ruangong.mapperxml" targetProject=".src">
                <property name="enableSubPackages" value="true"/>
            </sqlMapGenerator>
    
            <!-- DAO的包名和位置-->
            <javaClientGenerator type="XMLMAPPER" targetPackage="org.ruangong.mapper" targetProject=".src">
                <property name="enableSubPackages" value="true"/>
            </javaClientGenerator>
    
            <table tableName="person" domainObjectName="Student" enableCountByExample="false"
                   enableDeleteByExample="false" enableUpdateByExample="false" enableSelectByExample="false">
            </table>
            <table tableName="person_card" domainObjectName="Student" enableCountByExample="false"
                   enableDeleteByExample="false" enableUpdateByExample="false" enableSelectByExample="false">
            </table>
            <table tableName="person_class" domainObjectName="Student" enableCountByExample="false"
                   enableDeleteByExample="false" enableUpdateByExample="false" enableSelectByExample="false">
            </table>
        </context>
        
    </generatorConfiguration>
    

      在测试类中测试:

    package org.ruangong.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 Test {
    	public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException{
    		File file = new File("src/generator.xml");
    		List<String> warnings = new ArrayList<>();
    		ConfigurationParser cp = new ConfigurationParser(warnings);
    		Configuration config = cp.parseConfiguration(file);
    		DefaultShellCallback callBack = new DefaultShellCallback(true);
    		MyBatisGenerator generator = new MyBatisGenerator(config,callBack,warnings);
    		generator.generate(null);
    	}
    }
    

      通过设计数据库可生成mapper,entity等文件。

  • 相关阅读:
    PAT 1020 月饼 (25)(精简版代码+思路+推荐测试用例)
    PAT 1010 一元多项式求导 (25)(STL-map+思路)
    通过无线连接的方式来做 Appium 自动化
    Eclipse shift + ctrl + F 不好用
    Appium 出现 > error: com.test/.activity1 never started. Current: com.test/.activity2
    Appium 出现 > error: com.test/.activity1 never started. Current: com.test/.activity2
    从CSDN 来到博客园入驻——2015/1/28
    敏捷自动化测试(1)—— 我们的测试为什么不够敏捷?
    敏捷自动化测试(2)——像用户使用软件一样享受自动化测试
    Android自动化测试之Monkey工具
  • 原文地址:https://www.cnblogs.com/jccjcc/p/13973014.html
Copyright © 2011-2022 走看看