mybatis的动态sql及模糊查询
1、动态sql
使用类似于jstl表达式来实现
2、模糊查找
用一个对象来封装条件
步骤:
1)新建一个条件实体
package com.hy.mybatis.entity; public class ConditionUser {
private String name; private int minAge; private int maxAge; @Override
public String toString() {
return "ConditionUser [name=" + name + ", minAge=" + minAge
+ ", maxAge=" + maxAge + "]";
} public ConditionUser(String name, int minAge, int maxAge) {
super();
this.name = name;
this.minAge = minAge;
this.maxAge = maxAge;
} public ConditionUser() {
super();
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getMinAge() {
return minAge;
} public void setMinAge(int minAge) {
this.minAge = minAge;
} public int getMaxAge() {
return maxAge;
} public void setMaxAge(int maxAge) {
this.maxAge = maxAge;
} }
2)写sql:
<?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="com.hy.mybatis.mapper.User"> <select id="getUser" parameterType="ConditionUser" resultType="DUser">
select * from d_user where 1=1
<if test="name != '%null%'">
and name like ${name}
</if>
and age between #{minAge} and #{maxAge}
</select> </mapper><?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="com.hy.mybatis.mapper.User"> <select id="getUser" parameterType="ConditionUser" resultType="DUser">
select * from d_user where 1=1
<if test="name != '%null%'">
and name like ${name}
</if>
and age between #{minAge} and #{maxAge}
</select> </mapper>
3)测试:
@Test
public void testGetUsers() {
SqlSession session = MyBatisUtil.getSqlSession();
String statement = "com.hy.mybatis.mapper.User.getUser";
List<DUser> users = session.selectList(statement, new ConditionUser("%"+null+"%", 13, 18));
System.out.println(users);
}
mybatis的动态sql及模糊查询的更多相关文章
- Mybatis中动态SQL多条件查询
Mybatis中动态SQL多条件查询 mybatis中用于实现动态SQL的元素有: if:用if实现条件的选择,用于定义where的字句的条件. choose(when otherwise)相当于Ja ...
- 动态SQL与模糊查询
一: 1.需求 实现多条件查询用户(姓名模糊查询,年龄在指定的最小值与最大值之间) 2.结构目录 3.准备数据与建表 CREATE TABLE d_user( id int PRIMARY KEY A ...
- mybatis之动态SQL操作之查询
1) 查询条件不确定,需要根据情况产生SQL语法,这种情况叫动态SQL /** * 持久层 * @author AdminTC */ public class StudentDao { /** * ...
- 动态SQL之模糊查询
模糊查询学习了三种: DAO层 // 可以使用 List<User> wherelike01(String user_name); // 忘记 List<User> where ...
- MyBatis动态SQL与模糊查询
sqlxml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC & ...
- mybatis 动态Sql的模糊查询
where teacher.tname like concat(concat(#{tName}),'%') 2:distinct的使用 下面先来看看例子: table id name 1 ...
- 利用MyBatis的动态SQL特性抽象统一SQL查询接口
1. SQL查询的统一抽象 MyBatis制动动态SQL的构造,利用动态SQL和自定义的参数Bean抽象,可以将绝大部分SQL查询抽象为一个统一接口,查询参数使用一个自定义bean继承Map,使用映射 ...
- MyBatis中动态SQL语句完成多条件查询
一看这标题,我都感觉到是mybatis在动态SQL语句中的多条件查询是多么的强大,不仅让我们用SQL语句完成了对数据库的操作:还通过一些条件选择语句让我们SQL的多条件.动态查询更加容易.简洁.直观. ...
- Mybatis学习记录(五)----Mybatis的动态SQL
1. 什么是动态sql mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 1.1 需求 用户信息综合查询列表和用户信息查询列表总数这两个statemen ...
随机推荐
- js 判断浏览器的类型
function getBrowser() { var Sys = {}; var ua = navigator.userAgent.toLowerCase(); var s; ...
- css background-size
先来看下语法:background-size: length|percentage|cover|contain;具体的值,百分比都ok,w3c上面说的很清楚,当时具体的值或者百分比的时候,第一个表示宽 ...
- kernel/mktime
/* * linux/kernel/mktime.c * * Copyright (C) 1991, 1992 Linus Torvalds */ #include <linux/mkti ...
- 由java的八个基本数据类型说开去
Java中定义了四类/八种基本数据类型: 布尔型----boolean 字符型----char 整数型----byte,short,int,long 浮点型----float,double 这八种基本 ...
- 自己瞎捣腾的Win7下Linux安装之路-----图例篇
写在前面 网上很多这方面的教程,我也是参考他们的文章和一些书籍才弄成功.没啥创新性,只为记录下来,以供自已理解和以后复习. 所谓工欲善其事,必先利其器.俺想学习Linux,当然是要先装一个Linux( ...
- mysql 操作用户权限
使用可以对mysql数据库用户表有操作权限的用户名登陆mysqlinsert into user(Host,User,Password) values('%','name','password');如 ...
- Django后台post请求中的csrf token
使用Requests库操作自己的Django站点,post登陆admin页面返回403,serverlog显示csrf token not set. csrf token是get登陆页面时服务器放在c ...
- lnmp搭建的常见错误
1:运行nginx时的错误 ./configure: error: the HTTP rewrite module requires the PCRE library. 解决: [root@svr11 ...
- eclipse编辑器配置
1.添加行号 在侧边空白处右键 勾选如图 2.改字体 window -> preferences 字体的常用配置 Consolas有一个问题是中文字体难以看清 解决方式有两种:一.把字体设置为C ...
- Codeforces Round #361 Jul.6th B题 ☺译
最近迈克忙着考前复习,他希望通过出门浮躁来冷静一下.迈克所在的城市包含N个可以浮躁的地方,分别编号为1..N.通常迈克在家也很浮躁,所以说他家属于可以浮躁的地方并且编号为1.迈克从家出发,去一些可以浮 ...