MyBatis的逆向工程


  • 所谓的逆向⼯程是:根据数据库表逆向⽣成 Java的pojo类SqlMapper.xml⽂件,以及Mapper接⼝类等。
  • 要完成这个⼯作,需要借助别⼈写好的逆向⼯程插件。
  • 思考:使⽤这个插件的话,需要给这个插件配置哪些信息?
    • pojo类名、包名以及⽣成位置。
    • SqlMapper.xml⽂件名以及⽣成位置。
    • Mapper接⼝名以及⽣成位置。
    • 连接数据库的信息。
    • 指定哪些表参与逆向⼯程。

一、逆向⼯程配置与⽣成

1.在pom中添加逆向⼯程插件

<!--定制构建过程-->
<build>
    <!--可配置多个插件-->
    <plugins>
        <!--其中的⼀个插件:mybatis逆向⼯程插件-->
        <plugin>
            <!--插件的GAV坐标-->
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.4.1</version>
            <!--允许覆盖-->
            <configuration>
                <overwrite>true</overwrite>
            </configuration>
            <!--插件的依赖-->
            <dependencies>
                <!--mysql驱动依赖-->
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>8.0.33</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

2.配置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>
    <!--
    targetRuntime有两个值:
    MyBatis3Simple:⽣成的是基础版,只有基本的增删改查。
    MyBatis3:⽣成的是增强版,除了基本的增删改查之外还有复杂的增删改查。
    -->
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!--防⽌⽣成重复代码-->
        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"/>

        <commentGenerator>
            <!--是否去掉⽣成⽇期-->
            <property name="suppressDate" value="true"/>
            <!--是否去除注释-->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--连接数据库信息-->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/数据库名"
                        userId="用户名"
                        password="密码">
        </jdbcConnection>
        <!-- ⽣成pojo包名和位置 -->
        <javaModelGenerator targetPackage="com.gdb.generate.pojo" targetProject="src/main/java">
            <!--是否开启⼦包-->
            <property name="enableSubPackages" value="true"/>
            <!--是否去除字段名的前后空⽩-->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- ⽣成SQL映射⽂件的包名和位置 -->
        <sqlMapGenerator targetPackage="com.gdb.generate.mapper" targetProject="src/main/java">
            <!--是否开启⼦包-->

            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- ⽣成Mapper接⼝的包名和位置 -->
        <javaClientGenerator
                type="xmlMapper"
                targetPackage="com.gdb.generate.mapper"
                targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 表名和对应的实体类名-->
        <table tableName="article" domainObjectName="Article"
            enableCountByExample="false"
            enableDeleteByExample="false"
            enableSelectByExample="false"
            enableUpdateByExample="false"
            selectByExampleQueryId="false">
            <!--上面的几个配置项是不生成封装查询条件的类-->
        </table>
    </context>
</generatorConfiguration>

3.运⾏插件

在这里插入图片描述

4.结果展示

在这里插入图片描述


二、测试逆向⼯程⽣成的是否好⽤

1.准备环境

  • 添加依赖:mybatis依赖、mysql驱动依赖、junit依赖
  • jdbc.properties
  • mybatis-config.xml
  • pom.xml 中添加resource配置
    • 因为 maven 默认不会编译源代码中的 *.xml 文件,需要配置 resource。
<build>
  <!--配置maven在构建项目的时候除了编译Java源文件以外也编译所有的.xml文件-->
  <resources>
    <resource>
      <directory>src/main/java</directory>
      <includes>
        <include>**/*.xml</include>
      </includes>
    </resource>
    <resource>
      <directory>src/main/resources</directory>
      <includes>
        <include>**/*.*</include>
      </includes>
    </resource>
  </resources>
</build>

2.编写测试程序

import com.gdb.generate.mapper.ArticleMapper;
import com.gdb.generate.pojo.ArticleExample;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import com.gdb.generate.pojo.Article;

import java.util.List;

public class GeneratorTest {
   
    @Test
    public void testGenerator() throws Exception {
   
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);
        // 增
//        Article article = new Article(123, 2354, "springboot 配置文件", "外部化配置文件", 12346, new Date(), new Date());
//        int count = mapper.insert(article);
//        System.out.println("插⼊了⼏条记录:" + count);

        // 删
//        int count = mapper.deleteByPrimaryKey(123);
//        System.out.println("删除了⼏条记录:" + count);

        // 改
        // 根据主键修改
//        Article article = new Article(123, 2354, "springboot 配置文件", "外部化配置文件", 12346, new Date(), new Date());
//        int count = mapper.updateByPrimaryKey(article);
//        System.out.println("更新了⼏条记录:" + count);

        // 查⼀个
        Article article = mapper.selectByPrimaryKey(1);
        System.out.println(article);
        // 查所有
        List<Article> cars = mapper.selectByExample(null);
        cars.forEach(c -> System.out.println(c));
        System.out.println("=======================");
        // 多条件查询
        // QBC ⻛格:Query By Criteria ⼀种查询⽅式,⽐较⾯向对象,看不到sql语句。
        ArticleExample articleExample = new ArticleExample();
        articleExample.createCriteria().andTitleEqualTo("JavaWeb")
                .andUserIdBetween(9752, 99999);
        articleExample.or().andCreateTimeIsNotNull();
        mapper.selectByExample(articleExample);
        
        sqlSession.commit();
        sqlSession.close();
    }
}

相关推荐

  1. spring和Mybatis逆向工程

    2024-02-23 00:32:01       7 阅读
  2. Mybatis逆向工程

    2024-02-23 00:32:01       43 阅读
  3. MyBatis数据库逆向生成工具

    2024-02-23 00:32:01       11 阅读
  4. android 逆向工程(待续)

    2024-02-23 00:32:01       41 阅读
  5. MyBatisPlus逆向工程

    2024-02-23 00:32:01       28 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-23 00:32:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-23 00:32:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-23 00:32:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-23 00:32:01       20 阅读

热门阅读

  1. 去年面试的运维开发面试题二

    2024-02-23 00:32:01       35 阅读
  2. 【Kuiperinfer】笔记02 GoogleTest入门

    2024-02-23 00:32:01       29 阅读
  3. springboot+vue项目基础开发(16)主页面布局

    2024-02-23 00:32:01       26 阅读
  4. 抖音半蓝V商家电话采集软件使用教程

    2024-02-23 00:32:01       129 阅读
  5. Linux ip route命令

    2024-02-23 00:32:01       31 阅读
  6. Linux C++ 字符编码转换 GBK与UTF8互转

    2024-02-23 00:32:01       30 阅读