Java Learning MyBatis (4)

I haven't learned Java for a long time, mainly because I was lazy, and then I started playing games. Nan brother taught you to learn Java , I learned from him. Below are my lecture notes.


I learned reverse engineering this time. My understanding is: One-click generation tool ... Although it is not a mouse click and it came out, but I also wrote some code, and it came out and it was very easy to use.

  • First of all:Insert picture description here
  • For this I have recreated a project. (The red circle is the remaining entity and repository written by yourself is automatically generated)
  • pom.xml file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.southwind</groupId>
    <artifactId>aimgb</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.19</version>
        </dependency>
<!--        导入逆向工程需要的包-->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies>

</project>
  • generatorConfig.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>
    <!--创建MBG配制文件-->
    <!--    1.jdbcConnection 配制数据库连接信息-->
    <!--    2.javaModelGenerator 配制 JavaBean 的生成策略-->
    <!--    3.sqlMapGenerator 配制SQL映射文件生成策略-->
    <!--    4.javaClientGenerator 配制Mapper 接口的生成策略-->
    <!--    5.table 配制目标数据表(tableName:表名,domainObjectName:javaBean 类名)-->
    <context id="testTables" targetRuntime="MyBatis3">
<!--        第一个属性是驱动 第二个是连接地址 第三个是用户名 第四个是密码-->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://.......?useUnicode=true&amp;characterEncoding=UTF-8"
                        userId=""
                        password=""
        ></jdbcConnection>
<!--        第一个属性是 将来存放的包的名字,把实体类放进去 第二个属性是 包放在哪-->
        <javaModelGenerator targetPackage="com.southwind.entity" targetProject="./src/main/java"></javaModelGenerator>
<!--        sql语句的映射 属性同理-->
        <sqlMapGenerator targetPackage="com.southwind.repository" targetProject="./src/main/java"></sqlMapGenerator>
<!--        mapper接口-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.southwind.repository" targetProject="./src/main/java"></javaClientGenerator>
<!--        table-->
        <table tableName="goods" domainObjectName="Goods"></table>
    </context>
</generatorConfiguration>

  • Main startup class

package com.southwind.test;

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;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//创建Generator执行类
public class Main {
    public static void main(String[] args) {
        List<String> warings = new ArrayList<String>();
        boolean overwrite = true ;
        String genCig = "/generatorConfig.xml";
        File configFile = new File(Main.class.getResource(genCig).getFile());
        ConfigurationParser configurationParser = new ConfigurationParser(warings);
        Configuration configuration = null ;
        try {
            configuration = configurationParser.parseConfiguration(configFile);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMLParserException e) {
            e.printStackTrace();
        }
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = null;
        try {
            myBatisGenerator = new MyBatisGenerator(configuration,callback,warings);
        } catch (InvalidConfigurationException e) {
            e.printStackTrace();
        }
        try {
            myBatisGenerator.generate(null);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


Then the operation is completed like my file directory. Is there any code that feels less thieves? It feels good to copy and paste it, it's pretty easy to use.

Published 20 original articles · praised 4 · visits 612

Guess you like

Origin blog.csdn.net/qq_45031575/article/details/105469487