实训第四次主要是测试数据访问接口实现类。本次实训主要是依据前面创建的数据访问类进行扩展创建面向用户的服务层
一、创建服务接口
1、创建学校服务接口:
package net.dc.student.service;
import net.dc.student.bean.College;
/**
* 功能:学校服务接口
* 作者:邓聪
* 日期:2020年07月08日
*/
public interface CollegeService {
College findCollegeById(int id);
int updateCollege(College college);
}
2、创建状态服务接口:
package net.dc.student.service;
import net.dc.student.bean.Status;
/**
* 功能:状态服务接口
* 作者:邓聪
* 日期:2020年07月08日
*/
public interface StatusService {
Status findStatusById(int id);
int updateStatus(Status status);
}
3、创建学生服务接口:
package net.dc.student.service;
import net.dc.student.bean.Student;
import java.util.List;
import java.util.Vector;
/**
* 功能:学生服务接口
* 作者:邓聪
* 日期:2020年07月08日
*/
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<Student> findStudentsByName(String name);
List<Student> findStudentsByClass(String clazz);
List<Student> findStudentsByDepartment(String department);
List<Student> findAllStudents();
Vector findRowsBySex();
Vector findRowsByClass();
Vector findRowsByDepartment();
}
4、创建用户服务接口:
package net.dc.student.service;
import net.dc.student.bean.User;
import java.util.List;
/**
* 功能:用户服务接口
* 作者:邓聪
* 日期:2020年07月08日
*/
public interface UserService {
int addUser(User user);
int deleteUserById(int id);
int updateUser(User user);
User findUserById(int id);
List<User> findAllUsers();
User login(String username, String password);
boolean isUsernameExisted(String username);
}
二、创建服务接口实现类并进行测试
1、创建学校服务接口实现类并进行测试
(1)、学校服务接口实现类CollegeServiceImpl:
package net.dc.student.service.impl;
import net.dc.student.bean.College;
import net.dc.student.dao.CollegeDao;
import net.dc.student.dao.impl.CollegeDaoImpl;
import net.dc.student.service.CollegeService;
/**
* 功能:学校服务接口实现类
* 作者:邓聪
* 日期:2020年07月08日
*/
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);
}
}
(2)、创建TestCollegeServiceImpl类进行测试:
package net.dc.student.test;
import net.dc.student.bean.College;
import net.dc.student.service.CollegeService;
import net.dc.student.service.impl.CollegeServiceImpl;
import org.junit.Test;
/**
* 功能:测试学校服务接口实现类
* 作者:邓聪
* 日期:2020年07月08日
*/
public class TestCollegeServiceImpl {
CollegeService service = new CollegeServiceImpl();
@Test
public void testFindCollegeById() {
College college = service.findCollegeById(1);
System.out.println("校名:" + college.getName());
System.out.println("校长:" + college.getPresident());
System.out.println("地址:" + college.getAddress());
System.out.println("邮箱:" + college.getEmail());
System.out.println("电话:" + college.getTelephone());
}
@Test
public void testUpdateCollege(){
College college = new College();
college.setPresident("石昊");
service.updateCollege(college);
}
}
a、运行testFindCollegeById方法
b、运行testUpdateCollege方法
可以看到,遇见了程序员的的爱恨——报错
接下来改写代码:
@Test
public void testUpdateCollege() {
College college = service.findCollegeById(1);
college.setPresident("石昊");
int count = service.updateCollege(college);
if (count > 0) {
System.out.println("信息修改成功!");
} else {
System.out.println("信息修改失败!");
}
}
进行测试:
查看数据表:
错误总结:这几天在学习其他知识,Java学习稍微落后,遗忘了部分代码;在updateCollege方法中应该传入完整的College类的对象
2、创建状态服务接口实现类并进行测试
(1)、创建状态服务接口实现类:
package net.dc.student.service.impl;
import net.dc.student.bean.Status;
import net.dc.student.dao.StatusDao;
import net.dc.student.dao.impl.StatusDaoImpl;
import net.dc.student.service.StatusService;
/**
* 功能:状态服务接口实现类
* 作者:邓聪
* 日期:2020年07月10日
*/
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);
}
}
(2)、创建TestStatusServiceImpl类进行测试:
package net.dc.student.test;
import net.dc.student.bean.Status;
import net.dc.student.service.impl.StatusServiceImpl;
import org.junit.Test;
/**
* 功能:测试状态服务接口实现类
* 作者:邓聪
* 日期:2020年07月10日
*/
public class TestStatusServiceImpl {
//创建状态服务接口实现类对象
StatusServiceImpl statusService = new StatusServiceImpl();
@Test
public void testFindStatusById() {
int id = 1;
Status status = statusService.findStatusById(id);
if (status == null) {
System.out.println("未查询到信息!");
} else {
System.out.println(status);
}
}
@Test
public void testUpdateStatus() {
Status status = statusService.findStatusById(1);
status.setAuthor("唐志聪");
int count = statusService.updateStatus(status);
if (count > 0){
System.out.println("数据更新成功!");
System.out.println(status);
}else{
System.out.println("数据更新失败!");
}
}
}
a、运行testFindStatusById方法
b、运行testUpdateStatus方法
3、创建学生服务接口实现类并进行测试
(1)、创建学生服务接口实现类
package net.dc.student.service.impl;
import net.dc.student.bean.Student;
import net.dc.student.dao.StudentDao;
import net.dc.student.dao.impl.StudentDaoImpl;
import net.dc.student.service.StudentService;
import java.util.List;
import java.util.Vector;
/**
* 功能:创建学生服务接口实现类
* 作者;邓聪
* 日期:2020年07月10日
*/
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> findStudentsByName(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();
}
}
(2)、创建TestStudentServiceImpl进行测试
package net.dc.student.test;
import net.dc.student.bean.Student;
import net.dc.student.service.StudentService;
import net.dc.student.service.impl.StudentServiceImpl;
import org.junit.Test;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
public class TestStudentServiceImpl {
StudentService service = new StudentServiceImpl();
@Test
public void testAddStudent() {
Student student = service.findStudentById("20193401");
student.setId("20193402");
student.setName("张三丰");
int count = service.addStudent(student);
if (count > 0) {
System.out.println("信息插入成功!");
} else {
System.out.println("信息插入失败!");
}
}
@Test
public void testDeleteStudentById() {
int count = service.deleteStudentById("20193402");
if (count > 0) {
System.out.println("信息删除成功!");
} else {
System.out.println("信息删除失败!");
}
}
@Test
public void testDeleteStudentsByClass() {
int count = service.deleteStudentsByClass("2019经济1班");
if (count > 0) {
System.out.println("信息删除成功!");
} else {
System.out.println("信息删除失败!");
}
}
@Test
public void testDeleteStudentsByDepartment() {
int count = service.deleteStudentsByDepartment("商学院");
if (count > 0) {
System.out.println("信息删除成功!");
} else {
System.out.println("信息删除失败!");
}
}
@Test
public void testUpdateStudent() {
Student student = service.findStudentById("20192101");
student.setName("加强版高大强");
int count = service.updateStudent(student);
if (count > 0) {
System.out.println("数据更新成功!");
} else {
System.out.println("数据更新失败!");
}
}
@Test
public void testFindStudentById() {
Student student = service.findStudentById("20192101");
if (student == null) {
System.out.println("未查询到该数据!");
} else {
System.out.println(student);
}
}
@Test
public void testFindStudentsByName() {
List<Student> students = service.findStudentsByName("加强版高大强");
Iterator iterator = students.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
@Test
public void testFindStudentsByClass() {
List<Student> students = service.findStudentsByClass("2019经济1班");
Iterator<Student> studentIterator = students.iterator();
while (studentIterator.hasNext()) {
System.out.println(studentIterator.next());
}
}
@Test
public void testFindStudentsByDepartment() {
List<Student> students = service.findStudentsByDepartment("商学院");
Iterator<Student> studentIterator = students.iterator();
while (studentIterator.hasNext()) {
System.out.println(studentIterator.next());
}
}
@Test
public void testFindAllStudents() {
List<Student> students = service.findAllStudents();
Iterator<Student> studentIterator = students.iterator();
while (studentIterator.hasNext()) {
System.out.println(studentIterator.next());
}
}
@Test
public void testFindRowsBySex() {
Vector vector = service.findRowsBySex();
for (Object o : vector) {
System.out.println(o);
}
}
@Test
public void testFindRowsByClass() {
Vector vector = service.findRowsByClass();
for (Object o : vector) {
System.out.println(o);
}
}
@Test
public void testFindRowsByDepartment() {
Vector vector = service.findRowsByDepartment();
for (Object o : vector) {
System.out.println(o);
}
}
}
a:运行testAddStudent方法
查看数据表:
b:运行testStudentById方法
查看数据表:
c:运行testDeleteStudentsByClass方法
查看数据表:
d:运行testDeleteStudentsByDepartment方法
查看数据表:
e:运行testUpdateStudent方法
查看数据表:
f:运行testFindStudentById方法
g:运行testFindStudentsByName方法
h:运行testFindStudentsByClass方法
i:运行testFindStudentsByDepartment方法
j:运行testFindAllStudents方法
k:运行testFindRowsBySex方法
l:运行testFindRowsByClass方法
m:运行testFindRowsByDepartment方法
4、创建用户服务接口实现类UserServiceImpl并进行测试
(1)、创建UserServiceImpl实现类
package net.dc.student.service.impl;
import net.dc.student.bean.User;
import net.dc.student.dao.UserDao;
import net.dc.student.dao.impl.UserDaoImpl;
import net.dc.student.service.UserService;
import java.util.List;
/**
* 功能:用户数据访问接口实现类
* 作者:邓聪
* 日期:2020年07月10日
*/
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);
}
}
(2)、创建UserServiceImpl测试类TestUserServiceImpl
package net.dc.student.test;
import net.dc.student.bean.User;
import net.dc.student.service.UserService;
import net.dc.student.service.impl.UserServiceImpl;
import org.junit.Test;
import java.util.List;
/**
* 功能:测试用户数据访问接口实现类
* 作者;邓聪
* 日期:2020年07月10日
*/
public class TestUserServiceImpl {
// 创建用户服务接口对象
UserService service = new UserServiceImpl();
@Test
public void testAddUser() {
User user = service.findUserById(1);
user.setUsername("七彩长毛怪");
int count = service.addUser(user);
if (count > 0) {
System.out.println("信息插入成功!");
} else {
System.out.println("信息插入失败!");
}
}
@Test
public void testDeleteUserById() {
int count = service.deleteUserById(9);
if (count > 0) {
System.out.println("信息删除成功!");
} else {
System.out.println("信息删除失败!");
}
}
@Test
public void testUpdateUser() {
User user = service.findUserById(8);
user.setUsername("七彩长毛怪");
int count = service.updateUser(user);
if (count > 0) {
System.out.println("信息更新成功!");
} else {
System.out.println("信息更新失败!");
}
}
@Test
public void testFindAllUsers() {
List<User> userList = service.findAllUsers();
for (User user : userList) {
System.out.println(user);
}
}
@Test
public void testFindUserById() {
User user = service.findUserById(1);
if (user != null) {
System.out.println(user);
} else {
System.out.println("未查询到该用户!");
}
}
@Test
public void testLogin() {
String username = "七彩长毛怪";
String password = "66666";
User user = service.login(username, password);
if (user != null) {
System.out.println("登录成功!");
} else {
System.out.println("登录失败!");
}
}
@Test
public void testIsUsernameExisted() {
String username = "七彩长毛怪";
boolean isExisted = service.isUsernameExisted(username);
if (isExisted) {
System.out.println("存在该用户!");
} else {
System.out.println("该用户不存在!");
}
}
}
a:运行testAddUser方法
查看数据表:
b:运行testDeleteUserById方法
查看数据表:
c:运行testUpdateUser方法
查看数据表:
e:运行testFindAllUsers方法
f:运行testFindUserById方法
g:运行testLogin方法
查看数据表:
h:运行testIsUsernameExisted方法
查看数据表: