MyBatis uses reverse engineering to automatically generate the entity layer and dao layer of the project

1, Foreword
I wrote an article about MyBatis
Click the link to enter the basic CRUD of low version comparison
However, it is not necessary to write dao layer and entity layer code in actual development, mainly using the shelf package provided by MyBatis

Here is the whole process of using reverse engineering

2, Demo and code

① Create a java project

Name the project and click finish

② Create some packages and xml files (you don't need to create them, but you can create them for demonstration)

dbconfig.xml is the basic information of the database. MySQL database is used here. See the notes for the specific content to be configured

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true
jdbc.username=root
jdbc.password=123

mbg.xml is the basic configuration file information of reverse engineering

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

    <!-- 
        targetRuntime="MyBatis3Simple":Build a simple version of CRUD
        MyBatis3:Deluxe

     -->
  <context id="DB2Tables" targetRuntime="MyBatis3">
    <!-- jdbcConnection: Specify how to connect to the target database -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true"
        userId="root"
        password="123">
    </jdbcConnection>

    <!--  -->
    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>

    <!-- javaModelGenerator: Appoint javaBean Generation strategy for 
    targetPackage="test.model": Target package name
    targetProject="\MBGTestProject\src": Target project
    -->
    <javaModelGenerator targetPackage="com.cun.mybatis.bean" 
            targetProject=".\src">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>

    <!-- sqlMapGenerator: sql Map generation policy: -->
    <sqlMapGenerator targetPackage="com.cun.mybatis.xml"  
        targetProject=".\src">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>

    <!-- javaClientGenerator:Appoint mapper Location of the interface -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.cun.mybatis.dao"  
        targetProject=".\src">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>

    <!-- Specify which tables to reverse analyze: create from tables javaBean -->
    <table tableName="user" domainObjectName="User"></table>
  </context>
</generatorConfiguration>

③ Write a method in the test class to run

package com.cun.mybatis.test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
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 MyTest {

    @Test
    public void testMbg() throws Exception {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("mbg.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);
    }

}

④ After running, refresh the project, and you will find "enriched" in the package

So far, reverse engineering is completed

Tags: Mybatis JDBC xml MySQL

Posted on Fri, 01 May 2020 00:14:19 -0700 by geetakhurana