假期作业5
查询所有:
点击查看代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>列表</title>
<style>
/* 全局样式 */
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f4f7fa;
color: #333;
margin: 0;
padding: 20px;
}
/* 表单样式 */
form {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
display: flex;
align-items: center;
gap: 10px;
}
form label {
font-weight: 600;
}
form input[type="text"] {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
flex-grow: 1;
}
form input[type="submit"] {
background-color: #007BFF;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
}
form input[type="submit"]:hover {
background-color: #0056b3;
}
/* 表格样式 */
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
table th,
table td {
padding: 12px;
text-align: center;
border-bottom: 1px solid #ddd;
}
table th {
background-color: #007BFF;
color: white;
}
table tr:nth-child(even) {
background-color: #f2f2f2;
}
table tr:hover {
background-color: #e0e0e0;
}
table a {
color: #007BFF;
text-decoration: none;
margin: 0 5px;
}
table a:hover {
text-decoration: underline;
}
/* 按钮样式 */
button {
background-color: #007BFF;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
margin-right: 10px;
}
button:hover {
background-color: #0056b3;
}
</style>
<script>
function Back() {
window.location.href = "main.jsp";
}
function addBase() {
window.location.href = "addBase.jsp";
}
</script>
</head>
<body>
<form name="search-form" action="selectOneServlet" method="post">
<label for="assessmentId"></label>
<input type="text" id="assessmentId" name="assessmentId">
<input value="根据编号查询" type="submit" id="search_btn">
</form>
<table border="1" cellspacing="0" width="100%">
<tr>
<th>评估编号</th>
<th>评估日期</th>
<th>评估原因</th>
<th>姓名</th>
<th>身份证号</th>
<th>联系人</th>
<th>联系人电话</th>
<th>操作</th>
</tr>
<c:forEach items="${bases}" var="base">
<tr align="center">
<td>${base.assessmentId}</td>
<td>${base.assessmentDate}</td>
<td>${base.assessmentReason}</td>
<td>${base.name}</td>
<td>${base.idCard}</td>
<td>${base.contactName}</td>
<td>${base.contactPhone}</td>
<td>
<a href="${pageContext.request.contextPath}/update1Servlet?id=${base.assessmentId}">修改</a>
<a href="${pageContext.request.contextPath}/deleteServlet?id=${base.assessmentId}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<button onclick="addBase()">新增基本信息</button>
<button onclick="Back()">返回首页</button>
</body>
</html>
mapper:
点击查看代码
package com.vivy.mapper;
import com.vivy.pojo.Base;
import java.util.List;
public interface BaseMapper {
void add(Base base);
List<Base> selectAll();
}
mapper.xml:
点击查看代码
<?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">
<!--接口全路径名-->
<mapper namespace="com.vivy.mapper.BaseMapper">
<insert id="add" parameterType="com.vivy.pojo.Base">
INSERT INTO tb_base (
assessmentId, assessmentDate, assessmentReason, name, gender, birthDate,
idCard, socialCard, ethnicity, educationLevel, religious, maritalStatus,
livingCondition, medicalPayment, economicSource, dementia, mentalIllness,
chronicDiseases, fall, gettingLost, choking, suicide, otherInfo,
informantName, relationship, contactName, contactPhone
) VALUES (
#{assessmentId}, #{assessmentDate}, #{assessmentReason}, #{name}, #{gender}, #{birthDate},
#{idCard}, #{socialCard}, #{ethnicity}, #{educationLevel}, #{religious}, #{maritalStatus},
#{livingCondition}, #{medicalPayment}, #{economicSource}, #{dementia}, #{mentalIllness},
#{chronicDiseases}, #{fall}, #{gettingLost}, #{choking}, #{suicide}, #{otherInfo},
#{informantName}, #{relationship}, #{contactName}, #{contactPhone}
)
</insert>
<select id="selectAll" resultType="com.vivy.pojo.Base">
SELECT * FROM tb_base
</select>
</mapper>
servlet:
点击查看代码
package com.vivy.web;
import com.vivy.pojo.Base;
import com.vivy.service.BaseService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/selectAllServlet")
public class selectAllServlet extends HttpServlet {
private BaseService service = new BaseService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//# = new String(classId.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
List<Base> bases = service.selectAll();
request.setAttribute("bases",bases);
request.getRequestDispatcher("/base.jsp").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
service:
点击查看代码
package com.vivy.service;
import com.vivy.mapper.BaseMapper;
import com.vivy.pojo.Base;
import com.vivy.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
public class BaseService {
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
public void add(Base base){
SqlSession sqlSession = sqlSessionFactory.openSession();
BaseMapper baseMapper = sqlSession.getMapper(BaseMapper.class);
baseMapper.add(base);
sqlSession.commit();
sqlSession.close();
}
public List<Base> selectAll(){
SqlSession sqlSession = sqlSessionFactory.openSession();
BaseMapper baseMapper = sqlSession.getMapper(BaseMapper.class);
List<Base> bases = baseMapper.selectAll();
sqlSession.close();
return bases;
}
}

浙公网安备 33010602011771号