MyBatis学习笔记7-逆向工程

本文详细介绍了如何在Maven工程中利用MyBatis Generator进行逆向工程,配置文件详解、生成操作流程及常用方法总结,助您快速上手MyBatis映射文件生成。
*个人对mybatis逆向工程的理解,就是一个工具,此工具可以一键生成mybatis配置文件和主要文件*

mybatis逆向工程的使用: 1.在maven工程中引入jar包:

<!--    逆向工程jar包-->
    <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.3.7</version>
    </dependency>

在当前的根目录下,创建一个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://localhost:3306/mybatis_test" userId="root"
                        password="123456">
        </jdbcConnection>

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

        <!-- 指定javaBean生成的位置 -->
        <javaModelGenerator targetPackage="bean"
                            targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

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

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


        <!-- table指定每个表的生成策略 配置表名和对应的实体类名,我这里生成了两个表,分别是数据库中的user表和account表 -->
        <table tableName="user" domainObjectName="User"></table>
        <table tableName="account" domainObjectName="Account"></table>
    </context>
</generatorConfiguration>

其中,注释已经写的很清楚了,需要改动什么,添加什么,我们按照已有的代码改动就好

最后,写一个配置文件,将上面的配置文件执行一下,如下:

public class MyTest {
    @Test
    public void test() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
        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);
    }
}

最后生成的目录如下:
在这里插入图片描述

逆向工程中的方法总结:

  • int countByExample(UserExample example) thorws SQLException按条件计数
  • int deleteByPrimaryKey(Integer id) thorws SQLException按主键删除
  • int deleteByExample(UserExample example) thorws SQLException按条件删除
  • String/Integer insert(User record) thorws SQLException插入数据(返回值为ID)
  • User selectByPrimaryKey(Integer id) thorws SQLException按主键查询
  • ListselectByExample(UserExample example) thorws SQLException按条件查询
  • ListselectByExampleWithBLOGs(UserExample example) thorws SQLException按条件查询(包括BLOB字段)。只有当数据表中的字段类型有为二进制的才会产生
  • int updateByPrimaryKey(User record) thorws SQLException按主键更新
  • int updateByPrimaryKeySelective(User record) thorws SQLException按主键更新值不为null的字段
  • int updateByExample(User record, UserExample example) thorws SQLException按条件更新
  • int updateByExampleSelective(User record, UserExample example) thorws SQLException按条件更新值不为null的字段

注:如果对生成的部分表不满意,需要重新生成,需要删除所有已生成的表,然后再生成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值