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

mybatis通過if語句實(shí)現(xiàn)增刪改查操作

 更新時間:2020年11月27日 11:55:06   作者:有個機(jī)車夢  
這篇文章主要介紹了mybatis通過if語句實(shí)現(xiàn)增刪改查操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

有時候?yàn)榱撕喕覀兊拇a。

1 舉個例子

Student類:

@Data
public class Student {
 private Integer id;
 private Integer age;
 private Integer sno;
}

有時候我們想通過age這個屬性獲取Student對象

有時候我們也想通過sno這個屬性獲取Student對象

難道我們在DAO層寫兩個接口?

比如這樣子?

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 上手測試 實(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é)號",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é)號',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

手動添加一些數(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 直接測試

通過age屬性查詢student:成功

通過sno屬性查詢:

通過屬性age刪除Student:

通過sno屬性刪除Student

補(bǔ)充知識:mybatis使用if條件判斷,數(shù)字類型不能寫 0 !=‘',否則會進(jìn)不到條件拼接里面

1.對于 if條件判斷:數(shù)字類型屬性判斷的時候

注意不可以是這種情況

<if test="delFlag!= null and delFlag!= ''">
 and del_flag = #{delFlag}
</if>

參數(shù)一個是0,一個是"",最終debug會走進(jìn)case 8 里面,0和“”都會被轉(zhuǎn)成double進(jìn)行比較,都會變成0.0,這就是mybati中if test 0!=""判定為false的原因

以上這篇mybatis通過if語句實(shí)現(xiàn)增刪改查操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring?Boot如何實(shí)現(xiàn)統(tǒng)一數(shù)據(jù)返回

    Spring?Boot如何實(shí)現(xiàn)統(tǒng)一數(shù)據(jù)返回

    這篇文章主要介紹了Spring?Boot如何實(shí)現(xiàn)統(tǒng)一數(shù)據(jù)返回,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-07-07
  • Spring之ShutDown?Hook死鎖現(xiàn)象解讀

    Spring之ShutDown?Hook死鎖現(xiàn)象解讀

    這篇文章主要介紹了Spring之ShutDown?Hook死鎖現(xiàn)象解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Spring @Conditional注解講解及示例詳解

    Spring @Conditional注解講解及示例詳解

    這篇文章主要介紹了Spring @Conditional注解講解及示例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • MyBatis-Plus自動填充字段的詳細(xì)教程

    MyBatis-Plus自動填充字段的詳細(xì)教程

    今天編寫一個詳細(xì)的教程來介紹如何在?Spring?Boot?項(xiàng)目中使用?MyBatis-Plus?實(shí)現(xiàn)自動填充時間字段(如創(chuàng)建時間?createTime?和更新時間?updateTime),可以分為以下幾個部分,這個教程將涵蓋從項(xiàng)目配置到自動填充的完整過程,需要的朋友可以參考下
    2024-08-08
  • 兼容Spring Boot 1.x和2.x配置類參數(shù)綁定的工具類SpringBootBindUtil

    兼容Spring Boot 1.x和2.x配置類參數(shù)綁定的工具類SpringBootBindUtil

    今天小編就為大家分享一篇關(guān)于兼容Spring Boot 1.x和2.x配置類參數(shù)綁定的工具類SpringBootBindUtil,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • 深入理解Java IO的flush

    深入理解Java IO的flush

    本篇文章是小編總結(jié)的關(guān)于Java IO的flush的相關(guān)知識點(diǎn)內(nèi)容,有需要的朋友可以跟著學(xué)習(xí)下。
    2018-06-06
  • Java并發(fā)編程之線程中斷

    Java并發(fā)編程之線程中斷

    這篇文章主要介紹了Java并發(fā)編程線程中斷,java線程中斷是一種線程間的協(xié)作模式,通過設(shè)置線程的中斷標(biāo)志并不能直接終止該線程的運(yùn)行,而是被中斷的線程根據(jù)中斷狀態(tài)自行處理,需要的朋友可以參考一下
    2021-09-09
  • Java中的CopyOnWriteArrayList原理詳解

    Java中的CopyOnWriteArrayList原理詳解

    這篇文章主要介紹了Java中的CopyOnWriteArrayList原理詳解,如源碼所示,CopyOnWriteArrayList和ArrayList一樣,都在內(nèi)部維護(hù)了一個數(shù)組,操作CopyOnWriteArrayList其實(shí)就是在操作內(nèi)部的數(shù)組,需要的朋友可以參考下
    2023-12-12
  • MyBatis主鍵自增的兩種實(shí)現(xiàn)方法

    MyBatis主鍵自增的兩種實(shí)現(xiàn)方法

    本文主要介紹了MyBatis主鍵自增的兩種實(shí)現(xiàn)方法,主要包括注解方式或配置文件方式來實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01
  • idea中使用maven?archetype新建項(xiàng)目時卡住問題解決方案

    idea中使用maven?archetype新建項(xiàng)目時卡住問題解決方案

    這篇文章主要介紹了idea中使用maven?archetype新建項(xiàng)目時卡住,解決本問題的方法,就是在maven的runner加上參數(shù)-DarchetypeCatalog=local就可以了,不需要下載xml文件再放到指定目錄,需要的朋友可以參考下
    2023-08-08

最新評論