- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- Mybatis Plus使用條件構造器增刪改查功能的實(shí)現方
接口層比較好理解,它是面向web網(wǎng)絡(luò )的接口,使用http格式去調用
/** * 圖文課程管理Controller */ @RestController @RequestMapping("/driver/imageCourse") public class TImageCourseController extends BaseController { @Autowired private ITImageCourseService tImageCourseService; @Autowired private TImageCourseMapper tImageCourseMapper; // 具體接口... }
在實(shí)際應用中,更復雜的邏輯應該寫(xiě)在 Service
業(yè)務(wù)層方法中,在業(yè)務(wù)方法中再調用數據層方法,實(shí)現從 接口層-業(yè)務(wù)層-數據層
的鏈路調用關(guān)系,提高代碼的可讀性
/** * 圖文課程管理Service接口 */ public interface ITImageCourseService extends IService<TImageCourse> { }
業(yè)務(wù)層實(shí)現
/** * 圖文課程管理Service業(yè)務(wù)層處理 */ @Service public class TImageCourseServiceImpl extends ServiceImpl<TImageCourseMapper, TImageCourse> implements ITImageCourseService { @Autowired private TImageCourseMapper tImageCourseMapper; }
ServiceImpl
類(lèi)實(shí)現了 IService
接口中的方法;ServiceImpl
中的方法,本質(zhì)上是對 BaseMapper
方法的封裝,同時(shí)也增加了一些 BaseMapper
類(lèi)中沒(méi)有的特性,例如常用的 list()
、count()
方法
// Service方法調用了Mapper方法 只是將insert()返回轉換成了布爾值 @Override public boolean save(T entity) { return retBool(baseMapper.insert(entity)); }
繼承 BaseMapper
接口后,無(wú)需編寫(xiě) mapper.xml 文件,即可獲得CRUD功能;例如,insert()
、 deleteById()
、updateById()
、 selectById()
等方法
如果手動(dòng)編寫(xiě)數據層的sql,BaseMapper實(shí)現者即對應xml中的sql方法
/** * 圖文課程管理Mapper接口 */ public interface TImageCourseMapper extends BaseMapper<TImageCourse> { }
**mapper.xml **
xml內容例子,該例子自定義了一個(gè)根據id的查詢(xún)方法,無(wú)視了刪除標志
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//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_time from t_rural_info </sql> <select id="getRuralInfoById" parameterType="Long" resultMap="RuralInfoResult"> <include refid="selectRuralInfoVo"/> where id = #{id} </select> </mapper>
使用 mapper
對象的 insert()
方法新增一條記錄,成果后會(huì )將數據庫的id返回給實(shí)體
/** * 新增圖文課程管理 */ @PostMapping public AjaxResult add(@RequestBody TImageCourse tImageCourse) { ... return toAjax(tImageCourseMapper.insert(tImageCourse)); }
saveBatch
service
類(lèi)中提供了 saveBatch()
方法,可實(shí)現批量插入,該方法是支持事務(wù)
saveOrUpdate
service
類(lèi)中提供了 saveOrUpdate()
方法,如果id為空則調用 save()
方法保存,反之則調用 updateById()
方法更新
查詢(xún)(R)
查詢(xún)多數要借助條件構造器使用才有意義,實(shí)現更靈活的查詢(xún);
常用的方法有 .getOne()
,getById()
;
.getOne()
接收一個(gè)條件構造器作為參數
getById()
根據id進(jìn)行查詢(xún)實(shí)體
常用的查詢(xún)方法包括 .list()
,
.list()
方法也可以接收一個(gè)條件構造器作為參數
構造器的使用
條件構造器包含 QueryWrapper
和 LambdaQueryWrapper
兩個(gè)類(lèi)。
LambdaQueryWrapper
為函數式編程的書(shū)寫(xiě)習慣,與 QueryWrapper
表達的意義相同,優(yōu)點(diǎn)是簡(jiǎn)化了代碼。
此處以 LambdaQueryWrapper
的使用為例,常用的三種方法:
// 1、直接用new創(chuàng )建 // 創(chuàng )建對象的方式會(huì )更加靈活,可配合 if()...else 達到更靈活的sql拼接 LambdaQueryWrapper<TCenterPoint> wrapper = new LambdaQueryWrapper<>(); wrapper.eq(TCenterPoint::getPoint, 10.0); // 2、靜態(tài)方法創(chuàng )建 Wrappers.<>lambdaQuery() // 構造器方法多為鏈式編程 可連寫(xiě) Wrappers.<TCenterPoint>lambdaQuery().eq(TCenterPoint::getPoint, 10.0) // 3、靜態(tài)方法創(chuàng )建 Wrappers.query() // query可接受對象 字段不為null則自動(dòng)拼接.eq()方法 Wrappers.query(tUserDetail)
構造器方法
/** * 源碼 * @param condition 執行條件 可省略 * @param column 字段 * @param val 值 */ eq(boolean condition, R column, Object val)
在sql中使用and和or,邏輯只需寫(xiě)在where中即可,在ORM框架中較為不好理解,總之,其結果是實(shí)現一個(gè)查詢(xún)條件和多個(gè)條件并列的關(guān)系
OR
or(Consumer<Param> consumer) or(boolean condition, Consumer<Param> consumer)
OR 嵌套,例如
// or (name = '李白' and status <> '活著(zhù)') or(i -> i.eq("name", "李白").ne("status", "活著(zhù)"))
AND
and(Consumer<Param> consumer) and(boolean condition, Consumer<Param> consumer)
AND 嵌套,例如
// and (name = '李白' and status <> '活著(zhù)') and(i -> i.eq("name", "李白").ne("status", "活著(zhù)"))
修改(U)
使用 mapper
對象的 updateById()
方法更新實(shí)體,只有字段內容不為空,才會(huì )觸發(fā)字段內容的修改
/** * 修改圖文課程管理 */ @PutMapping public AjaxResult edit(@RequestBody TImageCourse tImageCourse) { return toAjax(tImageCourseMapper.updateById(tImageCourse)); }
刪除(D)
刪除常用的方法是根據id進(jìn)行刪除,使用 mapper
對象的 deleteById
,框架也支持批量刪除的操作 deleteBatchIds
/** * 刪除圖文課程管理 */ @DeleteMapping("/{ids}") public AjaxResult remove(@PathVariable Long[] ids) { return toAjax(tImageCourseMapper.deleteBatchIds(Arrays.asList(ids))); }
到此這篇關(guān)于Mybatis-Plus使用條件構造器增刪改查的文章就介紹到這了,更多相關(guān)Mybatis Plus條件構造器增刪改查內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站