idea使用Mybatis逆向工程插件詳情
一、使用mybatis連接數(shù)據(jù)庫(kù)

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

二、安裝Better-Mybatis-Generator插件

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

添加要生成的一些配置。

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

三、關(guān)于example類詳解
1、example成員變量
mybatis-generator會(huì)為每個(gè)字段產(chǎn)生Criterion,為底層的mapper.xml創(chuàng)建動(dòng)態(tài)sql。如果表的字段比較多,產(chǎn)生的example類會(huì)十分龐大。理論上通過(guò)example類可以構(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)部類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類中進(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類使用說(shuō)明
?*/
@SpringBootTest
class MybatisDemoApplicationTests {
? ? @Autowired
? ? private StudentDao studentDao;
?
? ? @Test
? ? void contextLoads() {
? ? ? ? StudentExample studentExample = new StudentExample();
?
? ? ? ? // 查詢數(shù)據(jù)的總條數(shù) 類似于: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條件查詢或多條件查詢
? ? ?* 類似于: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);
? ? }
}運(yùn)行結(jié)果:

官方文檔:MyBatis Generator Core – Example Class Usage Notes
到此這篇關(guān)于idea使用Mybatis逆向工程插件詳情的文章就介紹到這了,更多相關(guān)idea使用Mybatis逆向工程插件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java環(huán)境中MyBatis與Spring或Spring MVC框架的集成方法
和MyBatis類似,Spring或者Spring MVC框架在Web應(yīng)用程序的運(yùn)作中同樣主要負(fù)責(zé)處理數(shù)據(jù)庫(kù)事務(wù),這里我們就來(lái)看一下Java環(huán)境中MyBatis與Spring或Spring MVC框架的集成方法2016-06-06
JAVA基礎(chǔ)--如何通過(guò)異常處理錯(cuò)誤
這篇文章主要介紹了JAVA中如何通過(guò)異常處理錯(cuò)誤,文中講解非常細(xì)致,代碼幫助大家更好的理解,感興趣的朋友可以了解下2020-06-06
一個(gè)簡(jiǎn)單JDK版動(dòng)態(tài)代理
這篇文章主要為大家詳細(xì)介紹了一個(gè)簡(jiǎn)單JDK版動(dòng)態(tài)代理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
Mybatis模糊查詢和動(dòng)態(tài)sql語(yǔ)句的用法
今天小編就為大家分享一篇關(guān)于Mybatis模糊查詢和動(dòng)態(tài)sql語(yǔ)句的用法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
SpringBoot整合RestTemplate用法的實(shí)現(xiàn)
本篇主要介紹了RestTemplate中的GET,POST,PUT,DELETE、文件上傳和文件下載6大常用的功能,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08

