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

网友投稿 228 2022-11-11


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 * from student where age=#{age}

select * from student where sno=#{sno}

显然,这样子是不高效的

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

select * from student where

age=#{age}

sno=#{sno}

delete from student

age =#{age}

sno=#{sno}

Service层:

@Service

public class StudentService {

@Autowired

StudentDao studentDao;

/**

*sBlwsfAqNd @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 clahttp://ss 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条件判断:数字类型属性判断的时候

注意不可以是这种情况

and del_flag = #{delFlag}

参数一个是0,一个是"",最终debug会走进case 8 里面,0和“”都会被转成double进行比较,都会变成0.0,这就是mybati中if test 0!=""判定为false的原因


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:windows定时器配置执行java jar文件的方法详解
下一篇:浅谈Java线程池是如何运行的
相关文章

 发表评论

暂时没有评论,来抢沙发吧~