mybatis通过if语句实现增删改查操作

时间:2021-05-19

有时候为了简化我们的代码。

1 举个例子

Student类:

@Datapublic 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:

@Datapublic 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层:

@Mapperpublic 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层:

@Servicepublic 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语句实现增删改查操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章