Mybatis Plus使用條件構(gòu)造器增刪改查功能的實現(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ù)層
在實際應(yīng)用中,更復(fù)雜的邏輯應(yīng)該寫在 Service 業(yè)務(wù)層方法中,在業(yè)務(wù)方法中再調(diào)用數(shù)據(jù)層方法,實現(xiàn)從 接口層-業(yè)務(wù)層-數(shù)據(jù)層 的鏈路調(diào)用關(guān)系,提高代碼的可讀性
/** * 圖文課程管理Service接口 */public interface ITImageCourseService extends IService<TImageCourse> {}
業(yè)務(wù)層實現(xiàn)
/** * 圖文課程管理Service業(yè)務(wù)層處理 */@Servicepublic class TImageCourseServiceImpl extends ServiceImpl<TImageCourseMapper, TImageCourse> implements ITImageCourseService { @Autowired private TImageCourseMapper tImageCourseMapper;}
ServiceImpl 類實現(xiàn)了 IService 接口中的方法;ServiceImpl 中的方法,本質(zhì)上是對 BaseMapper 方法的封裝,同時也增加了一些 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() 等方法
如果手動編寫數(shù)據(jù)層的sql,BaseMapper實現(xiàn)者即對應(yīng)xml中的sql方法
/** * 圖文課程管理Mapper接口 */public interface TImageCourseMapper extends BaseMapper<TImageCourse> {}
**mapper.xml **
xml內(nèi)容例子,該例子自定義了一個根據(jù)id的查詢方法,無視了刪除標志
<?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 對象的 insert() 方法新增一條記錄,成果后會將數(shù)據(jù)庫的id返回給實體
/** * 新增圖文課程管理 */@PostMappingpublic AjaxResult add(@RequestBody TImageCourse tImageCourse){... return toAjax(tImageCourseMapper.insert(tImageCourse));}
saveBatch
service 類中提供了 saveBatch() 方法,可實現(xiàn)批量插入,該方法是支持事務(wù)
saveOrUpdate
service 類中提供了 saveOrUpdate() 方法,如果id為空則調(diào)用 save() 方法保存,反之則調(diào)用 updateById() 方法更新
查詢(R)
查詢多數(shù)要借助條件構(gòu)造器使用才有意義,實現(xiàn)更靈活的查詢;
查詢實體常用的方法有 .getOne() ,getById() ;
.getOne() 接收一個條件構(gòu)造器作為參數(shù)
getById() 根據(jù)id進行查詢實體
查詢集合常用的查詢方法包括 .list(),
.list() 方法也可以接收一個條件構(gòu)造器作為參數(shù)
構(gòu)造器的使用
條件構(gòu)造器包含 QueryWrapper 和 LambdaQueryWrapper 兩個類。
LambdaQueryWrapper 為函數(shù)式編程的書寫習慣,與 QueryWrapper 表達的意義相同,優(yōu)點是簡化了代碼。
此處以 LambdaQueryWrapper 的使用為例,常用的三種方法:
// 1、直接用new創(chuàng)建// 創(chuàng)建對象的方式會更加靈活,可配合 if()...else 達到更靈活的sql拼接LambdaQueryWrapper<TCenterPoint> wrapper = new LambdaQueryWrapper<>();wrapper.eq(TCenterPoint::getPoint, 10.0);// 2、靜態(tài)方法創(chuàng)建 Wrappers.<>lambdaQuery()// 構(gòu)造器方法多為鏈式編程 可連寫Wrappers.<TCenterPoint>lambdaQuery().eq(TCenterPoint::getPoint, 10.0)// 3、靜態(tài)方法創(chuàng)建 Wrappers.query() // query可接受對象 字段不為null則自動拼接.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é)果是實現(xiàn)一個查詢條件和多個條件并列的關(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 對象的 updateById() 方法更新實體,只有字段內(nèi)容不為空,才會觸發(fā)字段內(nèi)容的修改
/** * 修改圖文課程管理 */@PutMappingpublic AjaxResult edit(@RequestBody TImageCourse tImageCourse){ return toAjax(tImageCourseMapper.updateById(tImageCourse));}
刪除(D)
刪除常用的方法是根據(jù)id進行刪除,使用 mapper 對象的 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)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. MySql中sql語句執(zhí)行過程詳細講解2. MYSQL(電話號碼,身份證)數(shù)據(jù)脫敏的實現(xiàn)3. SQL Server數(shù)據(jù)庫創(chuàng)建表及其約束條件的操作方法4. SQL語句中的ON DUPLICATE KEY UPDATE使用5. MySQL5.7 mysqldump備份與恢復(fù)的實現(xiàn)6. mysql查詢的控制語句圖文詳解7. Windows系統(tǒng)徹底卸載SQL Server通用方法(推薦!)8. Sql在多張表中檢索數(shù)據(jù)的方法詳解9. Mysql分組排序取每組第一條的2種實現(xiàn)方式10. 根據(jù)IP跳轉(zhuǎn)到用戶所在城市的實現(xiàn)步驟
