您好,登錄后才能下訂單哦!
本篇文章為大家展示了mybatis中的增刪改查怎么利用if語句來實現,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
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 上手測試 實驗
實體類 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 = "學號",example = "334",position = 3) private Integer sno; }
數據庫:
CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `age` int(11) DEFAULT NULL COMMENT '年齡', `sno` int(11) NOT NULL COMMENT '學號', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
手動添加一些數據
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("學生接口") @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
補充知識:mybatis使用if條件判斷,數字類型不能寫 0 !=‘',否則會進不到條件拼接里面
1.對于 if條件判斷:數字類型屬性判斷的時候
注意不可以是這種情況
<if test="delFlag!= null and delFlag!= ''"> and del_flag = #{delFlag} </if>
參數一個是0,一個是"",最終debug會走進case 8 里面,0和“”都會被轉成double進行比較,都會變成0.0,這就是mybati中if test 0!=""判定為false的原因
上述內容就是mybatis中的增刪改查怎么利用if語句來實現,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。