Mybatis Plus使用條件構(gòu)造器增刪改查功能的實(shí)現(xiàn)方法
接口層比較好理解,它是面向web網(wǎng)絡(luò)的接口,使用http格式去調(diào)用
/** * 圖文課程管理Controller */@RestController@RequestMapping('/driver/imageCourse')public class TImageCourseController extends BaseController {@Autowired private ITImageCourseService tImageCourseService; @Autowired private TImageCourseMapper tImageCourseMapper;// 具體接口...}Service 業(yè)務(wù)層
在實(shí)際應(yīng)用中,更復(fù)雜的邏輯應(yīng)該寫在 Service 業(yè)務(wù)層方法中,在業(yè)務(wù)方法中再調(diào)用數(shù)據(jù)層方法,實(shí)現(xiàn)從 接口層-業(yè)務(wù)層-數(shù)據(jù)層 的鏈路調(diào)用關(guān)系,提高代碼的可讀性
/** * 圖文課程管理Service接口 */public interface ITImageCourseService extends IService<TImageCourse> {}
業(yè)務(wù)層實(shí)現(xiàn)
/** * 圖文課程管理Service業(yè)務(wù)層處理 */@Servicepublic class TImageCourseServiceImpl extends ServiceImpl<TImageCourseMapper, TImageCourse> implements ITImageCourseService { @Autowired private TImageCourseMapper tImageCourseMapper;}
ServiceImpl 類實(shí)現(xiàn)了 IService 接口中的方法;ServiceImpl 中的方法,本質(zhì)上是對(duì) BaseMapper 方法的封裝,同時(shí)也增加了一些 BaseMapper 類中沒有的特性,例如常用的 list() 、count() 方法
// Service方法調(diào)用了Mapper方法 只是將insert()返回轉(zhuǎn)換成了布爾值@Overridepublic boolean save(T entity) { return retBool(baseMapper.insert(entity));}Mapper 數(shù)據(jù)層
繼承 BaseMapper 接口后,無需編寫 mapper.xml 文件,即可獲得CRUD功能;例如,insert() 、 deleteById() 、updateById() 、 selectById() 等方法
如果手動(dòng)編寫數(shù)據(jù)層的sql,BaseMapper實(shí)現(xiàn)者即對(duì)應(yīng)xml中的sql方法
/** * 圖文課程管理Mapper接口 */public interface TImageCourseMapper extends BaseMapper<TImageCourse> {}
**mapper.xml **
xml內(nèi)容例子,該例子自定義了一個(gè)根據(jù)id的查詢方法,無視了刪除標(biāo)志
<?xml version='1.0' encoding='UTF-8' ?><!DOCTYPE mapperPUBLIC '-//mybatis.org//DTD Mapper 3.0//EN''http://mybatis.org/dtd/mybatis-3-mapper.dtd'><mapper namespace='com.ruoyi.mapper.TRuralInfoMapper'> <resultMap type='TRuralInfo' id='RuralInfoResult'><id property='id' column='id' /><result property='cityName' column='city_name' /><result property='countyName' column='county_name' /><result property='townName' column='town_name' /><result property='villageName' column='village_name' /><result property='checkCode' column='check_code' /><result property='parentLevel' column='parent_level' /><result property='parentId' column='parent_id' /><result property='delFlag'column='del_flag' /><result property='createBy' column='create_by' /><result property='createTime' column='create_time' /><result property='updateBy' column='update_by' /><result property='updateTime' column='update_time' /> </resultMap> <sql id='selectRuralInfoVo'>select t_rural_info.id, city_name, county_name, town_name, village_name, check_code, parent_level, parent_id,t_rural_info.del_flag, t_rural_info.create_by, t_rural_info.create_time, t_rural_info.update_by, t_rural_info.update_timefrom t_rural_info </sql> <select parameterType='Long' resultMap='RuralInfoResult'><include refid='selectRuralInfoVo'/>where id = #{id} </select></mapper>增刪改查新增(C)
使用 mapper 對(duì)象的 insert() 方法新增一條記錄,成果后會(huì)將數(shù)據(jù)庫的id返回給實(shí)體
/** * 新增圖文課程管理 */@PostMappingpublic AjaxResult add(@RequestBody TImageCourse tImageCourse){... return toAjax(tImageCourseMapper.insert(tImageCourse));}
saveBatch
service 類中提供了 saveBatch() 方法,可實(shí)現(xiàn)批量插入,該方法是支持事務(wù)
saveOrUpdate
service 類中提供了 saveOrUpdate() 方法,如果id為空則調(diào)用 save() 方法保存,反之則調(diào)用 updateById() 方法更新
查詢(R)
查詢多數(shù)要借助條件構(gòu)造器使用才有意義,實(shí)現(xiàn)更靈活的查詢;
查詢實(shí)體常用的方法有 .getOne() ,getById() ;
.getOne() 接收一個(gè)條件構(gòu)造器作為參數(shù)
getById() 根據(jù)id進(jìn)行查詢實(shí)體
查詢集合常用的查詢方法包括 .list(),
.list() 方法也可以接收一個(gè)條件構(gòu)造器作為參數(shù)
構(gòu)造器的使用
條件構(gòu)造器包含 QueryWrapper 和 LambdaQueryWrapper 兩個(gè)類。
LambdaQueryWrapper 為函數(shù)式編程的書寫習(xí)慣,與 QueryWrapper 表達(dá)的意義相同,優(yōu)點(diǎn)是簡(jiǎn)化了代碼。
此處以 LambdaQueryWrapper 的使用為例,常用的三種方法:
// 1、直接用new創(chuàng)建// 創(chuàng)建對(duì)象的方式會(huì)更加靈活,可配合 if()...else 達(dá)到更靈活的sql拼接LambdaQueryWrapper<TCenterPoint> wrapper = new LambdaQueryWrapper<>();wrapper.eq(TCenterPoint::getPoint, 10.0);// 2、靜態(tài)方法創(chuàng)建 Wrappers.<>lambdaQuery()// 構(gòu)造器方法多為鏈?zhǔn)骄幊?可連寫Wrappers.<TCenterPoint>lambdaQuery().eq(TCenterPoint::getPoint, 10.0)// 3、靜態(tài)方法創(chuàng)建 Wrappers.query() // query可接受對(duì)象 字段不為null則自動(dòng)拼接.eq()方法Wrappers.query(tUserDetail)
構(gòu)造器方法
/** * 源碼 * @param condition 執(zhí)行條件 可省略 * @param column 字段 * @param val 值 */eq(boolean condition, R column, Object val)eq 相等 = ne 不等于 != gt 大于 > ge 大于等于 >= lt 小于 < le 小于等于 <= between BETWEEN 值1 AND 值2like LIKE ‘%值%’notLike NOT LIKE ‘%值%’likeLeft LIKE ‘%值’ ; likeRight同理 isNull 字段 IS NULL;orderByAsc 排序:ORDER BY 字段, … ASC; orderByDesc同理
在sql中使用and和or,邏輯只需寫在where中即可,在ORM框架中較為不好理解,總之,其結(jié)果是實(shí)現(xiàn)一個(gè)查詢條件和多個(gè)條件并列的關(guān)系
OR
or(Consumer<Param> consumer)or(boolean condition, Consumer<Param> consumer)
OR 嵌套,例如
// or (name = ’李白’ and status <> ’活著’)or(i -> i.eq('name', '李白').ne('status', '活著'))
AND
and(Consumer<Param> consumer)and(boolean condition, Consumer<Param> consumer)
AND 嵌套,例如
// and (name = ’李白’ and status <> ’活著’)and(i -> i.eq('name', '李白').ne('status', '活著'))
修改(U)
使用 mapper 對(duì)象的 updateById() 方法更新實(shí)體,只有字段內(nèi)容不為空,才會(huì)觸發(fā)字段內(nèi)容的修改
/** * 修改圖文課程管理 */@PutMappingpublic AjaxResult edit(@RequestBody TImageCourse tImageCourse){ return toAjax(tImageCourseMapper.updateById(tImageCourse));}
刪除(D)
刪除常用的方法是根據(jù)id進(jìn)行刪除,使用 mapper 對(duì)象的 deleteById ,框架也支持批量刪除的操作 deleteBatchIds
/** * 刪除圖文課程管理 */@DeleteMapping('/{ids}')public AjaxResult remove(@PathVariable Long[] ids) { return toAjax(tImageCourseMapper.deleteBatchIds(Arrays.asList(ids)));}
到此這篇關(guān)于Mybatis-Plus使用條件構(gòu)造器增刪改查的文章就介紹到這了,更多相關(guān)Mybatis Plus條件構(gòu)造器增刪改查內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. MySQL為什么要避免大事務(wù)以及大事務(wù)解決的方法2. mybatis中bind標(biāo)簽和concat的使用說明3. 測(cè)試sql server業(yè)務(wù)規(guī)則鏈接方法4. 在SQL Server數(shù)據(jù)庫中如何減少死鎖發(fā)生5. MYSQL SQL查詢近7天一個(gè)月的數(shù)據(jù)的操作方法6. MySQL 視圖、函數(shù)和存儲(chǔ)過程詳解7. Microsoft SQL Server 查詢處理器的內(nèi)部機(jī)制與結(jié)構(gòu)(1)8. 保“庫”之路:Oracle數(shù)據(jù)庫性能保護(hù)9. 無責(zé)任Oracle圖書簡(jiǎn)評(píng) (1)10. 輕松掌握Oracle數(shù)據(jù)庫Where條件執(zhí)行順序
