实训第三天

服务接口

学校服务接口CollegeService

package net.xmh.student.service;

import net.xmh.student.bean.College;

import java.util.Calendar;

/**

  • 包名:net.hw.student.service
  • 类名:CollegeService
  • 描述:学校服务接口
  • 作者:ygf
  • 日期:2019年6月19日
    */
    public interface CollegeService {
    College findCollegeById(int id);
    int updateCollege(College college);
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
用户服务接口UserService

package net.xmh.student.service;

import net.xmh.student.bean.User;

import java.util.List;

/**

  • 包名:net.xmh.student.service
  • 类名:UserService
  • 描述:用户服务接口
  • 作者:ygf
  • 日期:2019年6月19日
    */
    public interface UserService {
    int addUser(User user);
    int deleteUserById(int id);
    int updateUser(User user);
    List findAllUsers();
    User login(String username,String password);

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
学生服务接口StudentService

package net.xmh.student.service;

import net.xmh.student.bean.Student;

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

/**
*

  • 包名:net.hw.student.service
  • 类名:StudentService
  • 描述:学生服务接口
  • 作者:ygf
  • 日期:2019年6月19日
    */
    public interface StudentService {
    int addStudent(Student student);
    int deleteStudentsById(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 findAllAtudents( );
    Vector findRowsBySex();
    Vector findRowsByClass();
    Vector findRowsByDepartment();
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
状态服务接口StatusService

package net.xmh.student.service;

import net.xmh.student.bean.Status;

/**

  • 包名:net.hw.student.service
  • 类名:StatusService
  • 描述:状态服务接口
  • 作者:ygf
  • 日期:2019年6月19日
    */
    public interface StatusService {
    Status findStatusById(int id);
    int updateStatus(Status status);
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
服务接口实现类

学校服务接口实现类CollegeServiceImpl

package net.xmh.student.service.impl;

import net.xmh.student.bean.College;
import net.xmh.student.bean.dao.CollegeDaoImpl;
import net.xmh.student.service.CollegeService;
import net.xmh.student.bean.dao.impl.CollegeDao;

/**

  • 包名:net.hw.student.service.impl

  • 类名:CollegeServiceImpl

  • 描述:学校服务接口实现类

  • 作者:xiangminghan

  • 日期: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);
    }
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
状态服务接口实现类StatusServiceImpl

package net.xmh.student.service.impl;

import net.xmh.student.bean.dao.StatusDaoImpl;
import net.xmh.student.bean.Status;
import net.xmh.student.bean.dao.impl.StatusDao;
import net.xmh.student.bean.dao.StatusDaoImpl;
import net.xmh.student.service.StatusService;

/**

  • 包名:net.xmh.student.service.impl
  • 类名:StatusServiceImpl
  • 描述:状态服务接口实现类
  • 作者:ygf
  • 日期: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);
      }
      }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
学生服务接口实现类StudentServiceImpl

package net.xmh.student.service.impl;

import net.xmh.student.bean.College;
import net.xmh.student.bean.dao.CollegeDaoImpl;
import net.xmh.student.service.CollegeService;
import net.xmh.student.bean.dao.impl.CollegeDao;

/**

  • 包名:net.hw.student.service.impl

  • 类名:CollegeServiceImpl

  • 描述:学校服务接口实现类

  • 作者:xiangminghan

  • 日期: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);
    }
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
用户服务接口实现类UserServiceImpl

package net.xmh.student.service.impl;

import java.util.List;

import net.xmh.student.bean.User;
import net.xmh.student.bean.dao.impl.UserDao;
import net.xmh.student.bean.dao.UserDaoImpl;
import net.xmh.student.service.UserService;

/**

  • 功能:用户服务接口实现类

  • 作者:ygf

  • 日期: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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
在net.hw.student.test包里创建测试类TestCollegeServiceImpl

package net.xmh.student.bean.test;

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

/**

  • 功能:测试学校服务接口实现类
  • 作者:ygf
  • 日期: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("");
    int count = service.updateCollege(college);
    if (count > 0){
    System.out.println(“恭喜,学校记录更新成功”);
    college = service.findCollegeById(1);
    }else {
    System.out.println(“遗憾,学校记录更新失败”);
    }
    }
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
编写测试方法testFindCollegeById(),运行结果为:

编写测试方法testUpdateCollege(),运行结果为:

在net.hw.student.test包里创建测试类TestStatusServiceImpl

package net.xmh.student.bean.test;

import net.xmh.student.service.StatusService;
import net.xmh.student.service.UserService;
import net.xmh.student.service.impl.StatusServiceImpl;
import net.xmh.student.service.impl.UserServiceImpl;
import net.xmh.student.bean.Status;
import net.xmh.student.bean.Student;
import org.junit.Test;

import javax.xml.ws.Service;

/**

  • 功能:学生服务接口实现类
  • 作者:ygf
  • 日期:2019年6月19日
    */
    public class TestStatusServiceImpl {
    @Test
    public void testStatusById() {
    StatusService service = new StatusServiceImpl();
    Status status = service.findStatusById(1);
    System.out.println(status);
    }
    @Test
    public void testUpdateStatus(){
    StatusService service = new StatusServiceImpl();
    Status status = service.findStatusById(1);
    status.setAuthor(“圆圆”);
    status.setTelephone(“15800001111”);
    int count = service.updateStatus(status);
    if (count > 0) {
    System.out.println(“更新成功!”);
    status = service.findStatusById(1);
    System.out.println(status);
    } else {
    System.out.println(“更新失败!”);
    }
    }
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
编写测试方法testStatusById(),运行结果为:

编写测试方法testUpdateStatus(),运行结果为:

在net.hw.student.test包里创建测试类TestStudentServiceImpl

package net.xmh.student.bean.test;

import net.xmh.student.service.StudentService;
import net.xmh.student.service.impl.StudentServiceImpl;
import net.xmh.student.bean.Student;
import java.util.Vector;
import java.util.Iterator;
import org.junit.Test;

import java.util.List;

/**

  • 测试学生服务接口实现类

  • 作者:ygf

  • 日期:2019年6月19日
    */
    public class TestStudentServiceImpl {
    @Test
    public void testAddStudent() {
    Student student = new Student();
    student.setId(“18101001”);
    student.setName(“小可爱”);
    student.setSex(“女”);
    student.setAge(19);
    student.setDepartment(“信息工程系”);
    student.setClazz(“18大数据1班”);
    student.setTelephone(“15890674568”);

     StudentService service = new StudentServiceImpl();
     int count = service.addStudent(student);
     if (count > 0) {
         System.out.println(student);
         System.out.println("恭喜,学生记录插入成功!");
     } else {
         System.out.println("遗憾,学生记录插入失败!");
     }
    

    }

    @Test
    public void testDeleteStudentById() {
    StudentService service = new StudentServiceImpl();
    String id = “18101001”;
    int count = service.deleteStudentsById(id);
    if (count > 0) {
    System.out.println(“恭喜,学生记录删除成功!”);
    } else {
    System.out.println(“遗憾,学生记录删除失败!”);
    }
    }

    @Test
    public void testDeleteStudentClass() {
    StudentService service = new StudentServiceImpl();
    String clazz = “10英教1班”;
    int count = service.deleteStudentsByClass(clazz);
    if (count > 0) {
    System.out.println(“恭喜,[” + clazz + “]学生记录删除成功!”);
    } else {
    System.out.println(“遗憾,[” + clazz + “]学生记录删除失败!”);
    }
    }

    @Test
    public void testDeleteStudentDepartment() {
    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 testUpdateStudent() {
    StudentService service = new StudentServiceImpl();
    Student student = service.findStudentById(“10080301”);

     int count = service.updateStudent(student);
     if (count > 0){
         System.out.println("恭喜,学生记录更新成功!");
     }else{
         System.out.println("遗憾,学生记录更新失败!");
     }
    

    }
    @Test
    public void testFindStudentById() {
    StudentService service = new StudentServiceImpl();
    Student student = service.findStudentById(“10080301”);
    System.out.println(student);
    }

    @Test
    public void testFindStudentsByName(){
    StudentService service = new StudentServiceImpl();
    String name = “李”;
    List students = service.findStudentsByName(name);
    for (Student student:students){
    System.out.println(student);
    }
    }

    @Test
    public void testFindStudentsByClass(){
    StudentService service = new StudentServiceImpl();
    String clazz = “15软件1班”;
    List students = service.findStudentsByClass(clazz);
    for (Student student:students){
    System.out.println(student);
    }
    }
    @Test
    public void testFindStudentsByDepartment(){
    StudentService service = new StudentServiceImpl();
    String department = “信息工程系”;
    List students = service.findStudentsByDepartment(department);
    for (Student student:students){
    System.out.println(student);
    }
    }

    @Test
    public void testFindAllStudents(){
    StudentService service = new StudentServiceImpl();
    String all = “信息工程系”;
    List students = service.findAllStudents();
    for (Student student:students){
    System.out.println(student);
    }
    }

    @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 testFindRowsByClass() {
    StudentService service = new StudentServiceImpl();
    Vector rows = service.findRowsByClass();
    Iterator iterator = rows.iterator();
    while (iterator.hasNext()) {
    System.out.println(iterator.next());
    }
    }

    @Test
    public void testFindRowsByDepartment() {
    StudentService service = new StudentServiceImpl();
    Vector rows = service.findRowsByDepartment();
    Iterator iterator = rows.iterator();
    while (iterator.hasNext()) {
    System.out.println(iterator.next());
    }
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    编写测试方法testAddStudent(),运行结果为:

编写测试方法testDeleteStudentById(),运行结果为:

编写测试方法testDeleteStudentClass(),运行结果为:

编写测试方法testDeleteStudentDepartment(),运行结果为:

运行testUpdateStudent()时,报错:

解决方法:在数据库中再次运行student.sql,刷新表
链接:https://pan.baidu.com/s/1eP8cNZCXdD59q3fbfJWBzQ
提取码:500c
复制这段内容后打开百度网盘手机App,操作更方便哦

编写测试方法testUpdateStudent(),运行结果为:

编写测试方法testFindStudentById(),运行结果为:

编写测试方法testFindStudentsByName(),运行结果为:

编写测试方法testFindStudentsByClass(),运行结果为:

编写测试方法testFindStudentsByDepartment(),运行结果为:

编写测试方法testFindAllStudents(),运行结果为:

编写测试方法testFindRowsBySex(),运行结果为:

编写测试方法testFindRowsByClass(),运行结果为:

编写测试方法testFindRowsByDepartment(),运行结果为:

在net.hw.student.test包里创建测试类TestUserServiceImpl

package net.xmh.student.bean.test;

import net.xmh.student.service.UserService;
import net.xmh.student.bean.User;
import net.xmh.student.service.impl.UserServiceImpl;
import java.sql.Timestamp;
import java.util.Date;
import org.junit.Test;
import java.util.List;

/**

  • 功能:测试用户服务接口实现类

  • 作者:ygf

  • 日期: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(“遗憾,用户名或密码错误,登录失败!”);
    }
    }

    @Test
    public void testAddUser() {
    User user = new User();
    user.setId(10);
    user.setUsernname(“琳琳”);
    user.setRegisterTime(new Timestamp(new Date().getTime()));
    user.setPassword(“222”);

     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();
    String id = “2”;
    int count = service.deleteUserById(1);
    if (count > 0) {
    System.out.println(“恭喜,学生记录删除成功!”);
    } else {
    System.out.println(“遗憾,学生记录删除失败!”);
    }
    }

    @Test
    public void testUpdateUser() {
    UserService service = new UserServiceImpl();
    User user = service.findUserById(1);
    user.setUsernname(“圆圆”);
    int count = service.updateUser(user);
    if (count > 0) {
    System.out.println(“更新成功!”);
    } else {
    System.out.println(“更新失败!”);
    }
    }

    @Test
    public void testFindUserById() {
    UserService service = new UserServiceImpl();
    User user = service.findUserById(1);
    System.out.println(service);
    }

    @Test
    public void testFindAllUsers() {
    UserService service = new UserServiceImpl();
    List users = service.findAllUsers();
    for (User user : users) {
    System.out.println(users);
    }
    }
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
编写测试方法testLogin(),运行结果为:

编写测试方法testAddUser(),运行结果为:

编写测试方法testDeleteUserById(),运行结果为:

编写测试方法testUpdateUser(),运行结果为:

编写测试方法testFindUserById(),运行结果为:

编写测试方法testFindAllUsers(),运行结果为:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值