Mybatis-反向生成增删改查-generatorConfiguration

简单上代码

一 .添加pom坐标
<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.7</version>
</dependency>
二. 准备数据库驱动mysql-connector-java-5.1.32.jar

Mybatis-反向生成增删改查-generatorConfiguration_第1张图片

三. generatorConfig.xml 注意:(放到和pom.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="E:\maven\repository\mysql\mysql-connector-java\5.1.32\mysql-connector-java-5.1.32.jar"/>
    <context id="DB2Tables"    targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接地址账号密码-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/route" userId="root" password="root">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成Model类存放位置-->
        <javaModelGenerator targetPackage="com.zsdn.gateway.route.pojo" targetProject="D:\idea-workSpace\route\src\main\java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成映射文件存放位置-->
        <sqlMapGenerator targetPackage="mapper" targetProject="D:\idea-workSpace\route\src\main\resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成Dao类存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.zsdn.gateway.route.mapper" targetProject="D:\idea-workSpace\route\src\main\java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--生成对应表及类名-->
        <table tableName="airway" domainObjectName="AirWay" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>
DemoMybatisApplication.java
package com.zsdn.gateway.route.uitls;

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;

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

/**
 * @program: route
 * @description: 生成基础增加删除修改查询功能 (按照generatorConfig.xml生成)
 * @author: Mr.Li
 * @create: 2020-08-31 11:01
 **/
public class DemoMybatisApplication {
     

    public void generator() throws Exception {
     
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        // 指定配置文件
        File configFile = new File("generatorConfig.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);
    }

    // 执行main方法以生成代码
    public static void main(String[] args) {
     
        try {
     
            DemoMybatisApplication generatorSqlmap = new DemoMybatisApplication();
            generatorSqlmap.generator();
        } catch (Exception e) {
     
            e.printStackTrace();
        }
    }
}

Mybatis-反向生成增删改查-generatorConfiguration_第2张图片
好了就到这里了~~

你可能感兴趣的:(mybaits,计算机,java,java)