MyBatis reverse engineering Introduction

1. Concept:


Reverse engineering is to generate the code corresponding MyBatis (XXXMapper.java/XXXMapper.xml/Moudle(XXX) in accordance with the corresponding database table in the project), the generated code reverse engineering can be a simple database operations can save a lot of time, MyBatis official provided the corresponding code, so use more in practice.

2. Operation Configuration


1. Download the corresponding jar package, there are two:

  Download link (end of the article with the complete project code): //download.csdn.net/download/weixin_45910396/12013449

2. Project directory:


3. main methods:

After importing the core code jar: Open URL


code show as below:

public static void main(String[] args)throws Exception {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("src/generator.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);
    }

4. generator.xml profile

The same web site, open XML Configuration Reference


My code is as follows:

<?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">

<gen 大专栏  MyBatis 逆向工程介绍eratorConfiguration>

    <context id="mysqlTables" targetRuntime="MyBatis3">

        <!--数据库配置-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/test"
                        userId="root"
                        password="root0809">
        </jdbcConnection>

        <!-- java类型解析 -->
        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- 模型生成包名-->
        <javaModelGenerator targetPackage="com.seven.model" targetProject=".src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- mybatis的映射.xml-->
        <sqlMapGenerator targetPackage="com.seven.mapper" targetProject=".src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- mybatis 的mapper接口生成的包路径-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.seven.mapper" targetProject=".src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 配置生成表的模型-->
        <table tableName="rs" domainObjectName="Rs"/>
        <table tableName="pollutiontrend" domainObjectName="PollutionTrend"/>

    </context>
</generatorConfiguration>

At this point, all the configuration MyBatis reverse engineering have been completed, run the main method to obtain the corresponding model and the mapper and mapper.xml file.

  Accompanied by a full project code, you can use the direct download: //download.csdn.net/download/weixin_45910396/12013612

MyBatis run reverse engineering, found in the model folder created under the XXXExample class Example For more information about the class can refer to another article "Mybatis reverse engineering class generated in Example details"

Guess you like

Origin www.cnblogs.com/lijianming180/p/12370498.html