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

    RHCSA认证中的东西: Linux系统管理包括的内容有: 磁盘分区及文件系统管理 RAID LVM 网络属性管理 程序包管理 sed and awk 进程查看和管理 内核管理(编译和安装) 系统启动 ...

  2. vue2.0+webpack+vuerouter+vuex+axios构建项目基础

    前言 本文讲解的是vue2.0+webpack+vuerouter+vuex+axios构建项目基础 步骤 1.全局安装webpack,命令 npm install webpack -g 注意,web ...

  3. C#信号量(Semaphore,SemaphoreSlim)

    Object->MarshalByRefObject->WaitHandle->Semaphore 1.作用: 多线程环境下,可以控制线程的并发数量来限制对资源的访问 2.举例: S ...

  4. C#的队列(Queue,ConcurrentQueue)和堆栈(Stack,ConcurrentStack)

    一.Queue 表示对象的先进先出(FIFO)集合,非线程安全 常用方法   Dequeue 入队 Enqueue 出队 Contains 队列中是否存在某元素 Clear 清空队列 封装: /// ...

  5. Cloneable注解使用

    使用 clone()方法的类必须 implement Cloneable 如果没有继承,clone()方法会报错 java.lang.CloneNotSupportedException异常

  6. prometheus部署

    1.prometheus安装 软件下载: wget https://dl.grafana.com/oss/release/grafana-6.4.2-1.x86_64.rpm https://gith ...

  7. awk的妙用

    终端形式 有人说awk的优势在于可以个性化输出命令,这么说来太抽象了,假如我们查看占用6379端口的进程信息. lsof -i: 输出结果: COMMAND PID USER FD TYPE DEVI ...

  8. 【ArcMap】

    1.加载图层(1)内容列表中右键添加数据(2)目录列表中拖拽(3)导航中的添加数据 2.编辑要素(1)选中编辑器点击开始编辑(2)在编辑要素中选中要编辑的要素 选择构造工具 执行编辑操作(3)停止编辑 ...

  9. Web自动化简介

    在迭代中时,先写出粗略的自动化测试用例,不是非常精准的一些提示,等到项目上线后,提示文案已经确定,再完善用例,使之成为一个完整的自动化测试工程. 用excel管理测试数据,读取效率比较低,需要读取.数 ...

  10. Python文本和字节序列

    ASCII码 早期人们用8位二进制来编码英文字母(最前面的一位是0) 也就是说,将英文字母和一些常用的字符和这128种二进制0.1串一一对应起来, 比如:大写字母“A”所对应的二进制位“0100000 ...