创建服务接口
服务接口调用数据访问接口,可以添加必要的业务逻辑,而服务接口被控制层调用,获取数据传递给视图层显示。
1、创建学校服务接口
package net.cqf.student.service;
import net.cqf.student.bean.College;
/**
* 功能:学校服务接口
* 作者:陈秋帆
* 日期:2020年07月08日
*/
public interface CollegeService {
College findCollegeById(int id);
int updateCollege(College college);
}
2、创建状态服务接口
package net.cqf.student.service;
import net.cqf.student.bean.Status;
/**
* 功能:状态服务接口
* 作者:陈秋帆
* 日期:2020年07月08日
*/
public interface StatusService {
Status findStatusById(int id);
int updateStatus(Status status);
}
3、创建学生服务接口
package net.cqf.student.service;
import net.cqf.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.cqf.student.service;
import net.cqf.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、创建学校服务接口实现类
package net.cqf.student.service.impl;
import net.cqf.student.bean.College;
import net.cqf.student.dao.CollegeDao;
import net.cqf.student.dao.impl.CollegeDaoImpl;
import net.cqf.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);
}
}
单元测试:对CollegeServiceImpl进行单元测试
package net.cqf.student.test;
import net.cqf.student.bean.College;
import net.cqf.student.service.CollegeService;
import net.cqf.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());
System.out.println("简介" + college.getProfile());
}
@Test
public void testUpdateCollege(){
CollegeService service = new CollegeServiceImpl();
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("遗憾,学校记录更新失败!");
}
}
}
2、创建状态服务接口实现类
创建状态服务接口实现类StatusServiceImpl
package net.cqf.student.service.impl;
import net.cqf.student.dao.StatusDao;
import net.cqf.student.dao.impl.StatusDaoImpl;
import net.cqf.student.bean.Status;
import net.cqf.student.service.StatusService;
/**
* 功能:状态服务接口实现类
* 作者:陈秋帆
* 日期:2020年07月08日
*/
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);
}
}
单元测试:对StatusServiceImpl进行单元测试
package net.cqf.student.test;
import net.cqf.student.bean.Status;
import net.cqf.student.service.StatusService;
import net.cqf.student.service.impl.StatusServiceImpl;
import org.junit.Test;
/**
* 功能:测试状态服务接口实现类
* 作者:陈秋帆
* 日期:2020年07月08日
*/
public class TestStatusServiceImpl {
// 创建状态服务接口对象
StatusService service = new StatusServiceImpl();
@Test
public void testFindStatusById(){
// 调用状态服务接口对象的查询方法,获取状态对象
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("18968790586");
// 调用状态服务接口对象的更新方法,更新状态对象
int count = service.updateStatus(status);
if (count>0) {
System.out.println("恭喜,状态记录更新成功!");
status = service.findStatusById(1);
System.out.println(status);
}else {
System.out.println("遗憾,状态记录更新失败!");
}
}
}
3、创建学生服务接口实现类
创建学生服务接口实现类StudentServiceImpl
package net.cqf.student.service.impl;
import net.cqf.student.dao.StudentDao;
import net.cqf.student.bean.Student;
import net.cqf.student.dao.impl.StudentDaoImpl;
import net.cqf.student.service.StudentService;
import java.util.List;
import java.util.Vector;
/**
* 功能:学生服务接口实现类
* 作者:陈秋帆
* 日期:2020年07月08日
*/
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);
}
}
单元测试:对StudentServiceImpl进行单元测试
package net.cqf.student.test;
import net.cqf.student.dao.StudentDao;
import net.cqf.student.bean.Student;
import net.cqf.student.dao.impl.StudentDaoImpl;
import net.cqf.student.service.StudentService;
import net.cqf.student.service.impl.StudentServiceImpl;
import org.junit.Test;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
/**
* 功能:测试学生服务接口实现类
* 作者:陈秋帆
* 日期:2020年07月08日
*/
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(student);
}
}
StudentDao dao = new StudentDaoImpl();
@Test
public void testInsert(){
Student student = new Student();
student.setId("19242096");
student.setName("张晓慧");
student.setSex("女");
student.setAge(19);
student.setDepartment("艺术传媒学院");
student.setClazz("2019数媒3班");
student.setTelephone("12342344564");
//调用学生数据访问对象的插入方法
int count = dao.insert(student);
//判断学生记录是否插入成功
if (count > 0){
System.out.println("恭喜,学生记录插入成功");
System.out.println(dao.findById(student.getId()));
}else{
System.out.println("遗憾,学生记录插入失败!");
}
}
@Test
public void testDeleteById(){
String id = "20190107";
//调用学生数据访问对象的按ID删除方法
int count = dao.deleteById(id);
//判断学生记录是否删除成功
if (count > 0){
System.out.println("恭喜,学生记录删除成功");
}else{
System.out.println("遗憾,学生记录删除失败");
}
}
@Test
public void testDeleteByClass(){
String clazz = "19应用英语1班";
//调用学生数据访问对象的按班级删除方法
int count = dao.deleteByClass(clazz);
if (count > 0){
System.out.println("恭喜,["+clazz+"]学生记录删除成功");
}else{
System.out.println("遗憾,["+clazz+"]学生记录删除失败");
}
}
@Test
public void testFindByName(){
String name = "李";
List<Student> students = dao.findByName(name);
//判断列表里是否有元素
if (students.size() > 0){
for (Student student:students){
System.out.println(student);
}
}else{
System.out.println("温馨提示:查无此人");
}
}
@Test
public void testFindAll(){
//调用学生数据访问对象的查找全部方法
List<Student> students = dao.findAll();
for(Student student:students){
System.out.println(student);
}
}
@Test
public void testFindRowsBySex(){
//调用学生数据访问对象的按性别统计人数方法
Vector rows = dao.findRowsBySex();
//获取向量的跌代器
Iterator iterator = rows.iterator();
//遍历迭代器
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
@Test
public void testDeleteByDepartment(){
String department = "国际学院";
int count = dao.deleteByDepartment(department);
if (count > 0){
System.err.println("恭喜,删除记录成功!");
}else{
System.err.println("遗憾,删除记录失败!");
}
}
@Test
public void testUpdate(){
Student student = new Student();
student.setId("20190142");
student.setName("郭德纲");
student.setSex("男");
student.setAge(20);
student.setDepartment("人文学院");//由机械工程学院修改为人文学院
student.setClazz("19小学教育1班");//由19机电一体化1班修改为小学教育1班
student.setTelephone("12346578756");
int count = dao.update(student);
if (count > 0){
System.out.println("恭喜,更新学生记录成功");
System.out.println(student);
}else{
System.out.println("遗憾,更新数据失败");
}
}
@Test
public void FindById(){
String id = "20190138";
List<Student> students = Collections.singletonList(dao.findById(id));
if (students.size() > 0){
for (Student student:students){
System.out.println(student);
}
}else{
System.err.println("温馨提示:没有这个ID,请重新输入");
}
}
@Test
public void FindByClass(){
String clazz = "19语教2班";
List<Student> students = dao.findByClass(clazz);
if (students.size() > 0){
for (Student student:students){
System.out.println(student);
}
}else{
System.err.println("温馨提示:没有这个班级,请重新输入");
}
}
@Test
public void testFindByDepartment(){
String department = "信息工程学院";
List<Student> students = dao.findByDepartment(department);
//判断列表里是否有元素
if (students.size() > 0){
for (Student student:students){
System.out.println(student);
}
}else{
System.err.println("温馨提示:查无此人");
}
}
@Test
public void testFindRowsByClass(){
//调用学生数据访问对象的按班级统计人数方法
Vector rows = dao.findRowsByClass();
//获取向量的跌代器
Iterator iterator = rows.iterator();
//遍历迭代器
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
@Test
public void testFindRowsByDepartment(){
//调用学生数据访问对象的按学院统计人数方法
Vector rows = dao.findRowsByDepartment();
//获取向量的跌代器
Iterator iterator = rows.iterator();
//遍历迭代器
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
4、创建用户服务接口实现类
创建用户服务接口实现类UserServiceImpl
package net.cqf.student.service.impl;
import net.cqf.student.dao.UserDao;
import net.cqf.student.dao.impl.UserDaoImpl;
import net.cqf.student.bean.User;
import net.cqf.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);
}
}
单元测试:对UserServiceImpl进行单元测试
package net.cqf.student.test;
import net.cqf.student.dao.UserDao;
import net.cqf.student.dao.impl.UserDaoImpl;
import net.cqf.student.bean.User;
import org.junit.Test;
import java.util.Date;
import java.util.List;
/**
* 功能:测试用户服务接口实现类
* 作者:陈秋帆
* 日期:2020年07月08日
*/
public class TestUserServiceImpl{
UserDao dao = new UserDaoImpl();
@Test
public void testFindById(){
User user = dao.findById(1);
System.out.println("用户名:"+user.getUsername());
System.out.println("密码:"+user.getPassword());
System.out.println("电话:"+user.getTelephone());
System.out.println("注册时间:"+user.getRegisterTime());
}
@Test
public void testLogin(){
String username,password;
username = "admin";
password = "admin";
User user = dao.login(username,password);
if (user != null){
System.out.println("恭喜,用户名与密码正确,登陆成功");
}else{
System.out.println("遗憾。用户名与密码错误,登陆失败");
}
}
@Test
public void testIsUsernameExisted(){
String username = "张三丰";
boolean result = dao.isUsernameExisted(username);
if (result){
System.out.println("温馨提示:["+username+"]已存在,不可用此名注册");
}else{
System.out.println("温馨提示:"+username+"不存在,可用此名注册");
}
}
@Test
public void testInsert(){
User user = new User();
user.setUsername("吴彦文");
user.setPassword("345679");
user.setTelephone("15423245670");
user.setRegisterTime(new Date());
int count = dao.insert(user);
if (count > 0){
System.out.println("恭喜,用户记录插入成功");
System.out.println(dao.findById(dao.findAll().size()));
}else{
System.out.println("遗憾,用户记录插入失败");
}
}
@Test
public void testDeleteById(){
int id = 20190107;
//调用学生数据访问对象的按ID删除方法
int count = dao.deleteById(id);
//判断学生记录是否删除成功
if (count > 0){
System.out.println("恭喜,用户记录删除成功");
}else{
System.out.println("遗憾,用户记录删除失败");
}
}
@Test
public void testUpdate(){
User user = new User();
user.setId(1);
user.setUsername("甘庆源");
user.setPassword("123456");
user.setTelephone("12343425678");
user.setRegisterTime(new Date());//由机械工程学院修改为人文学院
int count = dao.update(user);
if (count > 0){
System.out.println("恭喜,更新学生记录成功");
System.out.println(user);
}else{
System.out.println("遗憾,更新数据失败");
}
}
@Test
public void testFindAll(){
//调用学生数据访问对象的查找全部方法
List<User> users = dao.findAll();
for(User user:users){
System.out.println(user);
}
}
}