上次做到了测试StudentDaoImpl数据访问接口实现类,虽然遇到了很多问题,但是在老师的帮助下,还是解决了,但其实感觉自己并没有学会自己写代码。能力不够,还有不够细心,有一些特殊的地方,只要打错一个代码就有可能导致整篇代码无法运行,所以自己还是应该细心一点,并且认真一点。今天我从测试UserDaoImpl数据访问接口实现类测试开始写。
4、UserDaoImpl数据访问接口实现类测试
package net.lsl.student.test;
import net.lsl.student.bean.User;
import net.lsl.student.dao.UserDao;
import net.lsl.student.dao.impl.UserDaoImpl;
import org.junit.Test;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
/**
功能:测试用户数据访问接口实现类
作者:李世兰
日期:2019年6月19日14:48:01
*/
public class TestUserDaoImpl {
@Test
public void testInsert(){
User user=new User();
user.setId(7);
user.setUsername(“张正敏”);
user.setPassword(“666666”);
user.setTelephone(“18780636650”);
user.setRegisterTime(new Timestamp(new Date().getTime()));
UserDao dao=new UserDaoImpl();
int count=dao.insert(user);
if(count>0){
System.out.println("恭喜,添加用户成功");
}else {
System.out.println("遗憾,添加用户失败");
}
}
@Test
public void testDeleteById(){
UserDao dao=new UserDaoImpl();
int id=1;
int count=dao.deleteById(id);
if (count>0){
System.out.println(“恭喜,学生删除成功”);
}else {
System.out.println(“遗憾,学生删除失败”);
}
}
@Test
public void testUpdate() {
UserDao dao = new UserDaoImpl();
User user = dao.findById(2);
user.setUsername(“袁雨晴”);
dao.update(user);
user = dao.findById(2);
System.out.println(user);
}
@Test
public void testFindById(){
UserDao dao = new UserDaoImpl();
User user = dao.findById(1);
System.out.println(user);
}
@Test
public void testFindAll(){
UserDao dao=new UserDaoImpl();
String username="*";
List users=dao.findAll();
for (User user:users){
System.out.println(user);
}
}
@Test
public void testLogin(){
UserDao dao=new UserDaoImpl();
String username,password;
username=“admin”;
password=“12345”;
User user=dao.login(username,password);
if(user!=null){
System.out.println(“恭喜,用户名与密码正确”);
}else {
System.out.println(“遗憾,用户名与密码错误”);
}
}
}
八、服务接口
1、学校服务接口CollegeService
package net.lsl.student.service;
import net.lsl.student.bean.College;
/**
2、状态服务接口StatusService
package net.lsl.student.service;
import net.lsl.student.bean.Status;
/**
3、学生服务接口StudentService
package net.lsl.student.service;
import net.lsl.student.bean.Student;
import java.util.List;
import java.util.Vector;
/**
4、用户服务接口UserService
package net.lsl.student.service;
import net.lsl.student.bean.User;
import java.util.List;
/**
九、服务接口实现类、
1、学校服务接口实现类CollegeServiceImpl
package net.lsl.student.service.impl;
import net.lsl.student.bean.College;
import net.lsl.student.dao.CollegeDao;
import net.lsl.student.dao.impl.CollegeDaoImpl;
import net.lsl.student.service.CollegeService;
/**
功能:学校服务接口实现类
作者:李世兰
日期:2019年6月19日
*/
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、状态服务接口实现类StatusServiceImpl
package net.lsl.student.service.impl;
import net.lsl.student.bean.Status;
import net.lsl.student.dao.StatusDao;
import net.lsl.student.dao.impl.StatusDaoImpl;
import net.lsl.student.service.StatusService;
/**
功能:状态服务接口实现类
作者:李世兰
日期:2019年6月19日
*/
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);
}
}
3、学生服务接口实现类StudentServiceImpl
package net.lsl.student.service.impl;
import net.lsl.student.bean.Student;
import net.lsl.student.dao.StudentDao;
import net.lsl.student.dao.impl.StudentDaoImpl;
import net.lsl.student.service.StudentService;
import java.util.List;
import java.util.Vector;
/**
功能:学生服务接口实现类
作者:李世兰
日期:2019年6月19日
*/
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.finfByClass(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);
}
}
4、用户服务接口实现类UserServiceImpl
package net.lsl.student.service.impl;
import net.lsl.student.bean.User;
import net.lsl.student.dao.UserDao;
import net.lsl.student.dao.impl.UserDaoImpl;
import net.lsl.student.service.UserService;
import java.util.List;
/**
功能:用户服务接口实现类
作者:李世兰
日期:2019年6月19日
*/
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);
}
}
其实服务接口和实现类都是很简单的,代码比较少,看起来也不难,但是还是需要费时去理解,别人自己也不知道为什么要这样写。接下来要写测试了,我觉得对我来说最难的就是测试了。
十、服务接口实现类测试
1、学校服务接口实现类TestCollegeServiceImpl测试
package net.lsl.student.test;
import net.lsl.student.bean.College;
import net.lsl.student.service.CollegeService;
import net.lsl.student.service.impl.CollegeServiceImpl;
import org.junit.Test;
/**
2、状态服务接口实现类TestStatusServiceImpl测试
package net.lsl.student.test;
import net.lsl.student.bean.Status;
import net.lsl.student.service.StatusService;
import net.lsl.student.service.impl.StatusServiceImpl;
import org.junit.Test;
/**
3、学生服务接口实现类TestStudentServiceImpl测试
package net.lsl.student.test;
import net.lsl.student.bean.Student;
import net.lsl.student.dao.StudentDao;
import net.lsl.student.dao.impl.StudentDaoImpl;
import net.lsl.student.service.StudentService;
import net.lsl.student.service.impl.StudentServiceImpl;
import org.junit.Test;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
/**
功能:测试状态服务接口实现类
作者:李世兰
日期:2019年6月19日
*/
public class TestStudentServiceImpl {
@Test
public void testAddStudent(){
Student student=new Student();
student.setId(“18242026”);
student.setName(“张敏”);
student.setSex(“女”);
student.setAge(19);
student.setDepartment(“信息工程学院”);
student.setClazz(“18大数据1班”);
student.setTelephone(“18780636650”);
StudentService service=new StudentServiceImpl();
int count=service.addStudent(student);
if (count>0){
System.out.println( "恭喜,添加学生成功");
}else {
System.out.println("遗憾,添加学生失败");
}
}
@Test
public void testDeleteStudentById(){
StudentService service=new StudentServiceImpl();
String id=“18242026”;
int count =service.deleteStudentById(id);
if(count>0){
System.out.println(“恭喜,学生记录删除成功!”);
}else{
System.out.println(“遗憾,学生记录删除失败!”);
}
}
@Test
public void testDeleteStudentByClass(){
StudentService service = new StudentServiceImpl();
String clazz =“11建工1班”;
int count = service.deleteStudentsByClass(clazz);
if (count > 0){
System.out.println(“恭喜,[” + clazz + “]学生记录删除成功!”);
}else {
System.out.println(“遗憾,[” + clazz + “]学生记录删除失败!”);
}
}
@Test
public void testDeleteByDepartment(){
StudentService service = new StudentServiceImpl();
String department =“外语系”;
int count = service.deleteStudentsByDepartment(department);
if (count > 0){
System.out.println(“恭喜,[” + department + “]学生记录删除成功!”);
}else {
System.out.println(“遗憾,[” + department + “]学生记录删除失败!”);
}
}
@Test
public void testFindAllStudent(){
StudentService service=new StudentServiceImpl();
String name="*";
List students=service.findAllStudents();
for (Student student:students){
System.out.println(student);
}
}
@Test
public void testFindRowsByClass(){
StudentService service=new StudentServiceImpl();
String clazz=“11软件2班”;
Vector rows=service.findRowsByClass();
List students=service.findRowsByClass();
Iterator iterator=rows.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
@Test
public void testFindRowsByDepartment(){
StudentService service=new StudentServiceImpl();
String department=“信息工程系”;
Vector rows=service.findRowsByDepartment();
Iterator iterator=rows.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
@Test
public void testFindRowsBySex(){
StudentService service=new StudentServiceImpl();
Vector rows=service.findRowsBySex();
Iterator iterator=rows.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
@Test
public void testFindStudentById(){
StudentService service=new StudentServiceImpl();
String id=“11090701”;
Student student=service.findStudentById(id);
System.out.println(student);
}
@Test
public void testFindStudentByClass(){
StudentService service=new StudentServiceImpl();
String clazz=“11软件2班”;
List students=service.findStudentsByClass(clazz);
for(Student student:students){
System.out.println(student);
}
}
@Test
public void testFindStudentByDepartment(){
StudentService service=new StudentServiceImpl();
String department=“信息工程系”;
List students=service.findStudentsByDepartment(department);
for (Student student:students){
System.out.println(student);
}
}
@Test
public void testFindStudentByName(){
StudentService service = new StudentServiceImpl();
String name = “李”;
List students = service.findStudentsByName(name);
for (Student student : students) {
System.out.println(student);
}
}
@Test
public void testUpdateStudent() {
StudentService service = new StudentServiceImpl();
Student student = service.findStudentById(“15012456”);
student.setName(“张正敏”);
student.setTelephone(“18780636650”);
int count = service.updateStudent(student);
if (count > 0) {
System.out.println(“恭喜,状态更新记录成功”);
student = service.findStudentById(“15012456”);
System.out.println(student);
} else {
System.out.println(“遗憾,状态更新记录失败”);
}
}
}
4、用户服务接口实现类TestUserServiceImpl测试
package net.lsl.student.test;
import net.lsl.student.bean.User;
import net.lsl.student.dao.UserDao;
import net.lsl.student.dao.impl.UserDaoImpl;
import net.lsl.student.service.UserService;
import net.lsl.student.service.impl.UserServiceImpl;
import org.junit.Test;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
/**
功能:测试用户服务接口实现类
作者:李世兰
日期:2019年6月19日
*/
public class TestUserServiceImpl {
@Test
public void testAddUser(){
User user=new User();
user.setId(10);
user.setUsername(“李四”);
user.setPassword(“888888”);
user.setTelephone(“18780632345”);
user.setRegisterTime(new Timestamp(new Date().getTime()));
UserService service=new UserServiceImpl();
int count=service.addUser(user);
if(count>0){
System.out.println(“恭喜,添加用户成功”);
}else {
System.out.println(“遗憾,添加用户失败”);
}
}
@Test
public void testDeleteUserById(){
UserService service=new UserServiceImpl();
int id=2;
int count=service.deleteUserById(id);
if (count>0){
System.out.println(“恭喜,学生删除成功”);
}else {
System.out.println(“遗憾,学生删除失败”);
}
}
@Test
public void testFindUserById(){
UserService service = new UserServiceImpl();
User user = service.findUserById(3);
System.out.println(user);
}
@Test
public void testFindAllUsers(){
UserService service=new UserServiceImpl();
String username="*";
List users=service.findAllUsers();
for (User user:users){
System.out.println(user);
}
}
@Test
public void testLogin(){
UserService service = new UserServiceImpl();
String username,password;
username = “howard”;
password = “903213”;
User user = service.login(username,password);
if (user != null) {
System.out.println(“恭喜,用户名与密码正确,登录成功!”);
}else{
System.out.println(“遗憾,用户名或密码错误,登录失败!”);
}
}
}
实训第三天写的代码,其实和第二天差不多,就是有一点小小的区别而已。但是在写测试类的时候我还是遇到了问题。老师给出了一两个测试类的写法,然后让我们自己把接下来的剩下 的全部写完,但是我除了复制和仿照之前的写法来写剩下的之外,我是不能自己写其他的方法。之中,写update测试就遇到了很多问题,永林老师给出的那些方法不行,然后自己尝试着写了很多方法,也都是错误的,这让自己变得非常焦躁,但是实训还不是要继续,剩下的问题留着第二天去请教会的人。其实自己算是不认真的那一类了吧,要不然知道自己底子薄,能力低就应该更加努力,但是就是没有能力学习的精力,可能自己真的不适合这一个专业,但是已经无法改变了,能做到就是让自己喜欢上这个东西,并且努力学习。