MyBatis对入参对象的属性空判断

<!-- 查询学生list,like姓名 -->

<select id="getStudentListLikeName" parameterType="StudentEntity" resultMap="studentResultMap">

SELECT * from STUDENT_TBL ST

WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT(‘%‘, #{studentName}),‘%‘)

</select>

但是此时如果studentName是null或空字符串,此语句很可能报错或查询结果为空。此时我们使用if动态sql语句先进行判断,如果值为null或等于空字符串,我们就不进行此条件的判断。

修改为:

<!-- 查询学生list,like姓名 -->

<select id=" getStudentListLikeName " parameterType="StudentEntity" resultMap="studentResultMap">

SELECT * from STUDENT_TBL ST

<if test="studentName!=null and studentName!=‘‘ ">

WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT(‘%‘, #{studentName}),‘%‘)

</if>

</select>

完整版:

<!-- 查询学生list,like姓名,=性别、=生日、=班级,使用where,参数entity类型 -->

<select id="getStudentListWhereEntity" parameterType="StudentEntity" resultMap="studentResultMap">

SELECT * from STUDENT_TBL ST

<where>

<if test="studentName!=null and studentName!=‘‘ ">

ST.STUDENT_NAME LIKE CONCAT(CONCAT(‘%‘, #{studentName}),‘%‘)

</if>

<if test="studentSex!= null and studentSex!= ‘‘ ">

AND ST.STUDENT_SEX = #{studentSex}

</if>

<if test="studentBirthday!=null">

AND ST.STUDENT_BIRTHDAY = #{studentBirthday}

</if>

<if test="classEntity!=null and classEntity.classID !=null and classEntity.classID!=‘‘ ">

AND ST.CLASS_ID = #{classEntity.classID}

</if>

</where>

</select>

当if标签较多时,这样的组合可能会导致错误。

例如:

<!-- 查询学生list,like姓名,=性别 -->

<select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">

SELECT * from STUDENT_TBL ST

WHERE

<if test="studentName!=null and studentName!=‘‘ ">

ST.STUDENT_NAME LIKE CONCAT(CONCAT(‘%‘, #{studentName}),‘%‘)

</if>

<if test="studentSex!= null and studentSex!= ‘‘ ">

AND ST.STUDENT_SEX = #{studentSex}

</if>

</select>

中,参数studentName为null或’’,则或导致此sql组合成“WHERE AND”之类的关键字多余的错误SQL。

这时可以使用where动态语句来解决。这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。

<!-- 查询学生list,like姓名,=性别 -->

<select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">

SELECT * from STUDENT_TBL ST

<where>

<if test="studentName!=null and studentName!=‘‘ ">

ST.STUDENT_NAME LIKE CONCAT(CONCAT(‘%‘, #{studentName}),‘%‘)

</if>

<if test="studentSex!= null and studentSex!= ‘‘ ">

AND ST.STUDENT_SEX = #{studentSex}

</if>

</where>

</select>

详见http://blog.csdn.net/ask_rent/article/details/6320326

时间: 2024-08-09 03:06:37

MyBatis对入参对象的属性空判断的相关文章

mybatis框架之多参数入参--传入Map集合

需求:查询出指定性别和用户角色列表下的用户列表信息 实际上:mybatis在入参的时候,都是将参数封装成为map集合进行入参的,不管你是单参数入参,还是多参数入参,都是可以封装成map集合的,这是无可非议的. /** * 需求:查询出指定性别和用户角色列表下的用户列表信息 * @param roleids * @return */ public List<User> getUserListByGender_UserRoleids(Map<String,Object> conditi

Mybatis方法入参处理

1,在单个入参的情况下,mybatis不做任何处理,#{参数名} 即可,甚至连参数名都可以不需要,因为只有一个参数,或者使用 Mybatis的内置参数 _parameter. 2,多个入参: 接口方法定义:public Employee getEmpByIdAndName(Integer id,String name); 取值:#{id},#{name} mybatis抛出异常:org.apache.ibatis.binding.BindingException:Paramter 'id' no

springMVC中 request请求数据绑定到Controller入参 过程剖析

前言:Controller方法的参数类型可以是基本类型,也可以是封装后的普通Java类型.若这个普通Java类型没有声明任何注解,则意味着它的每一个属性都需要到Request中去查找对应的请求参数.众所周知,无论客户端传入的是什么类型的请求参数,最终都要以字节的形式传给服务端.而服务端通过Request的getParameter方法取到的参数也都是字符串形式的结果.所以,需要有一个把字符串形式的参数转换成服务端真正需要的类型的转换工具(基类:PropertyEditorSupport) 注:只有

mybatis入参方式和缓冲

1.mybatis入参方式 @Param注解参数(注解) 封装成对象入参 public int updatePassword(@Param("id")int id,@Param("pwd")String newpwd); 注意:一般情况下:参数超过3个,就用对象. 2.MyBatis缓存 1).分类 一级缓存:SqlSession级别的缓存.(在同一个SqlSession中,缓存有效,默认打开) 二级缓存:应用级别缓存(全局缓存,随便在哪里都能取得到.默认是关闭的)

Mybatis调用PostgreSQL存储过程实现数组入参传递

注:本文来源于 < Mybatis调用PostgreSQL存储过程实现数组入参传递  > 前言 项目中用到了Mybatis调用PostgreSQL存储过程(自定义函数)相关操作,由于PostgreSQL自带数组类型,所以有一个自定义函数的入参就是一个int数组,形如: CREATE OR REPLACE FUNCTION "public"."func_arr_update"(ids _int4)... 1 如上所示,参数是一个int数组,Mybatis提

spring mvc绑定对象String转Date解决入参不能是Date的问题

使用spring的mvc,直接将页面参数绑定到对象中,对象中有属性为Date时会报错,此时需要处理下. 同样的,其他的需要处理的类型也可以用这种方法. 在controller中加入代码 @InitBinder protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { //对于需要转换为Date类型的属性,使用DateEditor进行处理 bin

springMVC4(12)复杂对象和集合类型入参绑定

1. 复杂对象参数绑定 对于普通的对象参数绑定,我们只需要对象成员变量名与请求参数名一一对应即可完成绑定. 而求对于组合对象,我们可以使用级联的方式来绑定方法参数.见下面实例: 我们先定义两个POJO类:User,Article其中Atricle是User的成员属性: public class Article { private Integer id; private String title; private String content; //忽略get和set方法 } package co

根据接口入参不同返回不同对象集合的方法

最近要写一个外部调用的webservice接口,入参和出参都是xml格式,根据不同的type查询不同的集合返回,代码如下: 首先配置webservice接口的xml文件 然后写接口类 然后是实现类 实现类中最主要的是根据不同的type查询出结果然后分装成返回xml 封装的返回方法运用了泛型,可以传入不同的对象集合 public <T> String getDetailXml(String retCode,List<T> list){ Document doc=DocumentHel

mybatis中mapper传多个入参

有三种方式 1.使用占位符#{0},#{1}....对应顺序就是参数的顺序 #方法签名 List<TbItem> selectByPage(int page, int rows); #sql语句 <select id="selectByPage" resultMap="BaseResultMap"> SELECT <include refid="Base_Column_List" /> from tb_item