使用Java书写,运用数据访问层、业务逻辑层、UI层三层架构设计完成用户管理模块,完成用户登录业务设计
时间: 2024-01-14 21:02:52 浏览: 57
用户登录模块的三层实现
好的,我会尽力回答您的问题。下面是一个可能的Java实现方案:
1. 三层架构设计
首先,我们需要创建三个包,分别为dao、service和ui,用于存放数据访问层、业务逻辑层和UI层的代码。其中,dao包用于存放与数据库交互的代码,service包用于存放业务逻辑的代码,ui包用于存放用户界面的代码。
2. 用户管理模块设计
接下来,我们可以开始实现用户管理模块。首先,我们需要在dao包中创建一个UserDao类,用于实现对用户信息的增删改查等操作。具体实现可以使用JDBC或ORM框架(如Hibernate、MyBatis)。
在service包中,我们需要创建一个UserService类,用于处理用户管理模块的业务逻辑。具体实现可以包括对用户信息的校验、权限控制等。
在ui包中,我们需要创建一个UserManageUI类,用于呈现用户管理模块的界面。具体实现可以包括用户信息的显示、输入等。下面是一个可能的UserManageUI类的代码示例:
```
package ui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import service.UserService;
import entity.User;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class UserManageUI extends JFrame {
private JPanel contentPane;
private JTextField txtUserId;
private JTextField txtUserName;
private JTextField txtPassword;
private JTable table;
private DefaultTableModel model;
private UserService userService;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UserManageUI frame = new UserManageUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public UserManageUI() {
userService = new UserService();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblUserId = new JLabel("用户ID:");
lblUserId.setBounds(10, 10, 54, 15);
contentPane.add(lblUserId);
txtUserId = new JTextField();
txtUserId.setBounds(74, 7, 66, 21);
contentPane.add(txtUserId);
txtUserId.setColumns(10);
JLabel lblUserName = new JLabel("用户名:");
lblUserName.setBounds(150, 10, 54, 15);
contentPane.add(lblUserName);
txtUserName = new JTextField();
txtUserName.setBounds(214, 7, 66, 21);
contentPane.add(txtUserName);
txtUserName.setColumns(10);
JLabel lblPassword = new JLabel("密码:");
lblPassword.setBounds(290, 10, 54, 15);
contentPane.add(lblPassword);
txtPassword = new JTextField();
txtPassword.setBounds(344, 7, 66, 21);
contentPane.add(txtPassword);
txtPassword.setColumns(10);
JButton btnAdd = new JButton("添加");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 获取用户输入的信息
int userId = Integer.parseInt(txtUserId.getText());
String userName = txtUserName.getText();
String password = txtPassword.getText();
// 封装用户信息
User user = new User();
user.setUserId(userId);
user.setUserName(userName);
user.setPassword(password);
// 调用UserService的addUser方法添加用户
boolean result = userService.addUser(user);
if (result) {
JOptionPane.showMessageDialog(null, "添加成功!");
// 刷新表格
refreshTable();
} else {
JOptionPane.showMessageDialog(null, "添加失败!");
}
}
});
btnAdd.setBounds(10, 38, 93, 23);
contentPane.add(btnAdd);
JButton btnDelete = new JButton("删除");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 获取用户选中的行
int row = table.getSelectedRow();
if (row < 0) {
JOptionPane.showMessageDialog(null, "请选择要删除的行!");
return;
}
// 获取选中行的用户ID
int userId = Integer.parseInt(model.getValueAt(row, 0).toString());
// 调用UserService的deleteUser方法删除用户
boolean result = userService.deleteUser(userId);
if (result) {
JOptionPane.showMessageDialog(null, "删除成功!");
// 刷新表格
refreshTable();
} else {
JOptionPane.showMessageDialog(null, "删除失败!");
}
}
});
btnDelete.setBounds(113, 38, 93, 23);
contentPane.add(btnDelete);
JButton btnUpdate = new JButton("修改");
btnUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 获取用户选中的行
int row = table.getSelectedRow();
if (row < 0) {
JOptionPane.showMessageDialog(null, "请选择要修改的行!");
return;
}
// 获取选中行的用户ID
int userId = Integer.parseInt(model.getValueAt(row, 0).toString());
// 获取用户输入的信息
String userName = txtUserName.getText();
String password = txtPassword.getText();
// 封装用户信息
User user = new User();
user.setUserId(userId);
user.setUserName(userName);
user.setPassword(password);
// 调用UserService的updateUser方法修改用户
boolean result = userService.updateUser(user);
if (result) {
JOptionPane.showMessageDialog(null, "修改成功!");
// 刷新表格
refreshTable();
} else {
JOptionPane.showMessageDialog(null, "修改失败!");
}
}
});
btnUpdate.setBounds(216, 38, 93, 23);
contentPane.add(btnUpdate);
JButton btnSearch = new JButton("查询");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 获取用户输入的信息
int userId = Integer.parseInt(txtUserId.getText());
String userName = txtUserName.getText();
// 调用UserService的getUser方法查询用户
User user = userService.getUser(userId, userName);
// 如果查询结果不为空,将结果显示在输入框中
if (user != null) {
txtUserId.setText(String.valueOf(user.getUserId()));
txtUserName.setText(user.getUserName());
txtPassword.setText(user.getPassword());
} else {
JOptionPane.showMessageDialog(null, "用户不存在!");
}
}
});
btnSearch.setBounds(319, 38, 93, 23);
contentPane.add(btnSearch);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 71, 422, 180);
contentPane.add(scrollPane);
table = new JTable();
model = new DefaultTableModel(
new Object[][] {},
new String[] { "用户ID", "用户名", "密码" }
);
table.setModel(model);
scrollPane.setViewportView(table);
// 初始时,显示所有用户信息
refreshTable();
}
/**
* 刷新表格
*/
private void refreshTable() {
// 清空表格数据
model.setRowCount(0);
// 调用UserService的getAllUsers方法获取所有用户
List<User> userList = userService.getAllUsers();
// 将用户信息添加到表格中
for (User user : userList) {
Object[] rowData = { user.getUserId(), user.getUserName(), user.getPassword() };
model.addRow(rowData);
}
}
}
```
3. 用户登录业务设计
最后,我们需要在ui包中创建一个LoginUI类,用于呈现用户登录的界面。具体实现可以包括用户输入用户名和密码的输入框、登录按钮等。下面是一个可能的LoginUI类的代码示例:
```
package ui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import service.UserService;
import entity.User;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class LoginUI extends JFrame {
private JPanel contentPane;
private JTextField txtUserName;
private JTextField txtPassword;
private UserService userService;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginUI frame = new LoginUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LoginUI() {
userService = new UserService();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblUserName = new JLabel("用户名:");
lblUserName.setBounds(98, 73, 54, 15);
contentPane.add(lblUserName);
txtUserName = new JTextField();
txtUserName.setBounds(162, 70, 150, 21);
contentPane.add(txtUserName);
txtUserName.setColumns(10);
JLabel lblPassword = new JLabel("密码:");
lblPassword.setBounds(98, 124, 54, 15);
contentPane.add(lblPassword);
txtPassword = new JTextField();
txtPassword.setBounds(162, 121, 150, 21);
contentPane.add(txtPassword);
txtPassword.setColumns(10);
JButton btnLogin = new JButton("登录");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 获取用户输入的用户名和密码
String userName = txtUserName.getText();
String password = txtPassword.getText();
// 调用UserService的getUser方法查询用户
User user = userService.getUser(userName, password);
// 如果查询结果不为空,提示登录成功
if (user != null) {
JOptionPane.showMessageDialog(null, "登录成功!");
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
}
}
});
btnLogin.setBounds(162, 167, 93, 23);
contentPane.add(btnLogin);
}
}
```
通过以上代码示例,我们可以完成使用Java运用数据访问层、业务逻辑层、UI层三层架构设计完成用户管理模块,完成用户登录业务设计。
阅读全文