请帮我挑选出这段代码的主要部分@RequiresPermissions("system:record:list") List<Integer> activeIds = new ArrayList<>(); if(!Objects.equals(teamUserActiveRecord.getActiveName(), "")){ List<TeamActive> teamActives = teamActiveService.selectTeamActiveList(new TeamActive(teamUserActiveRecord.getActiveName())); System.out.println(teamActives.size()); for (TeamActive teamActive : teamActives) { activeIds.add(teamActive.getActiveId()); } System.out.println(activeIds); } startPage(); List<TeamUserActiveRecord> list = teamUserActiveRecordService.selectTeamUserActiveRecordList(new TeamUserActiveRecord(0,0)); if(Objects.equals(teamUserActiveRecord.getuName(), "") && activeIds.size()==0){ TeamUserActiveRecord teamUserActiveRecord1 = new TeamUserActiveRecord(); List<TeamUserActiveRecord> list1 = teamUserActiveRecordService.selectTeamUserActiveRecordList(teamUserActiveRecord1); for (TeamUserActiveRecord userActiveRecord : list1) { userActiveRecord.setActiveName(teamActiveService.selectTeamActiveByActiveId(userActiveRecord.getActiveId()).getActiveName()); System.out.println(userActiveRecord); userActiveRecord.setUserName(sysUserService.selectUserById(userActiveRecord.getUserId()).getUserName()); } return getDataTable(list1); }else if(!Objects.equals(teamUserActiveRecord.getuName(), "") && activeIds.size()==0){ List<TeamUserActiveRecord> list2 = teamUserActiveRecordService.selectTeamUserActiveRecordList(new TeamUserActiveRecord(0,0)); SysUser sysUser = new SysUser(); sysUser.setUserName(teamUserActiveRecord.getuName()); List<SysUser> sysUsers = sysUserService.selectUserList(sysUser); if(sysUsers.size()!=0){ for (SysUser user : sysUsers) { List<TeamUserActiveRecord> teamUserActiveRecords = teamUserActiveRecordService.selectTeamUserActiveRecordListByUserId(user.getUserId()); for (TeamUserActiveRecord userActiveRecord : teamUserActiveRecords) { userActiveRecord.setActiveName(teamActiveService.selectTeamActiveByActiveId(userActiveRecord.getActiveId()).getActiveName()); userActiveRecord.setUserName(sysUserService.selectUserById(userActiveRecord.getUserId()).getUserName()); } if (list2 == null) { list2 = teamUserActiveRecords; }else { list2.addAll(teamUserActiveRecords); } } list = list2; } }else if (Objects.equals(teamUserActiveRecord.getuName(), "") && activeIds.size()!=0){ List<TeamUserActiveRecord> records = teamUserActiveRecordService.selectTeamCommentRecordListByIds(activeIds); for (TeamUserActiveRecord record : records) { record.setActiveName(teamActiveService.selectTeamActiveByActiveId(record.getActiveId()).getActiveName()); record.setUserName(sysUserService.selectUserById(record.getUserId()).getUserName()); } list = records; } return getDataTable(list); }
时间: 2023-06-20 17:04:11 浏览: 158
这段代码主要分为三个部分:
1. 检查用户是否有权限访问该记录列表 @RequiresPermissions("system:record:list")
2. 根据前端传来的参数查询符合条件的记录列表,并且将结果封装成一个 DataTable 返回给前端
3. 如果没有查询到记录,则根据不同的情况返回不同的结果,具体如下:
a. 如果 activeIds 集合为空并且 uName 为空,则返回全部记录列表
b. 如果 activeIds 集合为空并且 uName 不为空,则返回对应用户的全部记录列表
c. 如果 activeIds 集合不为空并且 uName 为空,则返回对应活动的全部记录列表
相关问题
帮我用Java写一个实现k匿名的代码
好的,这里给你一个简单的Java代码实现K匿名算法,供你参考:
```java
import java.util.*;
public class KAnonymity {
// 用于存储数据集的列表
private List<Map<String, String>> dataset;
// 敏感性属性的名称
private String sensitiveAttribute;
// 敏感性属性值的敏感性等级
private Map<String, Integer> sensitivityLevels;
// K值
private int k;
/**
* 构造函数,初始化数据集、敏感性属性、敏感性等级和K值
*/
public KAnonymity(List<Map<String, String>> dataset, String sensitiveAttribute, Map<String, Integer> sensitivityLevels, int k) {
this.dataset = dataset;
this.sensitiveAttribute = sensitiveAttribute;
this.sensitivityLevels = sensitivityLevels;
this.k = k;
}
/**
* 实现K匿名算法
*/
public List<Map<String, String>> anonymize() {
List<Map<String, String>> result = new ArrayList<>();
// 循环直到所有记录都被匿名化
while (!dataset.isEmpty()) {
// 筛选出所有未被匿名化的记录
List<Map<String, String>> unanonymizedRecords = getUnanonymizedRecords();
if (unanonymizedRecords.isEmpty()) {
break;
}
// 按照敏感性级别进行排序
Collections.sort(unanonymizedRecords, new RecordComparator(sensitiveAttribute, sensitivityLevels));
// 对于每个等级,将记录按照该属性值进行分组
Map<String, List<Map<String, String>>> groups = new HashMap<>();
for (Map<String, String> record : unanonymizedRecords) {
String sensitiveValue = record.get(sensitiveAttribute);
List<Map<String, String>> group = groups.get(sensitiveValue);
if (group == null) {
group = new ArrayList<>();
groups.put(sensitiveValue, group);
}
group.add(record);
}
// 对于每个分组,检查是否满足K匿名条件,如果不满足,则进行匿名化操作
for (List<Map<String, String>> group : groups.values()) {
if (group.size() < k) {
// 如果分组大小小于K值,则将所有记录通用化敏感属性值
for (Map<String, String> record : group) {
record.put(sensitiveAttribute, "*");
}
result.addAll(group);
} else {
// 如果分组大小大于等于K值,则随机生成虚假值
String sensitiveValue = group.get(0).get(sensitiveAttribute);
List<String> fakeValues = generateFakeValues(sensitiveValue, sensitivityLevels.get(sensitiveValue), k);
for (int i = 0; i < group.size(); i++) {
Map<String, String> record = group.get(i);
record.put(sensitiveAttribute, fakeValues.get(i));
}
result.addAll(group);
}
// 将已匿名化的记录从原始数据集中删除
dataset.removeAll(group);
}
}
return result;
}
/**
* 获取所有未被匿名化的记录
*/
private List<Map<String, String>> getUnanonymizedRecords() {
List<Map<String, String>> unanonymizedRecords = new ArrayList<>();
for (Map<String, String> record : dataset) {
if (!"*".equals(record.get(sensitiveAttribute))) {
unanonymizedRecords.add(record);
}
}
return unanonymizedRecords;
}
/**
* 随机生成虚假值
*/
private List<String> generateFakeValues(String sensitiveValue, int sensitivityLevel, int count) {
List<String> fakeValues = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < count; i++) {
fakeValues.add(generateFakeValue(sensitiveValue, sensitivityLevel, random));
}
return fakeValues;
}
/**
* 随机生成单个虚假值
*/
private String generateFakeValue(String sensitiveValue, int sensitivityLevel, Random random) {
int sensitivityRange = (int) Math.pow(10, sensitivityLevel);
int minValue = Integer.parseInt(sensitiveValue) / sensitivityRange * sensitivityRange;
int maxValue = minValue + sensitivityRange - 1;
return Integer.toString(minValue + random.nextInt(sensitivityRange));
}
/**
* 用于比较记录的类,按照指定属性的敏感性等级进行排序
*/
private class RecordComparator implements Comparator<Map<String, String>> {
private String sensitiveAttribute;
private Map<String, Integer> sensitivityLevels;
public RecordComparator(String sensitiveAttribute, Map<String, Integer> sensitivityLevels) {
this.sensitiveAttribute = sensitiveAttribute;
this.sensitivityLevels = sensitivityLevels;
}
@Override
public int compare(Map<String, String> o1, Map<String, String> o2) {
int level1 = sensitivityLevels.get(o1.get(sensitiveAttribute));
int level2 = sensitivityLevels.get(o2.get(sensitiveAttribute));
return Integer.compare(level2, level1);
}
}
}
```
使用方法:
```java
// 测试数据集
List<Map<String, String>> dataset = new ArrayList<>();
Map<String, String> record1 = new HashMap<>();
record1.put("name", "Alice");
record1.put("age", "20");
record1.put("zip", "12345");
record1.put("gender", "F");
record1.put("salary", "50000");
dataset.add(record1);
Map<String, String> record2 = new HashMap<>();
record2.put("name", "Bob");
record2.put("age", "25");
record2.put("zip", "67890");
record2.put("gender", "M");
record2.put("salary", "60000");
dataset.add(record2);
Map<String, String> record3 = new HashMap<>();
record3.put("name", "Carol");
record3.put("age", "30");
record3.put("zip", "12345");
record3.put("gender", "F");
record3.put("salary", "70000");
dataset.add(record3);
// 敏感性属性和敏感性等级
String sensitiveAttribute = "salary";
Map<String, Integer> sensitivityLevels = new HashMap<>();
sensitivityLevels.put("50000", 1);
sensitivityLevels.put("60000", 2);
sensitivityLevels.put("70000", 3);
// K值
int k = 2;
// 创建KAnonymity对象并执行匿名化
KAnonymity anonymizer = new KAnonymity(dataset, sensitiveAttribute, sensitivityLevels, k);
List<Map<String, String>> result = anonymizer.anonymize();
// 输出匿名化结果
for (Map<String, String> record : result) {
System.out.println(record);
}
```
这里的数据集包含了姓名、年龄、邮编、性别和工资等属性,其中工资是敏感性属性,其敏感性等级由敏感性值决定。K值是2,表示每个分组的大小必须大于等于2。在执行匿名化之后,输出匿名化结果。
请给出基于Java、mysql、tomcat、html的web候选人投票系统的详细代码
好的,以下是基于Java、MySQL、Tomcat和HTML的Web候选人投票系统的详细代码示例:
1. 数据库设计
创建数据库vote,其中包括两个表格candidate和vote_record。
```mysql
CREATE DATABASE vote;
USE vote;
CREATE TABLE candidate (
id INT(11) PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
photo VARCHAR(100),
description VARCHAR(255)
);
CREATE TABLE vote_record (
id INT(11) PRIMARY KEY AUTO_INCREMENT,
user_id INT(11) NOT NULL,
candidate_id INT(11) NOT NULL,
vote_time DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES user(id),
FOREIGN KEY (candidate_id) REFERENCES candidate(id)
);
```
2. Java Web项目搭建
创建一个新的Java Web项目vote_system,添加如下依赖:
- MySQL JDBC驱动
- Tomcat Servlet API
- JSP API
创建一个名为DBUtil的Java类,用于封装数据库操作。
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/vote?useSSL=false&serverTimezone=UTC";
private static final String USER = "root";
private static final String PASSWORD = "password";
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn, PreparedStatement pstmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
3. 用户登录
创建一个名为LoginServlet的Servlet,用于处理用户登录请求。
```java
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
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 javax.servlet.http.HttpSession;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
pstmt = conn.prepareStatement("SELECT id FROM user WHERE username=? AND password=?");
pstmt.setString(1, username);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if (rs.next()) {
HttpSession session = request.getSession();
session.setAttribute("user_id", rs.getInt("id"));
response.sendRedirect("candidate_list.jsp");
} else {
response.sendRedirect("login.jsp?error=1");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt, rs);
}
}
}
```
4. 候选人列表
创建一个名为CandidateListServlet的Servlet,用于获取候选人列表并在JSP页面中展示。
```java
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/candidate_list")
public class CandidateListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
pstmt = conn.prepareStatement("SELECT id, name, photo, description FROM candidate");
rs = pstmt.executeQuery();
List<Candidate> candidates = new ArrayList<Candidate>();
while (rs.next()) {
Candidate candidate = new Candidate();
candidate.setId(rs.getInt("id"));
candidate.setName(rs.getString("name"));
candidate.setPhoto(rs.getString("photo"));
candidate.setDescription(rs.getString("description"));
candidates.add(candidate);
}
request.setAttribute("candidates", candidates);
request.getRequestDispatcher("candidate_list.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt, rs);
}
}
}
```
5. 投票
创建一个名为VoteServlet的Servlet,用于处理用户投票请求。
```java
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
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 javax.servlet.http.HttpSession;
@WebServlet("/vote")
public class VoteServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int candidateId = Integer.parseInt(request.getParameter("candidate_id"));
HttpSession session = request.getSession();
int userId = (int) session.getAttribute("user_id");
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
pstmt = conn.prepareStatement("SELECT id FROM vote_record WHERE user_id=? AND candidate_id=?");
pstmt.setInt(1, userId);
pstmt.setInt(2, candidateId);
rs = pstmt.executeQuery();
if (rs.next()) {
response.sendRedirect("candidate_list.jsp?error=1");
} else {
pstmt = conn.prepareStatement("INSERT INTO vote_record (user_id, candidate_id) VALUES (?, ?)");
pstmt.setInt(1, userId);
pstmt.setInt(2, candidateId);
pstmt.executeUpdate();
response.sendRedirect("candidate_list.jsp");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt, rs);
}
}
}
```
6. JSP页面
最后创建一个名为candidate_list.jsp的JSP页面,用于展示候选人列表并处理用户投票请求。
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>候选人列表</title>
<style type="text/css">
.candidate {
margin: 10px;
padding: 10px;
border: 1px solid #ddd;
display: inline-block;
text-align: center;
vertical-align: top;
}
.candidate img {
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<h1>候选人列表</h1>
<c:if test="${not empty param.error}">
<p style="color: red;">您已经投过票了,请勿重复投票!</p>
</c:if>
<c:forEach var="candidate" items="${candidates}">
<div class="candidate">
<img src="${candidate.photo}" />
<p>${candidate.name}</p>
<p>${candidate.description}</p>
<form method="post" action="vote">
<input type="hidden" name="candidate_id" value="${candidate.id}" />
<button type="submit">投票</button>
</form>
</div>
</c:forEach>
</body>
</html>
```
以上是基于Java、MySQL、Tomcat和HTML的Web候选人投票系统的详细代码示例。希望能对您有所帮助。
阅读全文