添加评论
This commit is contained in:
@@ -251,6 +251,22 @@ public class UserDao {
|
||||
return num;
|
||||
}
|
||||
|
||||
public int selectIdByName(String name )
|
||||
{
|
||||
int num ;
|
||||
userEntity userEntity;
|
||||
try {
|
||||
Connection conn = JdbcUtils.getConnection();
|
||||
QueryRunner runner = new QueryRunner();
|
||||
String sql = "select * from user where userName=? and isValid = 1";
|
||||
userEntity = runner.query(conn, sql, new BeanHandler<>(userEntity.class),name);
|
||||
num = userEntity.getUserId();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
public String selectNameById(int id)
|
||||
{
|
||||
String label;
|
||||
|
@@ -26,4 +26,18 @@ public class commentDao {
|
||||
}
|
||||
return commentEntityList;
|
||||
}
|
||||
//插入评论
|
||||
public int insertComment(int userid,int projectId,String content,int isUsed,String commentTime,int star){
|
||||
int num ;
|
||||
try {
|
||||
Connection conn = JdbcUtils.getConnection();
|
||||
QueryRunner runner = new QueryRunner();
|
||||
String sql = "INSERT INTO comment (userId,projectId,content,isUsed,commentTime,isValid,star) VALUES (?,?,?,?,?,1,?)";
|
||||
num = runner.update(conn, sql,userid,projectId,content,isUsed,commentTime,star);
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return num;
|
||||
}
|
||||
}
|
||||
|
@@ -36,4 +36,9 @@ public class commentService {
|
||||
}
|
||||
return JSONArray.toJSONString(dataMap2);
|
||||
}
|
||||
|
||||
public int insertComment(String username,int projectId,String content,int isUsed,String commentTime,int star){
|
||||
int userid = userDao.selectIdByName(username);
|
||||
return commentDao.insertComment(userid,projectId,content,isUsed,commentTime,star);
|
||||
}
|
||||
}
|
||||
|
59
src/com/hellogithub/servlet/commentServlet.java
Normal file
59
src/com/hellogithub/servlet/commentServlet.java
Normal file
@@ -0,0 +1,59 @@
|
||||
package com.hellogithub.servlet;
|
||||
|
||||
import com.hellogithub.entity.userEntity;
|
||||
import com.hellogithub.service.commentService;
|
||||
import jakarta.servlet.ServletConfig;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.annotation.WebServlet;
|
||||
import jakarta.servlet.http.HttpServlet;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
@WebServlet("/comment")
|
||||
public class commentServlet extends HttpServlet {
|
||||
private com.hellogithub.service.commentService commentService = new commentService();
|
||||
@Override
|
||||
public void init(ServletConfig config) throws ServletException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
doPost(req, resp);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
resp.setCharacterEncoding("UTF-8");
|
||||
resp.setContentType("text/html; charset=utf-8");
|
||||
// 设置响应头允许ajax跨域访问
|
||||
String curOrigin = req.getHeader("Origin");
|
||||
resp.setHeader("Access-Control-Allow-Origin", curOrigin == null ? "true" : curOrigin);
|
||||
resp.setHeader("Access-Control-Allow-Credentials", "true");
|
||||
resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD");
|
||||
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");
|
||||
//按照分类检索
|
||||
String proId = req.getParameter("id");
|
||||
String star = req.getParameter("star");
|
||||
String content = req.getParameter("content");
|
||||
String isUsed = req.getParameter("isUsed");
|
||||
DateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
|
||||
Date time = new Date();
|
||||
String now = format.format(time).toString();
|
||||
|
||||
PrintWriter writer = resp.getWriter();
|
||||
HttpSession session =req.getSession();
|
||||
userEntity userEntity=(userEntity)session.getAttribute("user");
|
||||
String username=userEntity.getUserName();
|
||||
writer.println(commentService.insertComment(username,Integer.parseInt(proId),content,Integer.parseInt(isUsed),now,Integer.parseInt(star)));
|
||||
writer.close();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user