mybatis-generator逆向工程,自动生成dao层和mapper配置

Posted mp-ui

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis-generator逆向工程,自动生成dao层和mapper配置相关的知识,希望对你有一定的参考价值。

官方文档:http://mybatis.org/generator/

  1. 先引入maven依赖
<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.4.0</version>
</dependency>
  1. 编写配置文件mbg.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>


    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
        	<!--不要注释-->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql:///student_score_system"
                        userId="root"
                        password="root">
        </jdbcConnection>

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

		<!--实体类存放的位置-->
        <javaModelGenerator targetPackage="com.prince.entity" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

		<!--mapper配置存放的位置-->
        <sqlMapGenerator targetPackage="mapper"  targetProject="./src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

		<!--dao接口存放的位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.prince.dao"  targetProject="./src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

		<!--你数据库里有哪些table都要一个一个列出来,要不然不会生成-->
        <table tableName="Student" domainObjectName="Student"></table>
        <table tableName="Teacher" domainObjectName="Teacher"></table>
        <table tableName="Course" domainObjectName="Course"></table>
        <table tableName="SC" domainObjectName="SC"></table>
        <table tableName="Admin" domainObjectName="Admin"></table>
    </context>
</generatorConfiguration>
  1. 复制下面的Java代码,执行
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;
import java.util.Objects;

public class Generator {
    public static void main(String[] args) throws SQLException, IOException, InterruptedException, XMLParserException, InvalidConfigurationException {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File(Objects.requireNonNull(Generator.class.getClassLoader().getResource("mbg.xml")).getPath());
        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);
    }
}

以上是关于mybatis-generator逆向工程,自动生成dao层和mapper配置的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis-generator,自动生成文件.问题及解决

利用mybatis-generator自动生成表实例类和映射文件

mybatis-generator自动生成

idea中使用mybatis逆向工程

Intellij IDEA中mybatis-generator自动生成

Mybatis-Generator自动生成DaoModelMapping等相关映射文件(懒人版)