2024.4.11
完成了node的组合条件查询和分页
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package com.hongliang.videotask.MapperProvider;
|
||||
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class NodeProvider {
|
||||
/**
|
||||
* 组合条件查询
|
||||
* @param condition 包含pageNum,pageSize,查询字段项,orderby
|
||||
* @return
|
||||
*/
|
||||
public String GetNodeList(Map<String,Object> condition){
|
||||
// System.out.println("condition:"+condition.toString());
|
||||
// 使用StringBuilder来拼接SQL
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sb.append("select * from node where 1=1");
|
||||
|
||||
//开如拼接查询条件
|
||||
if (condition.containsKey("nodename")){
|
||||
sb.append(" and nodename like concat('%', #{nodename},'%')");
|
||||
}
|
||||
|
||||
//……如果有其他条件,就继续
|
||||
|
||||
//如果有排序
|
||||
if (condition.containsKey("orderby")){
|
||||
sb.append(" order by #{orderby}");
|
||||
}
|
||||
|
||||
//拼接分页
|
||||
if (condition.containsKey("offset") && condition.containsKey("pageSize")){
|
||||
sb.append(" limit #{offset},#{pageSize}");
|
||||
}
|
||||
|
||||
//返回sql语句
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
13
src/main/java/com/hongliang/videotask/bean/NodeBean.java
Normal file
13
src/main/java/com/hongliang/videotask/bean/NodeBean.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.hongliang.videotask.bean;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class NodeBean {
|
||||
private Integer id;
|
||||
private String nodename;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.hongliang.videotask.controller;
|
||||
|
||||
import com.hongliang.videotask.common.Response;
|
||||
import com.hongliang.videotask.common.ResponseCode;
|
||||
import com.hongliang.videotask.service.impl.NodeServiceImpl;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/node")
|
||||
public class NodeControl {
|
||||
@Resource
|
||||
private NodeServiceImpl nodeService;
|
||||
|
||||
@PostMapping("list")
|
||||
public ResponseEntity<?> GetNodeListByCondition(@RequestBody String condition){
|
||||
System.out.println(condition);
|
||||
return ResponseEntity.ok(new Response(ResponseCode.OK,"success",
|
||||
this.nodeService.GetNodeListByCondition(condition)));
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,11 @@ package com.hongliang.videotask.controller;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.hongliang.videotask.bean.LoginBean;
|
||||
import com.hongliang.videotask.bean.LoginResultBean;
|
||||
import com.hongliang.videotask.bean.UserBean;
|
||||
import com.hongliang.videotask.common.Response;
|
||||
import com.hongliang.videotask.common.ResponseCode;
|
||||
import com.hongliang.videotask.service.impl.LoginServiceImpl;
|
||||
import com.hongliang.videotask.service.impl.UserServiceImpl;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@@ -18,6 +20,8 @@ import java.time.format.DateTimeFormatter;
|
||||
public class UserControl {
|
||||
@Resource
|
||||
private LoginServiceImpl loginService;
|
||||
@Resource
|
||||
private UserServiceImpl userService;
|
||||
|
||||
@GetMapping("hello")
|
||||
public ResponseEntity<?> hello(){
|
||||
@@ -48,6 +52,12 @@ public class UserControl {
|
||||
|
||||
@PostMapping("logout")
|
||||
public ResponseEntity<?> logout(){
|
||||
StpUtil.logout();
|
||||
return ResponseEntity.ok(new Response(ResponseCode.OK,"成功退出",null));
|
||||
}
|
||||
|
||||
@GetMapping("getuserbyid")
|
||||
public ResponseEntity<?> GetUserByID(@RequestParam("id") int id){
|
||||
return ResponseEntity.ok(new Response(ResponseCode.OK,"成功",userService.GetUserByID(id)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.hongliang.videotask.mappers;
|
||||
|
||||
import com.hongliang.videotask.MapperProvider.NodeProvider;
|
||||
import com.hongliang.videotask.bean.NodeBean;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.SelectProvider;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Repository
|
||||
public interface NodeMapper {
|
||||
@SelectProvider(type= NodeProvider.class,method = "GetNodeList")
|
||||
List<NodeBean> GetNodeListByCondition(Map<String,Object> condition);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.hongliang.videotask.service;
|
||||
|
||||
import com.hongliang.videotask.bean.NodeBean;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface NodeService {
|
||||
List<NodeBean> GetNodeListByCondition(String condition);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.hongliang.videotask.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.hongliang.videotask.bean.NodeBean;
|
||||
import com.hongliang.videotask.mappers.NodeMapper;
|
||||
import com.hongliang.videotask.service.NodeService;
|
||||
import com.hongliang.videotask.utils.CommonFuction;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class NodeServiceImpl implements NodeService {
|
||||
@Autowired
|
||||
private NodeMapper nodeMapper;
|
||||
|
||||
public List<NodeBean> GetNodeListByCondition(String condition){
|
||||
Map<String,Object> map= CommonFuction.JsonStringToMapWithOffset(condition);
|
||||
return this.nodeMapper.GetNodeListByCondition(map);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.hongliang.videotask.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class CommonFuction {
|
||||
/**
|
||||
* 将一个带pageNum和pageSize的json对象,转换为Map对象,供mybatis的provider使用,并添加offset
|
||||
* @param jsonStr 一个包含查询条件和分页信息的json对象
|
||||
* @return Map<String,Object>对象
|
||||
*/
|
||||
public static Map<String,Object> JsonStringToMapWithOffset(String jsonStr){
|
||||
JSONObject jsonObject= JSON.parseObject(jsonStr);
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
for (String key : jsonObject.keySet()) {
|
||||
map.put(key, jsonObject.get(key));
|
||||
}
|
||||
int pagenum=Integer.parseInt( map.get("pageNum").toString());
|
||||
int pagesize=Integer.parseInt(map.get("pageSize").toString());
|
||||
map.put("offset",(pagenum-1)*pagesize);
|
||||
// System.out.println("service condition:"+map.toString());
|
||||
return map;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,20 @@
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class LoginTest {
|
||||
@Test
|
||||
public void testAdd(){
|
||||
System.out.println("this is a test!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMap(){
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("key1", 1);
|
||||
params.put("key2", 2);
|
||||
|
||||
System.out.println(params);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user