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);
}
}