用java和mysql实现访客管理系统的黑名单增删改查功能,并进行action、dao、pojo、service分层。代码中要求不能省略getter和setter构造方法
时间: 2023-11-28 21:49:12 浏览: 78
java代码-使用java在Maven下用Spring+mysql 实现增删查改的源代码
首先,我们需要创建一个访客实体类Guest:
```java
public class Guest {
private int id;
private String name;
private String email;
private boolean isBlacklisted;
public Guest(int id, String name, String email, boolean isBlacklisted) {
this.id = id;
this.name = name;
this.email = email;
this.isBlacklisted = isBlacklisted;
}
public Guest(String name, String email, boolean isBlacklisted) {
this.name = name;
this.email = email;
this.isBlacklisted = isBlacklisted;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean isBlacklisted() {
return isBlacklisted;
}
public void setBlacklisted(boolean blacklisted) {
isBlacklisted = blacklisted;
}
}
```
然后我们需要创建一个访客服务接口GuestService:
```java
public interface GuestService {
List<Guest> getAllGuests();
List<Guest> getBlacklistedGuests();
Guest getGuestById(int id);
void addGuest(Guest guest);
void deleteGuest(int id);
void updateGuest(Guest guest);
}
```
接下来我们需要创建一个访客服务实现类GuestServiceImpl:
```java
public class GuestServiceImpl implements GuestService {
private GuestDao guestDao = new GuestDaoImpl();
@Override
public List<Guest> getAllGuests() {
return guestDao.getAllGuests();
}
@Override
public List<Guest> getBlacklistedGuests() {
return guestDao.getBlacklistedGuests();
}
@Override
public Guest getGuestById(int id) {
return guestDao.getGuestById(id);
}
@Override
public void addGuest(Guest guest) {
guestDao.addGuest(guest);
}
@Override
public void deleteGuest(int id) {
guestDao.deleteGuest(id);
}
@Override
public void updateGuest(Guest guest) {
guestDao.updateGuest(guest);
}
}
```
然后我们需要创建一个访客数据访问对象GuestDao:
```java
public interface GuestDao {
List<Guest> getAllGuests();
List<Guest> getBlacklistedGuests();
Guest getGuestById(int id);
void addGuest(Guest guest);
void deleteGuest(int id);
void updateGuest(Guest guest);
}
```
接下来我们需要创建一个访客数据访问对象实现类GuestDaoImpl:
```java
public class GuestDaoImpl implements GuestDao {
private Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:mysql://localhost:3306/guests", "root", "password");
}
@Override
public List<Guest> getAllGuests() {
List<Guest> guests = new ArrayList<>();
try (Connection conn = getConnection();
Statement stmt = conn.createStatement()) {
String sql = "SELECT * FROM guest";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String email = rs.getString("email");
boolean isBlacklisted = rs.getBoolean("is_blacklisted");
Guest guest = new Guest(id, name, email, isBlacklisted);
guests.add(guest);
}
} catch (SQLException e) {
e.printStackTrace();
}
return guests;
}
@Override
public List<Guest> getBlacklistedGuests() {
List<Guest> guests = new ArrayList<>();
try (Connection conn = getConnection();
Statement stmt = conn.createStatement()) {
String sql = "SELECT * FROM guest WHERE is_blacklisted = true";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String email = rs.getString("email");
boolean isBlacklisted = rs.getBoolean("is_blacklisted");
Guest guest = new Guest(id, name, email, isBlacklisted);
guests.add(guest);
}
} catch (SQLException e) {
e.printStackTrace();
}
return guests;
}
@Override
public Guest getGuestById(int id) {
Guest guest = null;
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM guest WHERE id = ?")) {
stmt.setInt(1, id);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
String name = rs.getString("name");
String email = rs.getString("email");
boolean isBlacklisted = rs.getBoolean("is_blacklisted");
guest = new Guest(id, name, email, isBlacklisted);
}
} catch (SQLException e) {
e.printStackTrace();
}
return guest;
}
@Override
public void addGuest(Guest guest) {
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement("INSERT INTO guest (name, email, is_blacklisted) VALUES (?, ?, ?)")) {
stmt.setString(1, guest.getName());
stmt.setString(2, guest.getEmail());
stmt.setBoolean(3, guest.isBlacklisted());
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void deleteGuest(int id) {
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement("DELETE FROM guest WHERE id = ?")) {
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void updateGuest(Guest guest) {
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement("UPDATE guest SET name = ?, email = ?, is_blacklisted = ? WHERE id = ?")) {
stmt.setString(1, guest.getName());
stmt.setString(2, guest.getEmail());
stmt.setBoolean(3, guest.isBlacklisted());
stmt.setInt(4, guest.getId());
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
最后,我们需要创建一个访客控制器GuestController:
```java
public class GuestController {
private GuestService guestService = new GuestServiceImpl();
public void displayAllGuests() {
List<Guest> guests = guestService.getAllGuests();
for (Guest guest : guests) {
System.out.println("ID: " + guest.getId() + ", Name: " + guest.getName() + ", Email: " + guest.getEmail() + ", Blacklisted: " + guest.isBlacklisted());
}
}
public void displayBlacklistedGuests() {
List<Guest> guests = guestService.getBlacklistedGuests();
for (Guest guest : guests) {
System.out.println("ID: " + guest.getId() + ", Name: " + guest.getName() + ", Email: " + guest.getEmail() + ", Blacklisted: " + guest.isBlacklisted());
}
}
public void displayGuestById(int id) {
Guest guest = guestService.getGuestById(id);
if (guest != null) {
System.out.println("ID: " + guest.getId() + ", Name: " + guest.getName() + ", Email: " + guest.getEmail() + ", Blacklisted: " + guest.isBlacklisted());
} else {
System.out.println("Guest not found!");
}
}
public void addGuest(String name, String email, boolean isBlacklisted) {
Guest guest = new Guest(name, email, isBlacklisted);
guestService.addGuest(guest);
System.out.println("Guest added successfully!");
}
public void deleteGuest(int id) {
guestService.deleteGuest(id);
System.out.println("Guest deleted successfully!");
}
public void updateGuest(int id, String name, String email, boolean isBlacklisted) {
Guest guest = new Guest(id, name, email, isBlacklisted);
guestService.updateGuest(guest);
System.out.println("Guest updated successfully!");
}
}
```
现在我们就可以在访客控制器中使用上述方法进行访客管理系统的增删改查操作了。
阅读全文