添加搜索历史

This commit is contained in:
cyk
2023-07-03 10:24:50 +08:00
parent 9c4e657396
commit df7d6207eb
3 changed files with 47 additions and 13 deletions

View File

@@ -231,4 +231,23 @@ public class UserDao {
}
}
}
public int addLabel(String username,String str){
String label;
userEntity userEntity;
int num;
try {
Connection conn = JdbcUtils.getConnection();
QueryRunner runner = new QueryRunner();
String sql = "select searchHistory from user where userName=?";
userEntity = runner.query(conn, sql, new BeanHandler<>(userEntity.class),username);
label = userEntity.getSearchHistory()+str+";";
sql="UPDATE user SET searchHistory = ? WHERE userName= ?";
num = runner.update(conn,sql,label,username);
} catch (SQLException e) {
throw new RuntimeException(e);
}
return num;
}
}

View File

@@ -57,4 +57,6 @@ public class UserService {
public long queryUserCount(){return userDao.queryUserCount();}
public int contributionValueReturn(String name){return userDao.contributionValueReturn(name);}
public int addLabel(String username,String str){return userDao.addLabel(username,str);}
}

View File

@@ -3,6 +3,7 @@ package com.hellogithub.servlet;
import com.alibaba.fastjson.JSONObject;
import com.hellogithub.entity.projectEntity;
import com.hellogithub.entity.userEntity;
import com.hellogithub.service.UserService;
import com.hellogithub.service.projectService;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
@@ -19,6 +20,7 @@ import java.util.List;
@WebServlet("/selectByInput")
public class selectByInputServlet extends HttpServlet {
private com.hellogithub.service.projectService projectService=new projectService();
private UserService userService = new UserService();
@Override
public void init(ServletConfig config) throws ServletException {
@@ -41,7 +43,7 @@ public class selectByInputServlet extends HttpServlet {
resp.setHeader("Access-Control-Max-Age", "3600");
resp.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With");
resp.setContentType("application/json;charset=UTF-8");
//返回最新期刊数字
//添加搜索历史
PrintWriter writer = resp.getWriter();
String str = req.getParameter("xxxxx");
List<projectEntity> projectEntityList = projectService.selectByInput(str);
@@ -51,24 +53,35 @@ public class selectByInputServlet extends HttpServlet {
userEntity=(userEntity)session.getAttribute("user");
if(userEntity == null)
{
if(projectEntityList.size() == 0 ){
setResultError("查询结果为空",writer);
}
else{
String jsonString = JSONObject.toJSONString(projectEntityList);
setResultOK("success",writer);
writer.println(jsonString);
writer.close();
setResultOK("success",writer);
}
}
else{
String username = userEntity.getUserName();
int num = userService.addLabel(username,str);
if(projectEntityList.size() == 0 ){
setResultError("查询结果为空",writer);
}
else{
String jsonString = JSONObject.toJSONString(projectEntityList);
setResultOK("success",writer);
writer.println(jsonString);
writer.close();
setResultOK("success",writer);
}
}
if(projectEntityList.size() == 0 ){
setResultError("查询结果为空",writer);
}
else{
String jsonString = JSONObject.toJSONString(projectEntityList);
setResultOK("success",writer);
writer.println(jsonString);
writer.close();
setResultOK("success",writer);
}
}
public void setResult(Integer code, String msg, PrintWriter writer) {