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

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

二、安裝Better-Mybatis-Generator插件

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

添加要生成的一些配置。

點擊OK,第一次生成會彈出窗口,需要輸入數(shù)據(jù)庫的帳號密碼??梢钥吹缴稍摫韺膍apper接口、實體類和sql。

三、關于example類詳解
1、example成員變量
mybatis-generator會為每個字段產(chǎn)生Criterion,為底層的mapper.xml創(chuàng)建動態(tài)sql。如果表的字段比較多,產(chǎn)生的example類會十分龐大。理論上通過example類可以構造你想到的任何篩選條件。
?//作用:升序還是降序
?//參數(shù)格式:字段+空格+asc(desc)
?protected String orderByClause; ?
?//作用:去除重復
?//true是選擇不重復記錄,false,反之
?protected boolean distinct;
?//自定義查詢條件
?//Criteria的集合,集合中對象是由or連接
?protected List<Criteria> oredCriteria;
?// 分頁的顯示條數(shù)
?private Integer limit;
?// 分頁的起始下標 ??
?private Long offset;
?//內(nèi)部類Criteria包含一個Cretiron的集合,
?//每一個Criteria對象內(nèi)包含的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();
?
? ? ? ? // 查詢數(shù)據(jù)的總條數(shù) 類似于:select count(*) from student
? ? ? ? long l = studentDao.countByExample(studentExample);
? ? ? ? System.out.println("---------------總條數(shù)----------------");
? ? ? ? System.out.println("數(shù)據(jù)庫的總條數(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("-----------------分頁查詢--------------");
? ? ? ? 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逆向工程插件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java環(huán)境中MyBatis與Spring或Spring MVC框架的集成方法
和MyBatis類似,Spring或者Spring MVC框架在Web應用程序的運作中同樣主要負責處理數(shù)據(jù)庫事務,這里我們就來看一下Java環(huán)境中MyBatis與Spring或Spring MVC框架的集成方法2016-06-06
SpringBoot整合RestTemplate用法的實現(xiàn)
本篇主要介紹了RestTemplate中的GET,POST,PUT,DELETE、文件上傳和文件下載6大常用的功能,具有一定的參考價值,感興趣的可以了解一下2023-08-08

