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


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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-// MyBatis Generator Configuration 1.0//EN"

        targetRuntime="MyBatis3Simple":Build a simple version of CRUD

  <context id="DB2Tables" targetRuntime="MyBatis3">
    <!-- jdbcConnection: Specify how to connect to the target database -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"

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

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

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

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

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

③ Write a method in the test class to run

package com.cun.mybatis.test;

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 {

    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);


④ 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