添加了可读的注释

develop
wht 11 months ago
parent 2fa7e8f259
commit cd2e82bfac

@ -1,39 +1,49 @@
// test
package com.inks.hb.authinfo.controller; package com.inks.hb.authinfo.controller;
import com.inks.hb.authinfo.pojo.AuthInfo; import com.inks.hb.authinfo.pojo.AuthInfo; // 引入AuthInfo实体类
import com.inks.hb.authinfo.service.AuthService; import com.inks.hb.authinfo.service.AuthService; // 引入AuthService接口
import com.inks.hb.authinfo.service.AuthServiceImpl; import com.inks.hb.authinfo.service.AuthServiceImpl; // 引入AuthServiceImpl实现类
import com.inks.hb.common.ExportExcel; import com.inks.hb.common.ExportExcel; // 引入ExportExcel工具类
import javax.servlet.annotation.WebServlet; import javax.servlet.annotation.WebServlet; // 引入@WebServlet注解
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet; // 引入HttpServlet类
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest; // 引入HttpServletRequest接口
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse; // 引入HttpServletResponse接口
import java.sql.SQLException; import java.sql.SQLException; // 引入SQLException异常类
import java.util.ArrayList; import java.util.ArrayList; // 引入ArrayList类
// 使用@WebServlet注解定义Servlet的名称和URL映射
@WebServlet(name = "AuthInfoExcelServlet", value = "/AuthInfoExcelServlet") @WebServlet(name = "AuthInfoExcelServlet", value = "/AuthInfoExcelServlet")
public class AuthInfoExcelServlet extends HttpServlet { public class AuthInfoExcelServlet extends HttpServlet {
// 重写doPost方法调用doGet方法
protected void doPost(HttpServletRequest request, HttpServletResponse response) { protected void doPost(HttpServletRequest request, HttpServletResponse response) {
this.doGet(request, response); this.doGet(request, response);
} }
// 重写doGet方法处理HTTP GET请求
protected void doGet(HttpServletRequest request, HttpServletResponse response) { protected void doGet(HttpServletRequest request, HttpServletResponse response) {
// 创建AuthService实例
AuthService service = new AuthServiceImpl(); AuthService service = new AuthServiceImpl();
// 声明并初始化AuthInfo列表
ArrayList<AuthInfo> infoArrayList = null; ArrayList<AuthInfo> infoArrayList = null;
try { try {
// 查询所有AuthInfo并赋值给infoArrayList
infoArrayList = service.query(1, service.queryAuthInfoNum()); infoArrayList = service.query(1, service.queryAuthInfoNum());
} catch (SQLException e) { } catch (SQLException e) {
// 捕获并打印SQLException异常
e.printStackTrace(); e.printStackTrace();
} }
// 定义Excel表头
String[] headers = {"权限ID", "权限名称", "可读", "可写", "可改", "可删"}; String[] headers = {"权限ID", "权限名称", "可读", "可写", "可改", "可删"};
// 定义导出的Excel文件名
String fileName = "权限信息"; String fileName = "权限信息";
// 创建ExportExcel实例
ExportExcel<AuthInfo> ee = new ExportExcel<>(); ExportExcel<AuthInfo> ee = new ExportExcel<>();
// 调用exportExcel方法导出Excel文件
ee.exportExcel(headers, infoArrayList, fileName, response); ee.exportExcel(headers, infoArrayList, fileName, response);
} }
} }

@ -16,48 +16,52 @@ import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
/** /**
* * Servlet
* '' * ''
* pojotoGson * PojotoGson
*/ */
@WebServlet(value = "/AuthInfoServlet", name = "AuthInfoServlet") @WebServlet(value = "/AuthInfoServlet", name = "AuthInfoServlet")
public class AuthInfoServlet extends HttpServlet { public class AuthInfoServlet extends HttpServlet {
// 重写doPost方法调用doGet方法以处理POST请求
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
this.doGet(request, response); this.doGet(request, response);
} }
// 重写doGet方法处理GET请求
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
// 设置请求和响应的字符编码
request.setCharacterEncoding("utf-8"); request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8"); response.setContentType("text/html;charset=utf-8");
// 获取响应输出流
PrintWriter out = response.getWriter(); PrintWriter out = response.getWriter();
// 从请求中获取分页参数和操作类型
int page = Integer.parseInt(request.getParameter("page")); // 当前页码 int page = Integer.parseInt(request.getParameter("page")); // 当前页码
int limit = Integer.parseInt(request.getParameter("limit")); // 每页的数据量 int limit = Integer.parseInt(request.getParameter("limit")); // 每页的数据量
int make = Integer.parseInt(request.getParameter("make")); int make = Integer.parseInt(request.getParameter("make")); // 操作类型
// 调用service // 创建AuthService实例
AuthService service = new AuthServiceImpl(); AuthService service = new AuthServiceImpl();
// 默认输出信息 // 初始化返回信息
String code = "0"; //状态码 String code = "0"; // 状态码默认为0表示成功
String msg = "数据查询正常"; //状态信息 String msg = "数据查询正常"; // 状态信息,默认为正常
String count = ""; //数据总数 String count = ""; // 数据总数,初始化为空字符串
ArrayList<AuthInfo> list = new ArrayList<>(); //数据内容 ArrayList<AuthInfo> list = new ArrayList<>(); // 数据内容列表
//单个全局属性 // 声明单个全局属性
int authId; // 权限ID int authId; // 权限ID
String authItem = ""; // 权限名称 String authItem = ""; // 权限名称
String isRead; // 可读 String isRead; // 可读
String isWrite; // 可写 String isWrite; // 可写
String isChange; // 可改 String isChange; // 可改
String isDelete; // 可删 String isDelete; // 可删
AuthInfo authInfo = null; AuthInfo authInfo = null; // AuthInfo对象
try { try {
// 根据操作类型执行不同的逻辑
// 状态标志 make 0重载 1新增 2修改 3搜索 4删除
if (make == 2) { if (make == 2) {
// 修改权限信息
authId = Integer.parseInt(request.getParameter("authId")); authId = Integer.parseInt(request.getParameter("authId"));
authItem = request.getParameter("authItem"); authItem = request.getParameter("authItem");
isRead = request.getParameter("isRead"); isRead = request.getParameter("isRead");
@ -65,38 +69,33 @@ public class AuthInfoServlet extends HttpServlet {
isChange = request.getParameter("isChange"); isChange = request.getParameter("isChange");
isDelete = request.getParameter("isDelete"); isDelete = request.getParameter("isDelete");
authInfo = new AuthInfo(authId, authItem, isRead, isWrite, isChange, isDelete); authInfo = new AuthInfo(authId, authItem, isRead, isWrite, isChange, isDelete);
service.updateAuthInfo(authInfo); // 更新权限信息
} else if (make == 3) { } else if (make == 3) {
// 搜索权限信息
authItem = request.getParameter("authItem"); authItem = request.getParameter("authItem");
authInfo = service.query(authItem); // 查询权限信息
list.clear(); // 清空列表
list.add(authInfo); // 添加查询结果到列表
} }
switch (make) { // 根据操作类型获取数据
case 2:
service.updateAuthInfo(authInfo);
break;
case 3:
authInfo = service.query(authItem);
list.clear();
list.add(authInfo);
break;
}
if (make != 3) { if (make != 3) {
list = service.query(page, limit); list = service.query(page, limit); // 分页查询权限信息
count = String.valueOf(service.queryAuthInfoNum()); count = String.valueOf(service.queryAuthInfoNum()); // 获取权限信息总数
} else { } else {
if (authInfo.getAuthId() == 0) { // 如果搜索结果为空则设置count为0否则为1
count = "0"; count = (authInfo.getAuthId() == 0) ? "0" : "1";
} else {
count = "1";
}
} }
} catch (SQLException e) { } catch (SQLException e) {
// 捕获到SQLException异常设置错误状态码和信息
code = "1"; code = "1";
msg = "数据查询出现异常"; msg = "数据查询出现异常";
e.printStackTrace(); e.printStackTrace(); // 打印异常堆栈信息
} finally { } finally {
// 创建PojotoGson对象并序列化为JSON字符串
PojotoGson pojotoGson = new PojotoGson(code, msg, count, list); PojotoGson pojotoGson = new PojotoGson(code, msg, count, list);
Gson gson = new Gson(); Gson gson = new Gson();
out.print(gson.toJson(pojotoGson)); out.print(gson.toJson(pojotoGson)); // 输出JSON字符串
} }
} }
} }

@ -10,48 +10,58 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
// 实现CommonDao接口提供AuthInfo数据访问对象
public class AuthInfoDao implements CommonDao { public class AuthInfoDao implements CommonDao {
// 插入AuthInfo数据到数据库
@Override @Override
public void insertData(Object o) throws SQLException { public void insertData(Object o) throws SQLException {
AuthInfo authInfo = (AuthInfo) o; AuthInfo authInfo = (AuthInfo) o; // 将Object对象转换为AuthInfo对象
Connection conn = DBUtil.getConnection(); Connection conn = DBUtil.getConnection(); // 获取数据库连接
// SQL插入语句
String sql = "insert into authInfo (authItem, isRead, isWrite, isChange, isDelete) values (?,?,?,?,?)"; String sql = "insert into authInfo (authItem, isRead, isWrite, isChange, isDelete) values (?,?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql); PreparedStatement pstmt = conn.prepareStatement(sql);
// 设置SQL语句中的参数
pstmt.setString(1, authInfo.getAuthItem()); pstmt.setString(1, authInfo.getAuthItem());
pstmt.setString(2, authInfo.getIsRead()); pstmt.setString(2, authInfo.getIsRead());
pstmt.setString(3, authInfo.getIsWrite()); pstmt.setString(3, authInfo.getIsWrite());
pstmt.setString(4, authInfo.getIsChange()); pstmt.setString(4, authInfo.getIsChange());
pstmt.setString(5, authInfo.getIsDelete()); pstmt.setString(5, authInfo.getIsDelete());
pstmt.executeUpdate(); pstmt.executeUpdate(); // 执行插入操作
pstmt.close(); pstmt.close(); // 关闭PreparedStatement
} }
// 删除AuthInfo数据
@Override @Override
public void deleteData(Object o) throws SQLException { public void deleteData(Object o) throws SQLException {
AuthInfo authInfo = (AuthInfo) o; AuthInfo authInfo = (AuthInfo) o; // 将Object对象转换为AuthInfo对象
Connection conn = DBUtil.getConnection(); Connection conn = DBUtil.getConnection(); // 获取数据库连接
// SQL删除语句
String sql = "DELETE FROM authInfo WHERE authId = ?"; String sql = "DELETE FROM authInfo WHERE authId = ?";
PreparedStatement pstmt = conn.prepareStatement(sql); PreparedStatement pstmt = conn.prepareStatement(sql);
// 设置SQL语句中的参数
pstmt.setInt(1, authInfo.getAuthId()); pstmt.setInt(1, authInfo.getAuthId());
pstmt.executeUpdate(); pstmt.executeUpdate(); // 执行删除操作
pstmt.close(); pstmt.close(); // 关闭PreparedStatement
} }
// 更新AuthInfo数据
@Override @Override
public void updateData(Object o) throws SQLException { public void updateData(Object o) throws SQLException {
AuthInfo authInfo = (AuthInfo) o; AuthInfo authInfo = (AuthInfo) o; // 将Object对象转换为AuthInfo对象
Connection conn = DBUtil.getConnection(); Connection conn = DBUtil.getConnection(); // 获取数据库连接
// SQL更新语句
String sql = "UPDATE authInfo SET authItem = ? ,isRead = ?,isWrite = ?,isChange = ?,isDelete = ? WHERE authId = ?"; String sql = "UPDATE authInfo SET authItem = ? ,isRead = ?,isWrite = ?,isChange = ?,isDelete = ? WHERE authId = ?";
PreparedStatement pstmt = conn.prepareStatement(sql); PreparedStatement pstmt = conn.prepareStatement(sql);
// 设置SQL语句中的参数
pstmt.setString(1, authInfo.getAuthItem()); pstmt.setString(1, authInfo.getAuthItem());
pstmt.setString(2, authInfo.getIsRead()); pstmt.setString(2, authInfo.getIsRead());
pstmt.setString(3, authInfo.getIsWrite()); pstmt.setString(3, authInfo.getIsWrite());
@ -59,53 +69,59 @@ public class AuthInfoDao implements CommonDao {
pstmt.setString(5, authInfo.getIsDelete()); pstmt.setString(5, authInfo.getIsDelete());
pstmt.setInt(6, authInfo.getAuthId()); pstmt.setInt(6, authInfo.getAuthId());
pstmt.executeUpdate(); pstmt.executeUpdate(); // 执行更新操作
pstmt.close(); pstmt.close(); // 关闭PreparedStatement
} }
// 查询AuthInfo数据总数
@Override @Override
public int queryDataNum() throws SQLException { public int queryDataNum() throws SQLException {
Connection conn = DBUtil.getConnection(); // 获取数据库连接
Connection conn = DBUtil.getConnection(); // SQL查询总数语句
String sql = "select count(*) from authInfo;"; String sql = "select count(*) from authInfo;";
PreparedStatement pstmt = conn.prepareStatement(sql); PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery(); ResultSet rs = pstmt.executeQuery(); // 执行查询操作
int num; int num;
if (rs.next()) num = rs.getInt("count(*)"); if (rs.next()) {
else num = 0; num = rs.getInt("count(*)"); // 获取总数
} else {
num = 0; // 如果没有数据总数为0
}
rs.close(); rs.close(); // 关闭ResultSet
pstmt.close(); pstmt.close(); // 关闭PreparedStatement
return num; return num; // 返回数据总数
} }
// 分页查询AuthInfo数据
@Override @Override
public ArrayList<AuthInfo> query(int start, int length) throws SQLException { public ArrayList<AuthInfo> query(int start, int length) throws SQLException {
Connection conn = DBUtil.getConnection(); // 获取数据库连接
Connection conn = DBUtil.getConnection(); // SQL分页查询语句
String sql = "select * from authInfo limit ?, ?;"; String sql = "select * from authInfo limit ?, ?;";
PreparedStatement pstmt = conn.prepareStatement(sql); PreparedStatement pstmt = conn.prepareStatement(sql);
// 设置分页参数
pstmt.setInt(1, start - 1); pstmt.setInt(1, start - 1);
pstmt.setInt(2, length); pstmt.setInt(2, length);
ResultSet rs = pstmt.executeQuery(); ResultSet rs = pstmt.executeQuery(); // 执行查询操作
ArrayList<AuthInfo> list = new ArrayList<>(); ArrayList<AuthInfo> list = new ArrayList<>(); // 创建AuthInfo列表
AuthInfo authInfo; AuthInfo authInfo;
while (rs.next()) { while (rs.next()) { // 遍历结果集
authInfo = new AuthInfo(rs.getInt(1), rs.getString(2), rs.getString(3) // 创建AuthInfo对象并添加到列表
, rs.getString(4), rs.getString(5), rs.getString(6)); authInfo = new AuthInfo(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6));
list.add(authInfo); list.add(authInfo);
} }
rs.close(); rs.close(); // 关闭ResultSet
pstmt.close(); pstmt.close(); // 关闭PreparedStatement
return list; return list; // 返回AuthInfo列表
} }
@Override @Override

Loading…
Cancel
Save