mybatis:传入参数为一个实体类时,<if test="">怎么获取其属性

本文详细解析了在使用MyBatis框架时,如何正确地绑定实体类参数到SQL语句中,避免因参数绑定失败导致的错误。重点介绍了在&lt;if&gt;标签中直接使用对象属性而非#{}

接口方法:绑定一个实体类作为参数

对应的xml:

<if>标签里不用写#{},但要对象名.属性,如果没有对象名会报错

报的是参数绑定失败错误:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'countryIsoCode' not found

因此要 对象名.属性

解释一下列语句的语法,纯文本输出.<?xml version="1.0" encoding="UTF-8" ?> <!-- sky-server\src\main\resources\mapper\DishMapper.xml --> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.sky.mapper.DishMapper"> <insert id="insert" useGeneratedKeys="true" keyProperty="id"> insert into dish (name, category_id, price, image, description, create_time, update_time, create_user, update_user,status) values (#{name}, #{categoryId}, #{price}, #{image}, #{description}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser},#{status}) </insert> <select id="pageQuery" resultType="com.sky.vo.DishVO"> select d.*,c.name as categoryName from dish d left outer join category c on d.category_id = c.id <where> <if test="name != null"> and d.name like concat('%',#{name},'%') </if> <if test="categoryId != null"> and d.category_id = #{categoryId} </if> <if test="status != null"> and d.status = #{status} </if> </where> order by d.create_time desc </select> <select id="list" resultType="com.sky.entity.Dish"> select * from dish <where> <if test="categoryId != null"> and category_id = #{categoryId} </if> <if test="name != null"> and name = #{name} </if> <if test="status != null"> and status = #{status} </if> </where> order by create_time desc </select> <select id="countByMap" resultType="java.lang.Integer"> select count(id) from dish <where> <if test="status != null"> and status = #{status} </if> <if test="categoryId != null"> and category_id = #{categoryId} </if> </where> </select> <update id="update"> update dish <set> <if test="name != null"> name = #{name}, </if> <if test="categoryId != null"> category_id = #{categoryId}, </if> <if test="price != null"> price = #{price}, </if> <if test="image != null"> image = #{image}, </if> <if test="description != null"> description = #{description}, </if> <if test="status != null"> status = #{status}, </if> <if test="updateTime != null"> update_time = #{updateTime}, </if> <if test="updateUser != null"> update_user = #{updateUser}, </if> </set> where id = #{id} </update> </mapper>
最新发布
10-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值