Article Directory
- Java service interface implementation class
-
- 1. Create a status service interface implementation class StatusServiceImpl
- Unit test StatusServiceImpl
- (1) Write the test method testFindStatusById()
- (2) Write the test method testUpdateStatus()
- 2. Create a student service interface implementation class
- Unit test StudentServiceImpl
- (1) Write the test method testFindStudentsByName()
- (2) Write the test method testAddStudent()
- (3) Write the test method testDeleteStudentById()
- (4) Write the test method testDeleteStudentsByClass()
- (5) Write the test method testDeleteStudentsByDepartment()
- (6) Write the test method testUpdateStudent()
- (7) Write the test method testFindStudentById()
- (8) Write the test method testFindAllStudents()
- (9) Write the test method testFindStudentsByClass()
- (10) Write the test method testFindStudentsByDepartment()
- (11) Write the test method testFindRowsBySex()
- (12) Write the test method testFindRowsByClass()
- (13) Write the test method testFindRowsByDepartment()
- 3. Create a user service interface implementation class
- Unit test UserServiceImpl
- (1) Write the test method testLogin()
- (2) Write the test method testIsUsernameExisted()
- (3) Write the test method testAddUser()
- (4) Write the test method testDeleteUserById()
- (5) Write the test method testUpdateUser()
- (6) Write the test method testFindUserById()
- (7) Write the test method testFindAllUsers()
- Create application class
- Create window interface
Java service interface implementation class
In the last training, four corresponding service interfaces were established, and the school-like service interface implementation class was created. In this training, we continue to establish the service interface implementation class
1. Create a status service interface implementation class StatusServiceImpl
Create the implementation class in the sub-package impl of service: The
detailed code is:
Unit test StatusServiceImpl
Create a test class in the test package:
(1) Write the test method testFindStatusById()
The code is as follows: the
operation result is:
(2) Write the test method testUpdateStatus()
The running result is:
2. Create a student service interface implementation class
The detailed code is:
package net.lyq.student.service.impl;
import net.lyq.student.bean.Student;
import net.lyq.student.dao.StudentDao;
import net.lyq.student.dao.impl.StudentDaoImpl;
import net.lyq.student.service.StudentService;
import java.util.List;
import java.util.Vector;
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao = new StudentDaoImpl();
@Override
public int addStudent(Student student) {
return studentDao.insert(student);
}
@Override
public int deleteStudentById(String id) {
return studentDao.deleteById(id);
}
@Override
public int deleteStudentsByClass(String clazz) {
return studentDao.deleteByClass(clazz);
}
@Override
public int deleteStudentsByDepartment(String department) {
return studentDao.deleteByDepartment(department);
}
@Override
public int updateStudent(Student student) {
return studentDao.update(student);
}
@Override
public Student findStudentById(String id) {
return studentDao.findById(id);
}
@Override
public List<Student> findStudentByName(String name) {
return studentDao.findByName(name);
}
@Override
public List<Student> findStudentsByClass(String clazz) {
return studentDao.findByClass(clazz);
}
@Override
public List<Student> findStudentsByDepartment(String department) {
return studentDao.findByDepartment(department);
}
@Override
public List<Student> findAllStudents() {
return studentDao.findAll();
}
@Override
public Vector findRowsBySex() {
return studentDao.findRowsBySex();
}
@Override
public Vector findRowsByClass() {
return studentDao.findRowsByClass();
}
@Override
public Vector findRowsByDepartment() {
return studentDao.findRowsByDepartment();
}
}
Unit test StudentServiceImpl
(1) Write the test method testFindStudentsByName()
(2) Write the test method testAddStudent()
operation result:
(3) Write the test method testDeleteStudentById()
Running result:
data sheet:
(4) Write the test method testDeleteStudentsByClass()
(5) Write the test method testDeleteStudentsByDepartment()
(6) Write the test method testUpdateStudent()
(7) Write the test method testFindStudentById()
(8) Write the test method testFindAllStudents()
(9) Write the test method testFindStudentsByClass()
(10) Write the test method testFindStudentsByDepartment()
(11) Write the test method testFindRowsBySex()
(12) Write the test method testFindRowsByClass()
(13) Write the test method testFindRowsByDepartment()
3. Create a user service interface implementation class
package net.lyq.student.service.impl;
import net.lyq.student.bean.User;
import net.lyq.student.dao.UserDao;
import net.lyq.student.dao.impl.UserDaoImpl;
import net.lyq.student.service.UserService;
import java.util.List;
public class UserServiceImpl implements UserService {
/**
* 声明用户数据访问对象
*/
private UserDao userDao = new UserDaoImpl();
@Override
public int addUser(User user) {
return userDao.insert(user);
}
@Override
public int deleteUserById(int id) {
return userDao.deleteById(id);
}
@Override
public List<User> findAllUsers() {
return userDao.findAll();
}
@Override
public User findUserById(int id) {
return userDao.findById(id);
}
@Override
public User login(String username, String password) {
return userDao.login(username, password);
}
@Override
public int updateUser(User user) {
return userDao.update(user);
}
@Override
public boolean isUsernameExisted(String username) {
return userDao.isUsernameExisted(username);
}
}
Unit test UserServiceImpl
(1) Write the test method testLogin()
operation result:
(2) Write the test method testIsUsernameExisted()
operation result:
Modify the user name to change the running result
(3) Write the test method testAddUser()
operation result:
(4) Write the test method testDeleteUserById()
operation result:
(5) Write the test method testUpdateUser()
operation result:
(6) Write the test method testFindUserById()
operation result:
(7) Write the test method testFindAllUsers()
Operation result: The
above are all test classes of all service interface classes
Create application class
Create the app sub-package in the net.lyq.student package and create the Application class
Write the code as shown in the class, an error occurs, because the three windows have not been created yet
Create the gui package to store the three window classes. After the
creation is over, check the Application again
Create window interface
As mentioned above, create three windows as shown in the figure, and then you can write the window class
1. Create the main interface window MainFrame
package net.lyq.student.gui;
import net.lyq.student.app.Application;
import net.lyq.student.bean.Status;
import net.lyq.student.service.StatusService;
import net.lyq.student.service.impl.StatusServiceImpl;
import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MainFrame extends JFrame {
/**
* 构造方法
* @param title
*/
public MainFrame(String title){
super(title);
initGUI();
}
private Status status;//状态对象
private StatusService statusService;//状态服务对象
/**
* 初始化图形界面
*/
public void initGUI(){
//创建状态服务对象
statusService = new StatusServiceImpl();
//获取状态对象
status = statusService.findStatusById(1);
//设置窗口尺寸
setSize(800,640);
//设置窗口可见
setVisible(true);
setLocationRelativeTo(null);
//设置窗口标题
setTitle("学生信息管理系统"+status.getVersion());
//设置窗口默认关闭方式
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//注册窗口监听器,创建窗口适配器,编写事件处理代码
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
exitSystem();
}
});
}
/**
* 退出系统,询问用户是否退出
*/
private void exitSystem() {
int choice = JOptionPane.showConfirmDialog(this,"您是否要退出系统",
"学生信息管理系统",JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_NO_OPTION){
System.exit(0);
}else{
//卸载当前窗口
dispose();
//重新显示主窗口
Application.mainFrame = new MainFrame("学生信息管理系统"+status.getVersion());
}
}
public static void main(String[] args) {
Application.mainFrame = new MainFrame("");
}
}
2. Create a user login window LoginFrame
package net.lyq.student.gui;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import net.lyq.student.app.Application;
import net.lyq.student.bean.Status;
import net.lyq.student.bean.User;
import net.lyq.student.service.StatusService;
import net.lyq.student.service.UserService;
import net.lyq.student.service.impl.StatusServiceImpl;
import net.lyq.student.service.impl.UserServiceImpl;
public class LoginFrame extends JFrame{
private String username;
private String password;
private JLabel lblUsername;
private JLabel lblPassword;
private JTextField txtUsername;
private JPasswordField txtPassword;
private JButton btnOK;
private JButton btnCancel;
private JButton btnRegister;
private JPanel panel,panel1,panel2,panel3;
public LoginFrame(String title) {
super(title);
initGUI();
}
private void initGUI() {
panel = (JPanel) getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
lblUsername = new JLabel("用户名:");
lblPassword = new JLabel("密码:");
txtUsername = new JTextField(15);
txtPassword = new JPasswordField(15);
btnOK = new JButton("确定");
btnCancel = new JButton("取消");
btnRegister = new JButton("注册");
panel1.add(lblUsername);
panel1.add(txtUsername);
panel2.add(lblPassword);
panel2.add(txtPassword);
panel3.add(btnOK);
panel3.add(btnCancel);
panel3.add(btnRegister);
panel.setLayout(new GridLayout(3,1));
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
btnOK.setMnemonic(KeyEvent.VK_O);
btnCancel.setMnemonic(KeyEvent.VK_C);
btnRegister.setMnemonic(KeyEvent.VK_R);
txtPassword.setEchoChar('*');
setSize(250, 200);
setLocationRelativeTo(null);
setResizable(false);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
login();
}
});
btnOK.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
login();
}
}
});
txtUsername.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
txtUsername.requestFocus();
}
});
txtPassword.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
txtPassword.requestFocus();
}
});
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
btnRegister.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Application.loginFrame.setVisible(false);
Application.registerFrame = new RegisterFrame("用户注册");
}
});
}
private void login() {
username = txtUsername.getText().trim();
password = new String(txtPassword.getPassword());
UserService userService = new UserServiceImpl();
User user = userService.login(username, password);
if (user != null) {
Application.loginFrame.setVisible(false);
StatusService statusService = new StatusServiceImpl();
Status status = statusService.findStatusById(1);
Application.id = user.getId();
Application.username = user.getUsername();
Application.password = user.getPassword();
JOptionPane.showMessageDialog(null, "欢迎使用学生信息管理系统" + status.getVersion(),"用户登录",JOptionPane.INFORMATION_MESSAGE);
Application.mainFrame = new MainFrame("");
Application.loginFrame.dispose();
}else {
Application.loginFrame.setVisible(false);
JOptionPane.showMessageDialog(null, "用户名或密码错误,请重新输入","用户登录",JOptionPane.ERROR_MESSAGE);
Application.loginFrame.setVisible(true);
txtUsername.selectAll();
txtPassword.selectAll();
txtUsername.requestFocus();
}
}
public static void main(String[] args) {
Application.loginFrame = new LoginFrame("用户登录");
}
}
In this code, an error occurs in RegisterFrame, and the class needs to be modified