MyBatis动态sql
来源:互联网 发布:mac网页下载速度慢 编辑:程序博客网 时间:2024/06/05 17:50
MyBatis
数据库表的结构
实体类
——————
<select id="selectReaderMoney" resultType="Reader" parameterType="Reader"> select * from reader where 1=1 <if test="3>readerID"> and #{money}>money </if> <!-- userID是实体类对线中的userID --> <!-- 传入是实体类对象为参数值,如果userID大于三,说明有权限,就可以查询全部信息 如果小于三,说明权限不够,只能看钱少于参数的 --> </select>
下面看测试代码,省略了获取session的步骤,不懂得可以参考Mybatis配置
domain.Reader reader2=new domain.Reader(); reader2.setReaderID(3); reader2.setMoney(1000); List<domain.Reader> readerList=sqlSession.selectList("selectReaderMoney",reader2); for (domain.Reader readers : readerList) { System.out.println(readers.getMoney());
这里id>3 所以不会执行if中的语句,请看查询结果
如果修改以上代码
reader2.setReaderID(2);
这时sql的执行就如下了
这就是动态sql,通过不同的条件,执行不同的sql语句
但如果查询具有多个条件 就需要一下配置
<select id="selectReaderChoose" resultMap="ReaderMap" parameterType="Reader"> select * from reader where 1=1 <choose> <when test="money>300"> and readerID>1 </when> <when test="money==0"> and 4>readerID </when> </choose> </select>
choose when 非常累死java中的switch case 大家可以这么来理解
执行结果就和if的类似,只是只是查询的判断多了 ,执行的sql也不同
阅读全文
0 0
- mybatis动态SQL语句
- MyBatis动态SQL
- MyBatis 动态SQL
- Mybatis 动态SQL
- MyBatis动态SQL
- MyBatis动态SQL完整版
- mybatis动态sql
- mybatis动态SQL语句
- mybatis动态SQL语句
- MyBatis的动态SQL
- MyBatis动态SQL
- MyBatis动态SQL
- MyBatis 动态SQL语句
- MyBatis动态SQL
- Mybatis组建动态SQL
- MyBatis 动态sql
- mybatis 动态SQL语句
- MyBatis动态SQL详解
- Dialog.Alert黑色背景
- 数据结构实验之栈:行编辑器(栈)
- Linux定时任务Crontab命令详解
- 简单封装一个AlertDialog,调用只需要两行代码
- WebView与Js互调
- MyBatis动态sql
- CodeForces 834B The Festive Evening
- 数据库中的开窗函数
- JAVA——赋值运算符
- html 语言代码
- Mybatis 环境搭建以实现功能,Mybatis配置内容
- crm项目遇到的问题总结
- Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
- 部分正则验证