Mysql按条件查询
2017-11-28 21:07
155 查看
这是第二篇关于MySql语句的文章,打算写一系列关于Mysql查询的文章,之所以要写出来,是后期在做项目中遇到过一些比较复杂的查询语句,如果用完就放着不管了,时间
久了就忘记了,所以打算把这些语句写成一系列的文章。
和上一篇文章一样,开发环境不变,使用框架也不变。
java代码部分:
@Responsebody
@RequestMapping("/XXX.do")
public void XXX(ModelMap model ,HttpServletRequest request,HttpServletResponse response) {
int beginIndex_Int = -1;
int pageSize_Int = 0;
// 在这里取出需要分页的起始索引以及每页的数据量
String beginIndex = request.getparameter("beginIndex");
String pageSize = request.getParameter("pageSize");
// 因为数据库在分页的时候查询索引和数量不支持字符串,所以需要转换成int型
if(beginIndex != null){
beginIndex_Int = Integer.ParseInt(beginIndex);
}
if(pageSize != null){
pageSize_Int = Integer.parseInt(pageSize);
}
Map<String,Object> map = new HashMap<String,Object>();
// 把页数和数量放到map集合中
map.put("beginIndex",beginIndex_Int);
map.put("pageSize","pageSize_Int");
// 这里放入要用到的查询条件
map.put("state","1");
map.put("number",number);
// 把map作为参数传到Mapper文件中去
XXX(map);
}
sql语句的写法:
<select id = "XXX(方法名)" parameterType = "hashmap" resultMap = "BaseResultMap">
select * from table(table写自己的表名称)
where 1 = 1
<if test="state != null and state != ' ' ">
and state = #{state,jdbcType = INTEGER}
</if>
<if test="number != null and number !=' ' ">
and (sjrdh like concat ('%',#{number,jdbcType=VARCHAR},'%')
or hgh = #{number,jdbcType=VARCHAR})
</if>
order by XX(根据某个字段排序) desc
<if test ="beginIndex != null and beginIndex != -1 ">
limit #{beginIndex,jdbcType=INTEGER},#{pageSize,jdbcType=INTEGER}
</if>
</select>
久了就忘记了,所以打算把这些语句写成一系列的文章。
和上一篇文章一样,开发环境不变,使用框架也不变。
java代码部分:
@Responsebody
@RequestMapping("/XXX.do")
public void XXX(ModelMap model ,HttpServletRequest request,HttpServletResponse response) {
int beginIndex_Int = -1;
int pageSize_Int = 0;
// 在这里取出需要分页的起始索引以及每页的数据量
String beginIndex = request.getparameter("beginIndex");
String pageSize = request.getParameter("pageSize");
// 因为数据库在分页的时候查询索引和数量不支持字符串,所以需要转换成int型
if(beginIndex != null){
beginIndex_Int = Integer.ParseInt(beginIndex);
}
if(pageSize != null){
pageSize_Int = Integer.parseInt(pageSize);
}
Map<String,Object> map = new HashMap<String,Object>();
// 把页数和数量放到map集合中
map.put("beginIndex",beginIndex_Int);
map.put("pageSize","pageSize_Int");
// 这里放入要用到的查询条件
map.put("state","1");
map.put("number",number);
// 把map作为参数传到Mapper文件中去
XXX(map);
}
sql语句的写法:
<select id = "XXX(方法名)" parameterType = "hashmap" resultMap = "BaseResultMap">
select * from table(table写自己的表名称)
where 1 = 1
<if test="state != null and state != ' ' ">
and state = #{state,jdbcType = INTEGER}
</if>
<if test="number != null and number !=' ' ">
and (sjrdh like concat ('%',#{number,jdbcType=VARCHAR},'%')
or hgh = #{number,jdbcType=VARCHAR})
</if>
order by XX(根据某个字段排序) desc
<if test ="beginIndex != null and beginIndex != -1 ">
limit #{beginIndex,jdbcType=INTEGER},#{pageSize,jdbcType=INTEGER}
</if>
</select>
相关文章推荐
- 【MySQL错误】不能用中文条件进行查询
- mysql 数据条件过滤查询练习
- mysql排序查询,多条件排序查询,聚合函数的使用,分组统计查询,
- MySQL 设置数据按条件查询下的序号
- MySQL查询语句查询条件严格匹配大小写
- Oracle,Mysql ,SQL Server 三大数据库带参数的模糊查询, 拼接查询条件问题
- mybatis中mysql多条件查询
- mysql进阶(五)数据表中带OR的多条件查询
- MySQL之数据查询语法(DQL)--(2)条件查询
- MySQL DELETE FROM与子查询作为条件
- mysql的5中查询子句之三having条件查询
- 字符串变量作mysql查询条件
- mysql group by内排序的实现以及having和where 条件语句查询的区别
- mysql单表查询Where 条件查询
- MySQL种字符串拆分(substring_index)并作为关联查询条件后多列展示
- MySQL 5.1中IN查询不要用到NULL条件
- MYSQL 查询不同条件,但是同一个字段的结果
- 解决mysql的in条件中参数是带引号的字符串的时候查询失效
- MySQL 设置数据按条件查询下的序号
- mysql查询符合某种条件的语句是第几条