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

2022-01-24 0 467
目录

一、使用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 

:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!

猪小侠源码-最新源码下载平台 Java教程 idea使用Mybatis逆向工程插件详情 http://www.20zxx.cn/297552/xuexijiaocheng/javajc.html

猪小侠源码,优质资源分享网

常见问题
  • 本站所有资源版权均属于原作者所有,均只能用于参考学习,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,建议提前注册好百度网盘账号,使用百度网盘客户端下载
查看详情

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务