mybatis之动态SQL操作之查询
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操作之查询的更多相关文章
- Mybatis中动态SQL多条件查询
Mybatis中动态SQL多条件查询 mybatis中用于实现动态SQL的元素有: if:用if实现条件的选择,用于定义where的字句的条件. choose(when otherwise)相当于Ja ...
- MyBatis的动态SQL操作--查询
查询条件不确定,需要根据情况产生SQL语法,这种情况叫动态SQL,即根据不同的情况生成不同的sql语句. 模拟一个场景,在做多条件搜索的时候,
- mybatis的动态sql及模糊查询
1.动态sql 使用类似于jstl表达式来实现 2.模糊查找 用一个对象来封装条件 步骤: 1)新建一个条件实体 package com.hy.mybatis.entity; public class ...
- mybatis之动态SQL操作之更新
1) 更新条件不确定,需要根据情况产生SQL语法,这种情况叫动态SQL /** * 持久层*/ public class StudentDao { /** * 动态SQL--更新 */ public ...
- mybatis之动态SQL操作之插入
1) 根据条件,插入一个学生 /** * 持久层*/ public class StudentDao { /** * 动态SQL--插入 */ public void dynaSQLwithInse ...
- MyBatis的动态SQL操作--插入
需求:向数据库中插入一条数据 //id,name,sal非空,三个字段都插入 insert into student(id,name,sal) values (?,?,?) //id,name非空,只 ...
- MyBatis的动态SQL操作--删除
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUYAAAC/CAIAAAANX+LCAAAYvElEQVR4nO2dWWycV9nHDyC6UEGBGy
- MyBatis的动态SQL操作--更新
更新条件不确定,需要根据具体的情况生成sql语句. id是主键,一般不会去更新. 1.只更新name的值 update student set name = ? where id = ? 2.只更新s ...
- mybatis之动态SQL操作之删除
/** * 持久层 */ public class StudentDao { /** * 动态SQL--删除 */ public void dynaSQLwithDelete(int... ids) ...
随机推荐
- echo打印换行
shell环境中,echo是常用的数据命令,但有的时候,想通过“\n”使输出换行却换不了,这个时候需要增加-e选项: $ echo "Hellow.\nHey man~" Hell ...
- spring 整合mongodb报NoSuchMethodError错误
刚开始通过网上查到相关的资料进行了一些配置,参考链接:http://www.open-open.com/lib/view/open1454374782167.html maven的dependenci ...
- inter® management engine interface黄色感叹号解决方法
win10今天安装电脑驱动时发现inter® management engine interface怎么装都是黄色感叹号,所以做了下以下得测试 1.inter® management engine ...
- http方式获取远程文件内容
public class HttpServer { /// <summary> /// 读取远程文件的内容 /// </summary> /// <param name= ...
- Android利用json进行网络解析
必须单开一个线程,android界面的主线程不会负责通信模块
- JAVA遇见HTML——JSP篇(案例项目)
- 07—mybatis注解配置一
常用注解Select:映射查询的sql语句.SelectProvider:Select语句的动态sql映射.允许指定一个类名和一个方法在执行时返回运行的查询语句.有两个属性:type和mehtod,t ...
- docker: Error response from daemon: invalid mount config for type "bind": bind source path does not exist: /tmp/tfserving/
注意要是当前的完整路径 pwd查看到完整路径,再加入到source里面即可
- location - URL
1.hash:获取或设置href 属性中跟在数字符号 # 之后的内容 2.跳转页面: 1)location.href 2)location.replace() 3)location.reload(tr ...
- Java8-Stream-No.03
import java.util.ArrayList; import java.util.List; import java.util.UUID; import java.util.concurren ...