Mybatis中的CRUD可变参数

多参数查询

在对应接口方法的参数之前使用@Param(“名称”)来标识多个参数
具体使用为:

List<Student> 
getStudentByIdAndName(@Param("id") Integer id,
					  @Param("name") String name);

可变参数

可变参数的意思是,比如调用一个接口的参数最多可以有五个,但是输入三个、两个,或者不输入也是可以的。

查询条件的可变参数

一般类似于这种多条件查询。我们都会给其放到一个实体类里里面,然后在xml文件的where 标签里写条件

<select  id = "getList" resultType="对应实体">
	select * from 表名
	<where>
		<if test="实体字段1 != null and 实体字段1 != ''">
	 		查询条件1
    	</if>
		<if test="实体字段2 != null and 实体字段2 != ''">
	 		and 查询条件2
    	</if>
	</where>
	order by 字段
</select>

上述条件是对于String类型的字段,如果是Integer字段的话就去掉 != ‘’
就是以下这样

<if test="Integer字段 != null">
	 	查询条件
</if>

假如我们要根据id和name查询学生信息,其中id为Integer类型,name为String类型。那么可以这么写

<select  id = "getStudentList" resultType="Student">
	select * from student
	<where>
	 	<if test="id != null">
	 		id = #{id}
     	</if>
		<if test="name != null and name != ''">
	 		and name like concat('%' , #{name} ,'%')
     	</if>
	</where>
	order by id asc
</select>

更新条件的可变参数

更新的话里面就在set标签里写

<update id = "updateStudent">
	update student 
	<set>
		<if test="id != null">
	 	   	id = #{id} ,
        </if>
	    <if test="name != null and name != ''">
	 		name =  #{name}
        </if>
	</set>
</update>

插入条件的可变参数

假如一条insert语句是以下这样

insert into 表名(A) values(B)

那么可变参数的地方就是在A和B两个地方了。那么还是插入一个学生为例

<insert id = "addStu">
	insert into student(
		<if test="id != null">
	 	   id
        </if>
	    <if test="name != null and name != ''">
	 	 , name
        </if>
	) values (
		<if test="id != null">
	 	   #{id}
        </if>
	    <if test="name != null and name != ''">
	 	 , #{name}
        </if>
	)
</insert>	

在上面这个例子里,有个明显的错误,如果i对象(id和name)为空,那么就会出错。

解决这个问题也简单,我们可以前端做一个校验让其中的某一个字段为必填

顺带说一下PageHelper的使用

依赖

<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.3.0</version>
</dependency>

使用流程

一般是Controller获取到page和size
在servicesImp调用dao层之前

public List<Student> getAllStudent(Integer page , Integer size , Integer id , String name) {
	//开启分页
	PageHelper.startPage(page , size);
	//dao层的操作
	return dao.getAllStudent(id , name);
}

然后在对应请求的Controller里面

    @RequestMapping(value = "getAllStudent" , method = RequestMethod.POST)
    public Result getAllStudent(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
            @RequestParam(value = "id" , required = false) Integer id ,
            @RequestParam(value = "name" , required = false) String name
    ) {
        List<Student> list = services.getAllStudent(page , size , id , name);
        if(list != null) {
        	//将list处理成格式化的PageInfo
            PageInfo pageInfo = new PageInfo(list);
            return new Result(pageInfo);
        }else {
            return new Result("查询失败");
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值