mybatis通過if語句實(shí)現(xiàn)增刪改查操作
有時(shí)候?yàn)榱撕喕覀兊拇a。
1 舉個(gè)例子
Student類:
@Data
public class Student {
private Integer id;
private Integer age;
private Integer sno;
}
有時(shí)候我們想通過age這個(gè)屬性獲取Student對(duì)象
有時(shí)候我們也想通過sno這個(gè)屬性獲取Student對(duì)象
難道我們?cè)贒AO層寫兩個(gè)接口?
比如這樣子?
Student getStudentByAge(Int age);
Student getStudentBySno(Int sno);
那么在mapper文件中要這樣寫?
<select id="getStudentByAge" parameterType="int" resultMap="studentMap">
select * from student where age=#{age}
</select>
<select id="getStudentBySno" parameterType="int" resultMap="studentMap">
select * from student where sno=#{sno}
</select>
顯然,這樣子是不高效的
2 上手測(cè)試 實(shí)驗(yàn)
實(shí)體類 Student:
@Data
public class Student {
@ApiModelProperty(name = "id",example = "1",position = 1)
private Integer id;
@ApiModelProperty(name = "age",value = "年齡",example = "18",position = 2)
private Integer age;
@ApiModelProperty(name = "sno",value = "學(xué)號(hào)",example = "334",position = 3)
private Integer sno;
}
數(shù)據(jù)庫:
CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `age` int(11) DEFAULT NULL COMMENT '年齡', `sno` int(11) NOT NULL COMMENT '學(xué)號(hào)', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
手動(dòng)添加一些數(shù)據(jù)

Dao層:
@Mapper
public interface StudentDao {
/**
* @description: 通過student中的屬性 查詢到student
* @param: student
* @author: Yuz
* @creat_time: 2019/8/20
* @return: student
**/
Student getStudent(Student student);
/**
* @description: 通過age sno 屬性來刪除
* @param: student
* @author: Yuz
* @creat_time: 2019/8/20
* @return: void
**/
void deleteStudent(Student student);
}
Mapper
<?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.dao.StudentDao">
<resultMap id="studentMap" type="com.entity.Student">
<id property="id" column="id"/>
<result property="age" column="age"/>
<result property="sno" column="sno"/>
</resultMap>
<select id="getStudent" parameterType="com.entity.Student" resultMap="studentMap">
select * from student where
<if test="age != null">age=#{age}</if>
<if test="sno !=null">sno=#{sno}</if>
</select>
<delete id="deleteStudent" parameterType="Student">
delete from student
<where>
<if test="age != null">
age =#{age}
</if>
<if test="sno != sno">
sno=#{sno}
</if>
</where>
</delete>
</mapper>
Service層:
@Service
public class StudentService {
@Autowired
StudentDao studentDao;
/**
* @description: 通過student中的屬性 查詢到student
* @param: student
* @author: Yuz
* @creat_time: 2019/8/20
* @return: student
**/
public Student getStudent(Student student){
return studentDao.getStudent(student);
}
/**
* @description: 通過age sno 屬性來刪除
* @param: student
* @author: Yuz
* @creat_time: 2019/8/20
* @return: void
**/
public void deleteStudent(Student student){
studentDao.deleteStudent(student);
}
}
Controller:
@RestController
@Api("學(xué)生接口")
@RequestMapping("/student")
public class StudentController {
@Autowired
StudentService studentService;
/**
* @description: 通過student中的屬性 查詢到student
* @param: student
* @author: Yuz
* @creat_time: 2019/8/20
* @return: student
**/
@ApiOperation("通過屬性查詢student")
@PostMapping("/getStudent")
Student getStudent(@RequestBody Student student){
return studentService.getStudent(student);
}
/**
* @description: 通過age sno 屬性來刪除
* @param: student
* @author: Yuz
* @creat_time: 2019/8/20
* @return: void
**/
@ApiOperation("通過屬性刪除student")
@PostMapping("/delete")
public void deleteStudent(@RequestBody Student student){
studentService.deleteStudent(student);
}
}
3 直接測(cè)試
通過age屬性查詢student:成功

通過sno屬性查詢:

通過屬性age刪除Student:


通過sno屬性刪除Student


補(bǔ)充知識(shí):mybatis使用if條件判斷,數(shù)字類型不能寫 0 !=‘',否則會(huì)進(jìn)不到條件拼接里面
1.對(duì)于 if條件判斷:數(shù)字類型屬性判斷的時(shí)候
注意不可以是這種情況
<if test="delFlag!= null and delFlag!= ''">
and del_flag = #{delFlag}
</if>
參數(shù)一個(gè)是0,一個(gè)是"",最終debug會(huì)走進(jìn)case 8 里面,0和“”都會(huì)被轉(zhuǎn)成double進(jìn)行比較,都會(huì)變成0.0,這就是mybati中if test 0!=""判定為false的原因
以上這篇mybatis通過if語句實(shí)現(xiàn)增刪改查操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring MVC過濾器-登錄過濾的代碼實(shí)現(xiàn)
本篇文章主要介紹了Spring MVC過濾器-登錄過濾,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧。2017-01-01
Java Json字符串的雙引號(hào)("")括號(hào)如何去掉
這篇文章主要介紹了Java Json字符串的雙引號(hào)("")括號(hào)如何去掉?具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Spring?Boot整合Zookeeper實(shí)現(xiàn)分布式鎖的場(chǎng)景分析
這篇文章主要介紹了Spring?Boot整合Zookeeper實(shí)現(xiàn)分布式鎖,zk實(shí)現(xiàn)分布式鎖完全是依靠zk節(jié)點(diǎn)類型當(dāng)中的臨時(shí)序號(hào)節(jié)點(diǎn)來實(shí)現(xiàn)的,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
spring?boot?實(shí)現(xiàn)一個(gè)?禁止重復(fù)請(qǐng)求的方法
這篇文章主要介紹了spring?boot?實(shí)現(xiàn)一個(gè)?禁止重復(fù)請(qǐng)求,當(dāng)重復(fù)請(qǐng)求該方法時(shí),會(huì)返回"Duplicate?request",避免重復(fù)執(zhí)行相同的操作,需要的朋友可以參考下2024-03-03
MyBatis實(shí)現(xiàn)動(dòng)態(tài)SQL的實(shí)現(xiàn)方法
這篇文章主要介紹了MyBatis實(shí)現(xiàn)動(dòng)態(tài)SQL的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
java報(bào)錯(cuò)Cause: java.sql.SQLException問題解決
本文主要介紹了java報(bào)錯(cuò)Cause: java.sql.SQLException問題解決,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08
基于Spring Boot不同的環(huán)境使用不同的配置方法
下面小編就為大家分享一篇基于Spring Boot不同的環(huán)境使用不同的配置方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01

