关于mybatis中基本类型条件判断问题
零:sql动态语句中经常会有根据数据库某个字段状态进行判断的
如:status=0为未激活,status=1为激活的,那搜索未激活时:
<if test="model.activeStatus != null and model.activeStatus !='' or model.activeStatus==0">
and status=#{model.activeStatus}
</if>
但由于java的int类型默认值为0,导致0与null的判定无法识别。解决办法:
1、int修改为Integer类型
2、参数不要带activeStatus变量名,如:json的请求参数中不要带activeStatus参数名称。(未验证)
一:发现问题
sql动态语句中如果 parameterType="int"
<select id="sel_campusinfo" parameterType="int" resultType="Campusinfo">
select cmpid,cmpname from campusinfo where state!='d' and cmpid=#{cmpid}
</select>
是正确的,但是如果加上if test
<select id="sel_campusinfo" parameterType="int" resultType="Campusinfo">
select cmpid,cmpname from campusinfo where state!='d' and cmpid!=0
<if test="cmpid!=0">and cmpid=#{cmpid}</if>
</select>
There is no getter for property named 'cmpid' in 'class java.lang.Integer'
出错原因:Mybatis默认采用ONGL解析参数,所以会自动采用对象树的形式取Integer.cmpid。Integer对象没有cmpid属性。如果不解析参数,mybatis自动识别传入的参数,不会报错。
二:解决办法
1.修改select语句
<select id="sel_campusinfo" parameterType="int" resultType="Campusinfo">
select cmpid,cmpname from campusinfo where state!='d' and cmpid!=0
<if test="_parameter!=0">and cmpid=#{_parameter}</if>
</select>
参数名全部改为_parameter。
2.不修改sql,只修改接口
接口类:
Campusinfo sel_campusinfo( int cmpid);
改为:
3.可以将参数包装在hashmap或者对象中作为参数
关于mybatis中基本类型条件判断问题的更多相关文章
- bash Shell 中如何实现条件判断之if判断
http://blog.51cto.com/lovelace/1211353 bash中如何实现条件判断?条件测试类型: 整数测试 字符测试 文件测试 一.条件测试的表达式: ...
- linux Shell中常用的条件判断
linux Shell中常用的条件判断 -b file 若文件存在且是一个块特殊文件,则为真 -c file 若文件存在且是一个字符特殊文件,则为真 -d ...
- MyBatis从入门到精通(十四):在MyBatis中使用类型处理器
最近在读刘增辉老师所著的<MyBatis从入门到精通>一书,很有收获,于是将自己学习的过程以博客形式输出,如有错误,欢迎指正,如帮助到你,不胜荣幸! 本篇博客主要讲解在MyBatis中如何 ...
- mybatis if else if 条件判断SQL片段表达式取值和拼接
前言 最近在开发项目的时候涉及到复杂的动态条件查询,但是mybaits本身不支持if elseif类似的判断但是我们可以间接通过 chose when otherwise 去实现其中choose为一个 ...
- HtmlWebpackPlugin用的html的ejs模板文件中如何使用条件判断
折腾: [已解决]给react-hot-boilerplate中的index.html换成用HtmlWebpackPlugin自动生成html 期间,已经有了思路了,但是不知道如何在ejs的html中 ...
- shell编程学习笔记(八):Shell中的if条件判断
编程语言中都有条件判断,shell编程也不例外,下面我们来看一下shell中应该怎么使用if条件判断 以下蓝色字体部分为Linux命令,红色字体的内容为输出的内容: # cd /opt/scripts ...
- mybatis中xml字段空判断及模糊查询
由于业务特殊的查询需求,需要下面的这种查询,一直感觉模糊不清,本地测试一下顺便做个总结 贴一段xml代码,如下: <if test="receivedName != null and ...
- mybatis中的foreach条件参数过多时,#和$效率比较
在客户端查询都小于1秒. 测试: mybatis中in条件12.3万数据$ : 6051 ms# : 27045 ms 1.2万数据$ : 1154 ms# : 24387 ms 5 万数据$ : 2 ...
- shell编程学习笔记(九):Shell中的case条件判断
除了可以使用if条件判断,还可以使用case 以下蓝色字体部分为Linux命令,红色字体的内容为输出的内容: # cd /opt/scripts # vim script08.sh 开始编写scrip ...
随机推荐
- Android系统--Binder系统具体框架分析(一)补充
Android系统--Binder系统具体框架分析(一)补充 补充:对Binder驱动分析一的代码补充,添加saygoobye和saygoodbye_to服务 test_server.h #ifnde ...
- Deep Learning概述
1.深度学习发展简史 2.三步实现深度学习 2.1Neural Network 神经网络由模仿脑部神经系统发展而来,一个节点称为一个“Neuron”,包括连接在节点上面的weights和biases. ...
- SpringBoot 通用Validator
第一步,pom.xml引入hibernate-validator <dependency> <groupId>org.hibernate</groupId> < ...
- RealThinClient SDK 学习笔记(1)
从客户端调用远程函数的两种方法 1: RtcClientModule1.Prepare('select'); // call the "select" function on th ...
- LeetCode——Construct Binary Tree from Inorder and Postorder Traversal
Question Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may a ...
- 集群环境ssh免密码登录设置
一.准备工作 1) 用客户端工具(ssh client或者putty)连接到linux服务器.在root用户下输入命令 vi /etc/hosts,用vi编辑hosts文件,如下: #127.0.0. ...
- javascript 时间日期处理相加相减
var d = new Date("2008/04/15"); d.setMonth(d.getMonth() + 1 + 1);//加一个月,同理,可以加一天:getDate() ...
- linux查看cpu
#查看物理CPU个数 cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l #查看每个物理CPU中core的个数(即核数 ...
- Codeforces Round #366 (Div. 2) A , B , C 模拟 , 思路 ,queue
A. Hulk time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- keystone uwsgi failed
~$ /usr/local/bin/uwsgi /etc/keystone/keystone-uwsgi-public.ini[uWSGI] getting INI configuration fro ...