在student下面创建service包:
相关代码:
package net.xsp.student.service;
import net.xsp.student.bean.College;
/**
package net.xsp.student.service;
import net.xsp.student.bean.Status;
/**
package net.xsp.student.service;
/**
import java.util.List;
import java.util.Vector;
import net.xsp.student.bean.Student;
public interface StudentService {
int addStudent(Student student);
int deleteStudentById(String id);
int deleteStudentsByClass(String clazz);
int deleteStudentsByDepartment(String department);
int updateStudent(Student student);
Student findStudentById(String id);
List findStudentsByName(String name);
List findStudentsByClass(String clazz);
List findStudentsByDepartment(String department);
List findAllStudents();
Vector findRowsBySex();
Vector findRowsByClass();
Vector findRowsByDepartment();
}
package net.xsp.student.service;
import net.xsp.student.bean.User;
import java.util.List;
/**
在service包下面建立impl包:
相关代码:
package net.xsp.student.service.impl;
/**
import net.xsp.student.bean.College;
import net.xsp.student.dao.CollegeDao;
import net.xsp.student.dao.impl.CollegeDaoImpl;
import net.xsp.student.service.CollegeService;
public class CollegeServiceImpl implements CollegeService {
/**
* 声明学校数据访问对象
*/
private CollegeDao collegeDao = new CollegeDaoImpl();
@Override
public College findCollegeById(int id) {
return collegeDao.findById(id);
}
@Override
public int updateCollege(College college) {
return collegeDao.update(college);
}
}
package net.xsp.student.service.impl;
/**
import net.xsp.student.bean.Status;
import net.xsp.student.dao.StatusDao;
import net.xsp.student.dao.impl.StatusDaoImpl;
import net.xsp.student.service.StatusService;
public class StatusServiceImpl implements StatusService{
/**
* 声明状态数据访问对象
*/
private StatusDao statusDao = new StatusDaoImpl();
@Override
public Status findStatusById(int id) {
return statusDao.findById(id);
}
@Override
public int updateStatus(Status status) {
return statusDao.update(status);
}
}
package net.xsp.student.service.impl;
/**
import java.util.List;
import java.util.Vector;
import net.xsp.student.bean.Student;
import net.xsp.student.dao.StudentDao;
import net.xsp.student.dao.impl.StudentDaoImpl;
import net.xsp.student.service.StudentService;
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 List findAllStudents() {
return studentDao.findAll();
}
@Override
public Vector findRowsByClass() {
return studentDao.findRowsByClass();
}
@Override
public Vector findRowsByDepartment() {
return studentDao.findRowsByDepartment();
}
@Override
public Vector findRowsBySex() {
return studentDao.findRowsBySex();
}
@Override
public Student findStudentById(String id) {
return studentDao.findById(id);
}
@Override
public List findStudentsByClass(String clazz) {
return studentDao.findByClass(clazz);
}
@Override
public List findStudentsByDepartment(String department) {
return studentDao.findByDepartment(department);
}
@Override
public List findStudentsByName(String name) {
return studentDao.findByName(name);
}
@Override
public int updateStudent(Student student) {
return studentDao.update(student);
}
}
package net.xsp.student.service.impl;
/**
import java.util.List;
import net.xsp.student.bean.User;
import net.xsp.student.dao.UserDao;
import net.xsp.student.dao.impl.UserDaoImpl;
import net.xsp.student.service.UserService;
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 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);
}
}
测试类:
package net.xsp.student.test;
import net.xsp.student.bean.College;
import net.xsp.student.service.CollegeService;
import net.xsp.student.service.impl.CollegeServiceImpl;
import org.junit.Test;
/**
功能:测试学校服务接口实现类
作者:向仕平
日期:2019年6月19日
*/
public class TestCollegeServiceImpl {
@Test
public void testFindCollegeById() {
CollegeService service = new CollegeServiceImpl();
College college = service.findCollegeById(1);
System.out.println(college);
}
@Test
public void testUpdateCollege() {
CollegeService service = new CollegeServiceImpl();
College college = service.findCollegeById(1);
college.setPresident(“王洪礼”);
college.setTelephone(“3152639”);
int count = service.updateCollege(college);
if (count > 0) {
System.out.println(“恭喜,学校记录更新成功!”);
college = service.findCollegeById(1);
System.out.println(college);
} else {
System.out.println(“遗憾,学校记录更新失败!”);
}
}
}
package net.xsp.student.test;
import net.xsp.student.bean.Status;
import net.xsp.student.service.StatusService;
import net.xsp.student.service.impl.StatusServiceImpl;
import org.junit.Test;
/**
功能:测试状态服务接口实现类
作者:向仕平
日期:2019年6月19日
*/
public class TestStatusServiceImpl {
@Test
public void testFindStatuById() {
StatusService service = new StatusServiceImpl();
Status ststus = service.findStatusById(1);
System.out.println(ststus);
}
@Test
public void testUpdateStatus() {
StatusService service = new StatusServiceImpl();
Status status = service.findStatusById(1);
status.setAuthor(“无心剑”);
status.setTelephone(“17837378246”);
int count = service.updateStatus(status);
if (count > 0) {
System.out.println(“恭喜,状态记录更新成功!”);
status = service.findStatusById(1);
System.out.println(status);
} else {
System.out.println(“遗憾,状态记录更新失败!”);
}
}
}
package net.xsp.student.test;
import net.xsp.student.bean.Student;
import net.xsp.student.service.StudentService;
import net.xsp.student.service.impl.StudentServiceImpl;
import org.junit.Test;
import java.util.List;
/**
package net.xsp.student.test;
import net.xsp.student.bean.User;
import net.xsp.student.service.UserService;
import net.xsp.student.service.impl.UserServiceImpl;
import org.junit.Test;
/**
功能:测试用户服务接口实现类
作者:向仕平
日期:2019年6月19日
*/
public class TestUserServiceImpl {
@Test
public void testLogin() {
UserService service = new UserServiceImpl();
String username, password;
username = "admin";
password = "12345";
User user = service.login(username, password);
if (user != null) {
System.out.println("恭喜,用户名与密码正确,登录成功!");
} else {
System.out.println("遗憾,用户名或密码错误,登录失败!");
}
}
}
在student下面建立app包:
package net.xsp.student.app;
/**
import net.xsp.student.gui.LoginFrame;
import net.xsp.student.gui.MainFrame;
import net.xsp.student.gui.RegisterFrame;
public class Application {
/**
* 登录用户标识符
/
public static int id;
/*
* 登录用户名
/
public static String username;
/*
* 登录窗口
/
public static LoginFrame loginFrame;
/*
* 主窗口
/
public static MainFrame mainFrame;
/*
* 注册窗口
*/
public static RegisterFrame registerFrame;
}