mybatis逆向工程的使用

1、什么是逆向工程

mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml、pojo等)

2、添加依赖

<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
    <dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-core</artifactId>
      <version>1.3.5</version>
    </dependency>    

3、新建一个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>
    <!--导入的类什么的,我们不需要,直接删除-->
    <!--<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />-->

    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!--如果不配置这个,生成的文件会有很多的注释,不方便看代码-->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!--配置数据库连接-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/jwdb?useSSL=true"
                        userId="数据库账号"
                        password="数据库密码">
        </jdbcConnection>
        <!--配置数据库连接-->

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

        <!--JAVA模型生成,指定javabean生成位置-->
        <javaModelGenerator targetPackage="com.team5101.po" targetProject=".\src\main\java">
            <!--JAVA模型生成,指定javabean生成位置-->
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!--指定sql映射文件生成位置-->
        <sqlMapGenerator targetPackage="mapper"  targetProject=".\src\main\resources\mapper">
            <!--指定sql映射文件生成位置-->
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!--指定dao生成的位置,mapper接口-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.team5101.dao"  targetProject=".\src\main\java">
            <!--指定dao生成的位置,mapper接口-->
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!--table指定表的生成策略-->
        <table tableName="数据表" domainObjectName="pojo类类名"></table>
        <table tableName="t_user" domainObjectName="User"></table>
        <table tableName="t_score" domainObjectName="Score"></table>
        <table tableName="t_student" domainObjectName="Student"></table>
        <table tableName="t_course" domainObjectName="Course"></table>

    </context>
</generatorConfiguration>

4、新建测试类MbgTest.java

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;

public class MbgTest {
    public static void main(String[] args) 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);
    }
}

运行测试类,自动生成如下文件
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值