1)  查询条件不确定,需要根据情况产生SQL语法,这种情况叫动态SQL

/**
* 持久层
* @author AdminTC
*/
public class StudentDao {
/**
* 动态SQL--查询
*/
public List<Student> dynaSQLwithSelect(String name,Double sal) throws Exception{
SqlSession sqlSession = MyBatisUtil.getSqlSession();
try{
Map<String,Object> map = new LinkedHashMap<String, Object>();
map.put("pname",name);
map.put("psal",sal);
return sqlSession.selectList("mynamespace.dynaSQLwithSelect",map);
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();
throw e;
}finally{
sqlSession.commit();
MyBatisUtil.closeSqlSession();
}
}
public static void main(String[] args) throws Exception{
StudentDao dao = new StudentDao(); List<Student> studentList1 = dao.dynaSQLwithSelect("哈哈",null);
for(Student student : studentList1){
System.out.println(student.getId()+":"+student.getName()+":"+student.getSal());
}
System.out.println("--------------");
List<Student> studentList2 = dao.dynaSQLwithSelect(null,7000D);
for(Student student : studentList2){
System.out.println(student.getId()+":"+student.getName()+":"+student.getSal());
}
System.out.println("--------------");
List<Student> studentList3 = dao.dynaSQLwithSelect("哈哈",7000D);
for(Student student : studentList3){
System.out.println(student.getId()+":"+student.getName()+":"+student.getSal());
}
System.out.println("--------------");
List<Student> studentList4 = dao.dynaSQLwithSelect(null,null);
for(Student student : studentList4){
System.out.println(student.getId()+":"+student.getName()+":"+student.getSal());
}
System.out.println("--------------");
}
}

StudentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="mynamespace">
<select id="dynaSQLwithSelect" parameterType="map" resultType="loaderman.Student">
select id,name,sal from students
<where>
<if test="pname!=null">
and name=#{pname}
</if>
<if test="psal!=null">
and sal=#{psal}
</if>
</where>
</select>
</mapper>

mybatis之动态SQL操作之查询的更多相关文章

  1. Mybatis中动态SQL多条件查询

    Mybatis中动态SQL多条件查询 mybatis中用于实现动态SQL的元素有: if:用if实现条件的选择,用于定义where的字句的条件. choose(when otherwise)相当于Ja ...

  2. MyBatis的动态SQL操作--查询

    查询条件不确定,需要根据情况产生SQL语法,这种情况叫动态SQL,即根据不同的情况生成不同的sql语句. 模拟一个场景,在做多条件搜索的时候,

  3. mybatis的动态sql及模糊查询

    1.动态sql 使用类似于jstl表达式来实现 2.模糊查找 用一个对象来封装条件 步骤: 1)新建一个条件实体 package com.hy.mybatis.entity; public class ...

  4. mybatis之动态SQL操作之更新

    1)  更新条件不确定,需要根据情况产生SQL语法,这种情况叫动态SQL /** * 持久层*/ public class StudentDao { /** * 动态SQL--更新 */ public ...

  5. mybatis之动态SQL操作之插入

    1)  根据条件,插入一个学生 /** * 持久层*/ public class StudentDao { /** * 动态SQL--插入 */ public void dynaSQLwithInse ...

  6. MyBatis的动态SQL操作--插入

    需求:向数据库中插入一条数据 //id,name,sal非空,三个字段都插入 insert into student(id,name,sal) values (?,?,?) //id,name非空,只 ...

  7. MyBatis的动态SQL操作--删除

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUYAAAC/CAIAAAANX+LCAAAYvElEQVR4nO2dWWycV9nHDyC6UEGBGy

  8. MyBatis的动态SQL操作--更新

    更新条件不确定,需要根据具体的情况生成sql语句. id是主键,一般不会去更新. 1.只更新name的值 update student set name = ? where id = ? 2.只更新s ...

  9. mybatis之动态SQL操作之删除

    /** * 持久层 */ public class StudentDao { /** * 动态SQL--删除 */ public void dynaSQLwithDelete(int... ids) ...

随机推荐

  1. Linux小试牛刀

    1.统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来 [root@centos7data]#getent passwd | grep -v ...

  2. mysqll中索引详细讲解

    MySQL(五) MySQL中的索引详讲   序言 之前写到MySQL对表的增删改查(查询最为重要)后,就感觉MySQL就差不多学完了,没有想继续学下去的心态了,原因可能是由于别人的影响,觉得对于My ...

  3. 遍历二叉树 - 基于队列的BFS

    之前学过利用递归实现BFS二叉树搜索(http://www.cnblogs.com/webor2006/p/7262773.html),这次学习利用队列(Queue)来实现,关于什么时BFS这里不多说 ...

  4. Java&Selenium处理页面Table以及Table中随机位置的数据

    一.摘要 前一段时间公司小伙伴刚刚接触自动化,遇到的一个问题,页面新创建的数据保存后,出现在table中的某个位置,并不一定是第一行还是第几行,这种情况下如何去操控它 本篇博文将介绍处理这个问题的一种 ...

  5. 学习elasticsearch(一)linux环境搭建(3)——head插件安装

    对于5.x的es,head插件不支持 ./elasticearch-plugin install [plugin_name]方式安装. 进入正文 1.首先确保你的机器安装了python,如果没有,请看 ...

  6. C# Zip压缩、解压

    /* *引用 NuGet包 ICSharpCode.SharpZipLib.dll */ public class ZipUtility { /// <summary> /// 所有文件缓 ...

  7. 【Amaple教程】4. 组件

    在Amaple单页应用中,一个页面其实存在两种模块化单位,分别是 模块 (am.Module类),它是以web单页应用跳转更新为最小单位所拆分的独立块: 组件 (am.Component类),它的定位 ...

  8. ACM-ICPC 2015 沈阳赛区现场赛 F. Frogs && HDU 5514(容斥)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5514 题意:有m个石子围成一圈, 有n只青蛙从跳石子, 都从0号石子开始, 每只能越过xi个石子.问所 ...

  9. Educational Codeforces Round 50 (Rated for Div. 2) F - Relatively Prime Powers(数学+容斥)

    题目链接:http://codeforces.com/contest/1036/problem/F 题意: 题解:求在[2,n]中,x != a ^ b(b >= 2 即为gcd)的个数,那么实 ...

  10. winform中的Windows Media Player播放器设置

    namespace WindowsMediaPlayer{ public partial class Form1 : Form { Form2 form2;//声明from2窗体 public For ...