mybatis多条件语句查询
2017-09-26 15:06
260 查看
mybatis多条件语句查询
<?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" >
<!-- namespace命名空间,作用就是对sql进行分类化的管理,理解为sql隔离
注意:使用mapper代理开发时,namespace有特殊作用,namespace等于mapper接口地址
-->
<mapper namespace="com.yanshu.service.SpTopKeywordService" >
<!--只能针对一个表进行操作 一对一查询 -->
<select id="sptopshowID" parameterType="Map" resultType="com.yanshu.pojo.SpTopKeyword">
select * from Spider_Top_Keyword
</select>
<select id="sptopshow" parameterType="Map" resultType="com.yanshu.pojo.SpTopKeyword">
select * from Spider_Top_Keyword
<where>
<if test="status!=null and status!='' ">
status=#{status}
</if>
<if test="leveA!=null and leveA!='' ">
and leveA=#{leveA}
</if>
<if test="leveB!=null and leveB!='' ">
and leveB=#{leveB}
</if>
<if test="leveC!=null and leveC!='' ">
and leveC=#{leveC}
</if>
</where>
limit 0, 200
</select>
</mapper>
service和dao层省略....
controller层
package com.yanshu.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.yanshu.dao.SpTopKeywordDao;
import com.yanshu.dao.SpTopKeywordDaoJpa;
import com.yanshu.pojo.Business;
import com.yanshu.pojo.SpTopKeyword;
import com.yanshu.pojo.SpTopKeywordJpa;
import com.yanshu.service.SpTopKeywordService;
import com.yanshu.utils.JsonUtil;
@RestController
@CrossOrigin
public class SpTopKeywordController {
Queue<SpTopKeyword> queue = new LinkedList<>();
Queue<SpTopKeyword> queue1 = new LinkedList<>();
//任务:queue 0 和1
//查询:关键词
//update
@Autowired
private SpTopKeywordDao sptopdao;
@Autowired
private SpTopKeywordDaoJpa sptopjpa;
@Autowired
private SpTopKeywordService sptopservice;
@RequestMapping("/sptopkey")
public String getSpTopKeyword(String status,String leveC,String leveA,String leveB)
{
Map map=new HashMap();
map.put("leveA", leveA);
map.put("leveB", leveB);
map.put("leveC", leveC);
map.put("status", status);
if(status.equals("1"))
{
//得到的数据
//List<SpTopKeyword> listuser=sptopdao.getSpTopKeyword(status);
List<SpTopKeyword> listuser=sptopservice.sptopshow(map);
//把所有的数据放到Queue中
for (SpTopKeyword object : listuser) {
queue.offer(object);
}
//每次请求获取10个状态为0 的关键词。队列中保存200关键词。
List<SpTopKeyword> returnUsers=new ArrayList<>();
for(int i=1;i<=10;i++)
{
returnUsers.add(queue.poll());
}
return returnUsers!=null?JSON.toJSONString(returnUsers):JsonUtil.objectStatus(4);
}
else
{
//得到的数据
//List<SpTopKeyword> listuser=sptopdao.getSpTopKeyword(status);
List<SpTopKeyword> listuser=sptopservice.sptopshow(map);
//把所有的数据放到Queue中
for (SpTopKeyword object : listuser) {
queue1.offer(object);
}
//每次请求获取10个状态为0 的关键词。队列中保存200关键词。
List<SpTopKeyword> returnUsers=new ArrayList<>();
for(int i=1;i<=10;i++)
{
returnUsers.add(queue1.poll());
}
return returnUsers!=null?JSON.toJSONString(returnUsers):JsonUtil.objectStatus(4);
}
}
@RequestMapping("/upsptopkey")
public String updateSpTopKeyword(String status,String leveA,String leveB,String leveC)
{
Map map=new HashMap();
map.put("leveA", leveA);
map.put("leveB", leveB);
map.put("leveC", leveC);
map.put("status", status);
List<SpTopKeyword> listuser=sptopservice.sptopshow(map);
System.out.println(listuser);
return listuser!=null?JSON.toJSONString(listuser):JsonUtil.objectStatus(4);
}
}
<?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" >
<!-- namespace命名空间,作用就是对sql进行分类化的管理,理解为sql隔离
注意:使用mapper代理开发时,namespace有特殊作用,namespace等于mapper接口地址
-->
<mapper namespace="com.yanshu.service.SpTopKeywordService" >
<!--只能针对一个表进行操作 一对一查询 -->
<select id="sptopshowID" parameterType="Map" resultType="com.yanshu.pojo.SpTopKeyword">
select * from Spider_Top_Keyword
</select>
<select id="sptopshow" parameterType="Map" resultType="com.yanshu.pojo.SpTopKeyword">
select * from Spider_Top_Keyword
<where>
<if test="status!=null and status!='' ">
status=#{status}
</if>
<if test="leveA!=null and leveA!='' ">
and leveA=#{leveA}
</if>
<if test="leveB!=null and leveB!='' ">
and leveB=#{leveB}
</if>
<if test="leveC!=null and leveC!='' ">
and leveC=#{leveC}
</if>
</where>
limit 0, 200
</select>
</mapper>
service和dao层省略....
controller层
package com.yanshu.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.yanshu.dao.SpTopKeywordDao;
import com.yanshu.dao.SpTopKeywordDaoJpa;
import com.yanshu.pojo.Business;
import com.yanshu.pojo.SpTopKeyword;
import com.yanshu.pojo.SpTopKeywordJpa;
import com.yanshu.service.SpTopKeywordService;
import com.yanshu.utils.JsonUtil;
@RestController
@CrossOrigin
public class SpTopKeywordController {
Queue<SpTopKeyword> queue = new LinkedList<>();
Queue<SpTopKeyword> queue1 = new LinkedList<>();
//任务:queue 0 和1
//查询:关键词
//update
@Autowired
private SpTopKeywordDao sptopdao;
@Autowired
private SpTopKeywordDaoJpa sptopjpa;
@Autowired
private SpTopKeywordService sptopservice;
@RequestMapping("/sptopkey")
public String getSpTopKeyword(String status,String leveC,String leveA,String leveB)
{
Map map=new HashMap();
map.put("leveA", leveA);
map.put("leveB", leveB);
map.put("leveC", leveC);
map.put("status", status);
if(status.equals("1"))
{
//得到的数据
//List<SpTopKeyword> listuser=sptopdao.getSpTopKeyword(status);
List<SpTopKeyword> listuser=sptopservice.sptopshow(map);
//把所有的数据放到Queue中
for (SpTopKeyword object : listuser) {
queue.offer(object);
}
//每次请求获取10个状态为0 的关键词。队列中保存200关键词。
List<SpTopKeyword> returnUsers=new ArrayList<>();
for(int i=1;i<=10;i++)
{
returnUsers.add(queue.poll());
}
return returnUsers!=null?JSON.toJSONString(returnUsers):JsonUtil.objectStatus(4);
}
else
{
//得到的数据
//List<SpTopKeyword> listuser=sptopdao.getSpTopKeyword(status);
List<SpTopKeyword> listuser=sptopservice.sptopshow(map);
//把所有的数据放到Queue中
for (SpTopKeyword object : listuser) {
queue1.offer(object);
}
//每次请求获取10个状态为0 的关键词。队列中保存200关键词。
List<SpTopKeyword> returnUsers=new ArrayList<>();
for(int i=1;i<=10;i++)
{
returnUsers.add(queue1.poll());
}
return returnUsers!=null?JSON.toJSONString(returnUsers):JsonUtil.objectStatus(4);
}
}
@RequestMapping("/upsptopkey")
public String updateSpTopKeyword(String status,String leveA,String leveB,String leveC)
{
Map map=new HashMap();
map.put("leveA", leveA);
map.put("leveB", leveB);
map.put("leveC", leveC);
map.put("status", status);
List<SpTopKeyword> listuser=sptopservice.sptopshow(map);
System.out.println(listuser);
return listuser!=null?JSON.toJSONString(listuser):JsonUtil.objectStatus(4);
}
}
相关文章推荐
- MyBatis中动态SQL语句完成多条件查询
- mybatis分页多条件查询指定时间段数据的sql语句
- Mybatis查询语句中的条件判断问题
- 资料 mybatis 一条语句 分页查询(同时获得符合条件的总记录数)
- MyBatis中动态SQL语句完成多条件查询
- mybatis 查询语句(按条件查询)
- Mybatis 不确定条件查询(动态语句where和if)借助实体类封装参数
- mybatis like 条件查询语句
- mybatis 脚本处理语句(条件查询,批量增删改查)
- mybatis使用in语句作为查询条件
- mybatis 中使用模糊查询语句
- 要提高SQL查询效率where语句条件的先后次序应如何写
- mybatis 在mysql查询条件日期在某两个日期之间
- mybatis执行查询语句,可以在plsql中查询数据,但是在程序中查询不到
- MyBatis基本查询、条件查询、查询排序
- Mybatis list做条件查询
- 使用SQL查询语句时,数组动态赋值SQL IN ()作为条件条件一个
- 数据库查询语句 动态加入查询条件 不区分大小写
- mybatis 查询多个条件IN的使用方法
- MyBatis系列四 之 智能标签进行查询语句的拼接