av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

Mybatis Plus使用條件構(gòu)造器增刪改查功能的實(shí)現(xiàn)方法

瀏覽:2日期:2023-10-19 12:08:07
java后端層級(jí)結(jié)構(gòu)Controller 接口層

接口層比較好理解,它是面向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)!

標(biāo)簽: Mybatis 數(shù)據(jù)庫
相關(guān)文章:
主站蜘蛛池模板: 欧美在线免费 | 婷婷激情五月 | 中文字幕在线免费播放 | 一级片免费视频 | 欧美精品在线看 | 久久综合国产 | 精品国产欧美一区二区三区成人 | 欧美日韩在线视频观看 | 亚洲裸体视频 | 久久久一区二区 | 中文字幕av在线播放 | 久久中文网 | 国产高清视频在线 | 日韩精品极品 | 日本不卡在线视频 | 高潮一区二区三区乱码 | 欧美人xxxx| 国产欧美一区二区精品性色超碰 | 国产午夜激情 | 成人精品在线观看 | www.久久久久久 | 久久亚洲精品视频 | 久久小视频 | 午夜激情福利 | 在线视频日韩 | 操少妇视频 | 色妞色视频一区二区三区四区 | 亚洲精品1 | 日韩手机看片 | 天天射综合 | 黄色三级小说 | 夜夜草av | 91在线视频| 成人午夜激情 | 黄色av观看 | 久久综合爱| 国产免费视频 | 精品久久视频 | 国产三级在线免费观看 | 色播亚洲 | 日韩网站在线观看 |