溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

idea怎么使用Mybatis逆向工程插件

發(fā)布時(shí)間:2022-01-04 00:25:15 來(lái)源:億速云 閱讀:423 作者:柒染 欄目:開(kāi)發(fā)技術(shù)

idea怎么使用Mybatis逆向工程插件,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。


一、使用mybatis連接數(shù)據(jù)庫(kù)

idea怎么使用Mybatis逆向工程插件

添加連接的mysql的信息,測(cè)試鏈接成功即可。

idea怎么使用Mybatis逆向工程插件

二、安裝Better-Mybatis-Generator插件

idea怎么使用Mybatis逆向工程插件

安裝成功后,在需要生成的表上右鍵選擇mybatis-generator。

idea怎么使用Mybatis逆向工程插件

添加要生成的一些配置。

idea怎么使用Mybatis逆向工程插件

點(diǎn)擊OK,第一次生成會(huì)彈出窗口,需要輸入數(shù)據(jù)庫(kù)的帳號(hào)密碼??梢钥吹缴稍摫韺?duì)應(yīng)的mapper接口、實(shí)體類(lèi)和sql。

idea怎么使用Mybatis逆向工程插件

三、關(guān)于example類(lèi)詳解

1、example成員變量

mybatis-generator會(huì)為每個(gè)字段產(chǎn)生Criterion,為底層的mapper.xml創(chuàng)建動(dòng)態(tài)sql。如果表的字段比較多,產(chǎn)生的example類(lèi)會(huì)十分龐大。理論上通過(guò)example類(lèi)可以構(gòu)造你想到的任何篩選條件。

 //作用:升序還是降序
 //參數(shù)格式:字段+空格+asc(desc)
 protected String orderByClause;  
 //作用:去除重復(fù)
 //true是選擇不重復(fù)記錄,false,反之
 protected boolean distinct;
 //自定義查詢條件
 //Criteria的集合,集合中對(duì)象是由or連接
 protected List<Criteria> oredCriteria;
 // 分頁(yè)的顯示條數(shù)
 private Integer limit;
 // 分頁(yè)的起始下標(biāo)   
 private Long offset;
 //內(nèi)部類(lèi)Criteria包含一個(gè)Cretiron的集合,
 //每一個(gè)Criteria對(duì)象內(nèi)包含的Cretiron之間是由  AND連接的
 public static class Criteria extends GeneratedCriteria {
  protected Criteria() {super();}
 }
 //是mybatis中逆向工程中的代碼模型
 protected abstract static class GeneratedCriteria {......}
 //是最基本,最底層的Where條件,用于字段級(jí)的篩選
 public static class Criterion {......}

2、example使用

在MybatisDemoApplicationTests類(lèi)中進(jìn)行測(cè)試:

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類(lèi)使用說(shuō)明
 */
@SpringBootTest
class MybatisDemoApplicationTests {
    @Autowired
    private StudentDao studentDao;
 
    @Test
    void contextLoads() {
        StudentExample studentExample = new StudentExample();
 
        // 查詢數(shù)據(jù)的總條數(shù) 類(lèi)似于:select count(*) from student
        long l = studentDao.countByExample(studentExample);
        System.out.println("---------------總條數(shù)----------------");
        System.out.println("數(shù)據(jù)庫(kù)的總條數(shù):" + 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("-----------------分頁(yè)查詢--------------");
        selectLimit();
    }
 
    /**
     * where條件查詢或多條件查詢
     * 類(lèi)似于: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);
    }
 
    /**
     * 類(lèi)似于: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);
    }
 
    /**
     * 類(lèi)似于: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);
    }
 
    /**
     * 類(lèi)似于: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);
    }
}

運(yùn)行結(jié)果:

idea怎么使用Mybatis逆向工程插件

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI