亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2021年05月12日 09:30:29   作者:旺卡先生  
這篇文章主要介紹了Mybatis-Plus使用條件構(gòu)造器增刪改查,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

java后端層級結(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ù)層處理
 */
@Service
public class TImageCourseServiceImpl extends ServiceImpl<TImageCourseMapper, TImageCourse> implements ITImageCourseService {
    @Autowired
    private TImageCourseMapper tImageCourseMapper;

}

ServiceImpl 類實(shí)現(xiàn)了 IService 接口中的方法;ServiceImpl 中的方法,本質(zhì)上是對 BaseMapper 方法的封裝,同時也增加了一些 BaseMapper 類中沒有的特性,例如常用的 list() 、count() 方法

// Service方法調(diào)用了Mapper方法 只是將insert()返回轉(zhuǎn)換成了布爾值
@Override
public boolean save(T entity) {
    return retBool(baseMapper.insert(entity));
}

Mapper 數(shù)據(jù)層

繼承 BaseMapper 接口后,無需編寫 mapper.xml 文件,即可獲得CRUD功能;例如,insert() 、 deleteById() 、updateById() 、 selectById() 等方法

如果手動編寫數(shù)據(jù)層的sql,BaseMapper實(shí)現(xiàn)者即對應(yīng)xml中的sql方法

/**
 * 圖文課程管理Mapper接口
 */
public interface TImageCourseMapper extends BaseMapper<TImageCourse> {
}

**mapper.xml **

xml內(nèi)容例子,該例子自定義了一個根據(jù)id的查詢方法,無視了刪除標(biāo)志

<?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>

增刪改查


新增(C)

使用 mapper 對象的 insert() 方法新增一條記錄,成果后會將數(shù)據(jù)庫的id返回給實(shí)體

/**
 * 新增圖文課程管理
 */
@PostMapping
public 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òu)造器作為參數(shù)

getById() 根據(jù)id進(jìn)行查詢實(shí)體

查詢集合

常用的查詢方法包括 .list()

.list() 方法也可以接收一個條件構(gòu)造器作為參數(shù)

構(gòu)造器的使用

條件構(gòu)造器包含 QueryWrapperLambdaQueryWrapper 兩個類。

LambdaQueryWrapper 為函數(shù)式編程的書寫習(xí)慣,與 QueryWrapper 表達(dá)的意義相同,優(yōu)點(diǎn)是簡化了代碼。

此處以 LambdaQueryWrapper 的使用為例,常用的三種方法:

// 1、直接用new創(chuàng)建
// 創(chuàng)建對象的方式會更加靈活,可配合 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可接受對象 字段不為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 值2
like LIKE ‘%值%'
notLike NOT LIKE ‘%值%'
likeLeft LIKE ‘%值' ; likeRight同理
isNull 字段 IS NULL;
orderByAsc 排序:ORDER BY 字段, … ASC; orderByDesc同理

在sql中使用and和or,邏輯只需寫在where中即可,在ORM框架中較為不好理解,總之,其結(jié)果是實(shí)現(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() 方法更新實(shí)體,只有字段內(nèi)容不為空,才會觸發(fā)字段內(nèi)容的修改

/**
 * 修改圖文課程管理
 */
@PutMapping
public AjaxResult edit(@RequestBody TImageCourse tImageCourse)
{
    return toAjax(tImageCourseMapper.updateById(tImageCourse));
}

刪除(D)

刪除常用的方法是根據(jù)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使用條件構(gòu)造器增刪改查的文章就介紹到這了,更多相關(guān)Mybatis Plus條件構(gòu)造器增刪改查內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論