7月10日Java实训第四天

Java实训第四天

1.创建数据服务接口

在这里插入图片描述
其中
CollegeService

package net.wlm.student.service;

import net.wlm.student.bean.College;


public interface CollegeService {
    College findCollegeById(int id);
    int updateCollege(College college);
}

StatusService

package net.wlm.student.service;

import net.wlm.student.bean.Status;

public interface StatusService {
    Status findStatusById(int id);
    int updateStatus(Status status);
}

StudentService

package net.wlm.student.service;

import net.wlm.student.bean.Student;

import java.util.List;
import java.util.Vector;

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();
}

UserService

package net.wlm.student.service;

import net.wlm.student.bean.User;

import java.util.List;

public interface UserService {
    int addUser(User user);
    int deleteUserById(String id);
    int updateUser(User user);
    User findUserById(int id);
    List<User> findAllUsers();
    User login(String username, String password);
    boolean isUsernameExisted(String username);
}

2.创建服务接口实现类CollegeServiceImpl

在service包里创建impl包
在impl包中进行

package net.wlm.student.service.impl;

import net.wlm.student.bean.College;
import net.wlm.student.dao.CollegeDao;
import net.wlm.student.dao.impl.CollegeDaoimpl;
import net.wlm.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);
    }
}

创建测试类 TestCollegeServiceImpl

编写测试方法testFindCollegeById()
编写测试方法testUpdateCollege()

package net.wlm.student.test;


import net.wlm.student.bean.College;
import net.wlm.student.service.CollegeService;
import net.wlm.student.service.impl.CollegeServiceImpl;
import org.junit.Test;

public class TestCollegeServiceImpl {
    CollegeService service = new CollegeServiceImpl();
    @Test
    public void textFindCollegeById() {
        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 = service.findCollegeById(1);
        college.setPresident("王洪礼");
        college.setTelephone("0830-3150380");
        int count =service.updateCollege(college);
        if (count>0){
            System.out.println("恭喜,学校记录更新成功!");
            college = service.findCollegeById(1);
            System.out.println(college);
        }else {
            System.out.println("遗憾,学校记录更新失败!");
        }
    }
}

3.创建服务接口实现类StatusServiceImpl

package net.wlm.student.service.impl;

import net.wlm.student.bean.Status;
import net.wlm.student.dao.StatusDao;
import net.wlm.student.dao.impl.StatusDaoImpl;
import net.wlm.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);
    }
}

创建测试类 TestStatusServiceImpl

编写测试方法testFindStatusById()
编写测试方法testUpdateStatus()

package net.wlm.student.test;

import net.wlm.student.bean.Status;
import net.wlm.student.service.StatusService;
import net.wlm.student.service.impl.StatusServiceImpl;
import org.junit.Test;
public class TestStatusServiceImpl{
    StatusService service = new StatusServiceImpl();
    @Test
    public void testFindById() {
        Status status = service.findStatusById(1);
        System.out.println("作者:" + status.getAuthor());
        System.out.println("学校:" + status.getCollege());
        System.out.println("版本:" + status.getVersion());
        System.out.println("地址:" + status.getAddress());
        System.out.println("电话:" + status.getTelephone());
        System.out.println("邮箱:" + status.getEmail());
    }
    @Test
    public void testUpdateStatus(){
        Status status = service.findStatusById(1);
        status.setAuthor("王良墁");
        status.setTelephone("18308318874");
        int count =service.updateStatus(status);
        if (count >0 ){
            System.out.println("恭喜,状态记录更新成功!");
            status = service.findStatusById(1);
            System.out.println(status);

        }else {
            System.out.println("遗憾,状态记录更新失败");
        }
    }
}

4.创建服务接口实现类StudentServiceImpl

package net.wlm.student.service.impl;

import net.wlm.student.bean.Student;
import net.wlm.student.dao.StudentDao;
import net.wlm.student.dao.impl.StudentDaoImpl;
import net.wlm.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 List<Student> 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<Student> findStudentsByClass(String clazz) {
        return studentDao.findByClass(clazz);
    }

    @Override
    public List<Student> findStudentsByDepartment(String department) {
        return studentDao.findByDepartment(department);
    }

    @Override
    public List<Student> findStudentsByName(String name) {
        return studentDao.findByName(name);
    }

    @Override
    public int updateStudent(Student student) {
        return studentDao.update(student);
    }
}

创建测试类 TestStudentServiceImpl

编写测试方法testFindStudentsByName()
编写测试方法testAddStudent()
编写测试方法testDeleteStudentById()
编写测试方法testDeleteStudentsByClass()
编写测试方法testDeleteStudentsByDepartment()
编写测试方法testUpdateStudent()
编写测试方法testFindStudentById()
编写测试方法testFindAllStudents()
编写测试方法testFindStudentsByClass()
编写测试方法testFindStudentsByDepartment()
编写测试方法testFindRowsBySex()
编写测试方法testFindRowsByClass()
编写测试方法testFindRowsByDepartment()

package net.wlm.student.test;

import net.wlm.student.bean.Student;
import net.wlm.student.service.StudentService;
import net.wlm.student.service.impl.StudentServiceImpl;
import org.junit.Test;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class TestStudentServiceImpl {
    StudentService service =new StudentServiceImpl();
    @Test
    public void testFindStudentsByName(){
        String name = "李";
        List<Student> students =service.findStudentsByName(name);
        for (Student student:students);
            System.out.println(students);
    }
    @Test
    public  void textAddStudent(){
        Student student = new Student();
        student.setId("19206150");
        student.setName("张嘉倪");
        student.setSex("女");
        student.setAge(19);
        student.setDepartment("艺术传媒学院");
        student.setClazz("2019数媒三班");
        student.setTelephone("19856472332");
        int count = service.addStudent(student);
        if (count >0){
            System.out.println("恭喜,学生记录增加成功!");
            System.out.println(service.findStudentById(student.getId()));
        }else{
            System.out.println("遗憾,学生记录增加失败!");
        }

    }
    @Test
    public void testDeleteStudentById(){
        String id = "19201005";
        int count = service.deleteStudentById(id);
        if (count >0 ){
            System.out.println("恭喜,删除学生记录成功!");
        }else{
            System.out.println("遗憾,删除学生记录失败!");
        }
    }
    @Test
    public void testDeleteStudentsByClass(){
        String classs = "19级语教1班";
        int count = service.deleteStudentsByClass(classs);
        if (count>0){
            System.out.println("恭喜,["+classs+"]学生记录删除成功");
        }else{
            System.out.println("遗憾,["+classs+"]学生记录删除失败");
        }
    }
    @Test
    public void testDeleteStudentsByDepartment(){
        String dep = "艺术传媒学院";
        int count = service.deleteStudentById(dep);
        if (count>0){
            System.out.println("恭喜,["+dep+"]的学生记录删除成功");
        }else{
            System.out.println("遗憾,["+dep+"]的学生记录删除失败");
        }
    }
    @Test
    public void testUpdateStudent() {
        Student student = service.findStudentById("18205088");
        student.setId("18205088");
        student.setName("张嘉倪");
        student.setSex("女");
        student.setAge(19);
        student.setDepartment("艺术传媒学院");
        student.setClazz("2019数媒三班");
        student.setTelephone("19856472332");
        int count = service.updateStudent(student);
        if ( count >0){
            System.out.println("状态记录更新成功");
            System.out.println(service.findStudentById("18205088"));
        }else {
            System.out.println("状态记录更新失败");
        }
    }
    @Test
    public void textStudentFindById(){
        String id = "18205088";
        List<Student> students = Collections.singletonList(service.findStudentById(id));
        if(students.size()>0){
            for (Student student :students){
                System.out.println(student);
            }
        }else{
            System.out.println("温馨提示:查无此人");
        }

    }
    @Test
    public void textFindAllStudent(){
        List<Student> students = service.findAllStudents();
        for (Student student : students){
            System.out.println(student);
        }
    }
    @Test
    public void testStudentFindByClass(){
        String classs = "2019数媒三班";
        List<Student> students = service.findStudentsByClass(classs);
        if(students.size()>0){
            for (Student student :students){
                System.out.println(student);
            }
        }else{
            System.out.println("温馨提示:查无此人");
        }
    }
    @Test
    public void testFindStudentsByDepartment(){
        String department = "艺术传媒学院";
        List<Student> students = service.findStudentsByDepartment(department);
        if(students.size()>0){
            for (Student student :students){
                System.out.println(student);
            }
        }else{
            System.out.println("温馨提示:查无此人");
        }
    }
    @Test
    public void textFindRowsBySex(){
        Vector rows = service.findRowsBySex();
        Iterator iterator = rows.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
    @Test
    public void testFindRowsByClass(){
        Vector rows = service.findRowsByClass();
        Iterator iterator = rows.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
    @Test
    public void testFindRowsByDepartment(){
        Vector rows = service.findRowsByDepartment();
        Iterator iterator = rows.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}

5.创建服务接口实现类UserServiceImpl

package net.wlm.student.service.impl;

import net.wlm.student.bean.User;
import net.wlm.student.dao.UserDao;
import net.wlm.student.dao.impl.UserDaoImpl;
import net.wlm.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(String 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);
    }
}

创建测试类 TestUserServiceImpl

编写测试方法testLogin()
编写测试方法testIsUsernameExisted()
编写测试方法testAddUser()
编写测试方法testDeleteUserById()
编写测试方法testUpdateUser()
编写测试方法testFindUserById()
编写测试方法testFindAllUsers()

package net.wlm.student.test;

import net.wlm.student.bean.User;
import net.wlm.student.service.UserService;
import net.wlm.student.service.impl.UserServiceImpl;
import org.junit.Test;

import java.util.Date;
import java.util.List;

public class TestUserServiceImpl {
    UserService service = new UserServiceImpl();
    @Test
    public void textLongin() {
        String username, password;
        username = "王月";
        password = "222222";
        User user = service.login(username, password);
        if (user != null) {
            System.out.println("恭喜,用户名与密码正确,登录成功");

        } else {
            System.out.println("遗憾,用户名与密码错误,登录失败");
        }
    }
    @Test
    public void testIsUsernameExisted() {
        String username = "王月";
        boolean result = service.isUsernameExisted(username);
        if (result) {
            System.out.println("温馨提示:[" + username + "]已存在,不可用此名注册");

        } else {
            System.out.println("温馨提示:[" + username + "]不存在,可用此名注册");
        }
    }
    @Test
    public void testAddUser() {
        User user = new User();
        user.setUsername("张嘉倪");
        user.setPassword("111111");
        user.setTelephone("11223456888");
        user.setRegisterTime(new Date());
        int count = service.addUser(user);
        if (count > 0) {
            System.out.println("用户记录插入成功");
            System.out.println(service.findUserById(service.findAllUsers().size()));

        } else {
            System.out.println("用户插入记录失败");

        }
    }
    @Test
    public void testDeleteUserById() {
        String id = "199";
        // 调用学生数据访问对象的按id删除方法
        int count =service.deleteUserById(id);
        // 判断学生记录是否删除成功
        if (count > 0) {
            System.out.println("恭喜,学生记录删除成功!");
        } else {
            System.out.println("遗憾,学生记录删除失败!");
        }
    }
    @Test
    public void testUpdateUser() {
        User user = service.findUserById(10);
        user.setId(10);
        user.setUsername("张嘉倪");
        user.setPassword("111111");
        user.setTelephone("12349785155");
        user.setRegisterTime(new Date());
        int count = service.updateUser(user);
        if (count > 0) {
            System.out.println("状态记录更新成功");
            System.out.println(service.findUserById(10));
        } else {
            System.out.println("状态记录更新失败");
        }
    }
    @Test
    public void testFindUserById() {
        User user = service.findUserById(2);
        System.out.println("用户名:" + user.getUsername());
        System.out.println("密码:" + user.getPassword());
        System.out.println("电话:" + user.getTelephone());
        System.out.println("注册时间:" + user.getRegisterTime());
    }

    @Test
    public void testFindAllUsers() {
        // 调用学生数据访问对象的查找全部方法
        List<User> users = service.findAllUsers();
        // 通过增强for循环遍历学生列表
        for (User user : users) {
            System.out.println(user);
        }
    }

}

总结

错误总结

出现与访问接口同样的错误,经过修改String将问题解决

学习成果

通过老师的代码学习,我编写了剩余的代码,更加明了如何编写,增加了对实现类的认识

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值