idea使用Mybatis逆向工程插件详情

 更新时间:2022年01月25日 15:48:08   作者:游王子  
这篇文章主要介绍了idea使用Mybatis逆向工程插件详情,首先使用mybatis连接数据库接着添加连接的mysql的信息,测试链接等过程,更多过程了解请参考下面文章的详细内容

一、使用mybatis连接数据库

添加连接的mysql的信息,测试链接成功即可。

二、安装Better-Mybatis-Generator插件

安装成功后,在需要生成的表上右键选择mybatis-generator。

添加要生成的一些配置。

点击OK,第一次生成会弹出窗口,需要输入数据库的帐号密码。可以看到生成该表对应的mapper接口、实体类和sql

三、关于example类详解

1、example成员变量

mybatis-generator会为每个字段产生Criterion,为底层的mapper.xml创建动态sql。如果表的字段比较多,产生的example类会十分庞大。理论上通过example类可以构造你想到的任何筛选条件。

 //作用:升序还是降序
 //参数格式:字段+空格+asc(desc)
 protected String orderByClause;  
 //作用:去除重复
 //true是选择不重复记录,false,反之
 protected boolean distinct;
 //自定义查询条件
 //Criteria的集合,集合中对象是由or连接
 protected List<Criteria> oredCriteria;
 // 分页的显示条数
 private Integer limit;
 // 分页的起始下标   
 private Long offset;
 //内部类Criteria包含一个Cretiron的集合,
 //每一个Criteria对象内包含的Cretiron之间是由  AND连接的
 public static class Criteria extends GeneratedCriteria {
  protected Criteria() {super();}
 }
 //是mybatis中逆向工程中的代码模型
 protected abstract static class GeneratedCriteria {......}
 //是最基本,最底层的Where条件,用于字段级的筛选
 public static class Criterion {......}

2、example使用

在MybatisDemoApplicationTests类中进行测试:

package org.ywz.test;
 
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.ywz.dao.StudentDao;
import org.ywz.pojo.Student;
import org.ywz.pojo.StudentExample;
 
import java.util.List;
 
/**
 * Example类使用说明
 */
@SpringBootTest
class MybatisDemoApplicationTests {
    @Autowired
    private StudentDao studentDao;
 
    @Test
    void contextLoads() {
        StudentExample studentExample = new StudentExample();
 
        // 查询数据的总条数 类似于:select count(*) from student
        long l = studentDao.countByExample(studentExample);
        System.out.println("---------------总条数----------------");
        System.out.println("数据库的总条数:" + l);
        System.out.println("----------------and条件---------------");
        // where条件查询或多条件查询
        Student student = new Student();
        student.setName("王五");
        student.setSex("男");
        selectAndCondition(student);
        System.out.println("---------------or条件----------------");
        selectOrCondition(student);
        System.out.println("-----------------模糊查询--------------");
        student.setName("王");
        selectLikeCondition(student);
        System.out.println("-----------------分页查询--------------");
        selectLimit();
    }
 
    /**
     * where条件查询或多条件查询
     * 类似于:select * from student where name={#student.name} and sex={#student.sex} order by score asc;
     *
     * @param student
     */
    private void selectAndCondition(Student student) {
        StudentExample studentExample = new StudentExample();
        StudentExample.Criteria criteria = studentExample.createCriteria();
        studentExample.setOrderByClause("score asc"); //升序
        studentExample.setDistinct(false); //不去重
        if (StringUtils.isNotBlank(student.getName())) {
            criteria.andNameEqualTo(student.getName());
        }
        if (StringUtils.isNotBlank(student.getSex())) {
            criteria.andSexEqualTo(student.getSex());
        }
        List<Student> students = studentDao.selectByExample(studentExample);
        students.forEach(System.out::println);
    }
 
    /**
     * 类似于:select * from student where name={#student.name} or sex={#student.sex} ;
     *
     * @param student
     */
    private void selectOrCondition(Student student) {
        StudentExample studentExample = new StudentExample();
        StudentExample.Criteria criteria1 = studentExample.createCriteria();
        StudentExample.Criteria criteria2 = studentExample.createCriteria();
        if (StringUtils.isNotBlank(student.getName())) {
            criteria1.andNameEqualTo(student.getName());
        }
        if (StringUtils.isNotBlank(student.getSex())) {
            criteria2.andSexEqualTo(student.getSex());
        }
        studentExample.or(criteria2);
        List<Student> students = studentDao.selectByExample(studentExample);
        students.forEach(System.out::println);
    }
 
    /**
     * 类似于:select * from student where name like %{#student.name}%
     *
     * @param student
     */
    private void selectLikeCondition(Student student) {
        StudentExample studentExample = new StudentExample();
        StudentExample.Criteria criteria = studentExample.createCriteria();
        if (StringUtils.isNotBlank(student.getName())) {
            criteria.andNameLike("%" + student.getName() + "%");
        }
        List<Student> students = studentDao.selectByExample(studentExample);
        students.forEach(System.out::println);
    }
 
    /**
     * 类似于:select * from student limit offset,limit
     */
    public void selectLimit() {
        StudentExample studentExample = new StudentExample();
        studentExample.setOffset(2l);
        studentExample.setLimit(5);
        List<Student> students = studentDao.selectByExample(studentExample);
        students.forEach(System.out::println);
    }
}

运行结果:

 官方文档:MyBatis Generator Core – Example Class Usage Notes 

到此这篇关于idea使用Mybatis逆向工程插件详情的文章就介绍到这了,更多相关idea使用Mybatis逆向工程插件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java中内存溢出和内存泄漏如何解决

    Java中内存溢出和内存泄漏如何解决

    ‌内存溢出‌和‌内存泄漏‌是两种常见的内存管理问题,它们都会对程序的性能产生负面影响,本文主要介绍了Java中的内存溢出和内存泄漏问题解决,感兴趣的可以了解一下
    2024-12-12
  • Java读取数据库表的示例代码

    Java读取数据库表的示例代码

    这篇文章主要介绍了Java读取数据库表,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-05-05
  • Java环境中MyBatis与Spring或Spring MVC框架的集成方法

    Java环境中MyBatis与Spring或Spring MVC框架的集成方法

    和MyBatis类似,Spring或者Spring MVC框架在Web应用程序的运作中同样主要负责处理数据库事务,这里我们就来看一下Java环境中MyBatis与Spring或Spring MVC框架的集成方法
    2016-06-06
  • JAVA基础--如何通过异常处理错误

    JAVA基础--如何通过异常处理错误

    这篇文章主要介绍了JAVA中如何通过异常处理错误,文中讲解非常细致,代码帮助大家更好的理解,感兴趣的朋友可以了解下
    2020-06-06
  • 一个简单JDK版动态代理

    一个简单JDK版动态代理

    这篇文章主要为大家详细介绍了一个简单JDK版动态代理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-09-09
  • 详解Spring注解 @Configuration

    详解Spring注解 @Configuration

    @Configuration 是 Spring 中的一个注解,它用于标记一个类为配置类,通过配置类可以定义和组装 Spring Bean,并且支持高度灵活的配置方式。本问详细介绍了Spring注解 @Configuration,感兴趣的小伙伴可以参考一下
    2023-04-04
  • Mybatis模糊查询和动态sql语句的用法

    Mybatis模糊查询和动态sql语句的用法

    今天小编就为大家分享一篇关于Mybatis模糊查询和动态sql语句的用法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • Java协程编程之Loom

    Java协程编程之Loom

    这篇文章主要介绍了Java协程编程Loom的方法,需要的朋友请看下文
    2021-08-08
  • Spring事务管理零基础入门

    Spring事务管理零基础入门

    事务的作用就是为了保证用户的每一个操作都是可靠的,事务中的每一步操作都必须成功执行,只要有发生异常就 回退到事务开始未进行操作的状态。事务管理是Spring框架中最为常用的功能之一,我们在使用Spring Boot开发应用时,大部分情况下也都需要使用事务
    2022-10-10
  • SpringBoot整合RestTemplate用法的实现

    SpringBoot整合RestTemplate用法的实现

    本篇主要介绍了RestTemplate中的GET,POST,PUT,DELETE、文件上传和文件下载6大常用的功能,具有一定的参考价值,感兴趣的可以了解一下
    2023-08-08

最新评论