搭建的结构如下
package com.hr.entity;
/**
* @ClassName Student
* @Description: TODO
* @Author 汤永红
* @Date 2020/8/5 0005-9:57
* @Version V1.0
**/
public class Student {
/**
* 学号
*/
private int id;
/**
* 姓名
*/
private String name;
/**
* 密码
*/
private String password;
public Student() {
}
public Student(int id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
package com.hr.dao;
import com.hr.entity.Student;
/**
* @ClassName StudentDao
* @Description: TODO
* @Author 汤永红
* @Date 2020/8/5 0005-9:57
* @Version V1.0
**/
public interface StudentDao {
/**
* 添加一个学员
*
* @param student
* @return
*/
int add(Student student);
/**
* 修改一个学员
*
* @param student
* @return
*/
int update(Student student);
/**
* 根据学号查删除学生
*
* @param id
* @return
*/
int delete(int id);
/**
* 根据学号查询学生
*
* @param id
* @return
*/
Student findOne(int id);
}
package com.hr.dao.impl;
import com.hr.dao.StudentDao;
import com.hr.entity.Student;
/**
* @ClassName StudentDaoImpl
* @Description: TODO
* @Author 汤永红
* @Date 2020/8/5 0005-9:58
* @Version V1.0
**/
public class StudentDaoImpl implements StudentDao {
@Override
public int add(Student student) {
System.out.println("添加了一个学生:" + student.toString());
return 0;
}
@Override
public int update(Student student) {
System.out.println("修改了一个学生:" + student.toString());
return 0;
}
@Override
public int delete(int id) {
System.out.println("删除了一个学生:学号为" + id);
return 0;
}
@Override
public Student findOne(int id) {
System.out.println("查找了一个学生:" + id);
return null;
}
}
package com.hr.service;
import com.hr.entity.Student;
public interface StudentService {
/**
* 添加一个学员
*
* @param student
* @return
*/
boolean add(Student student);
/**
* 修改一个学员
*
* @param student
* @return
*/
boolean update(Student student);
/**
* 根据学号查删除学生
*
* @param id
* @return
*/
boolean delete(int id);
/**
* 根据学号查询学生
*
* @param id
* @return
*/
Student findOne(int id);
}
package com.hr.service.impl;
import com.hr.dao.StudentDao;
import com.hr.dao.impl.StudentDaoImpl;
import com.hr.entity.Student;
import com.hr.service.StudentService;
/**
* @ClassName StudentServiceImpl
* @Description: TODO
* @Author 汤永红
* @Date 2020/8/5 0005-9:59
* @Version V1.0
**/
public class StudentServiceImpl implements StudentService {
//服务层调dao层
StudentDao dao = new StudentDaoImpl();
@Override
public boolean add(Student student) {
int i = dao.add(student);
// if(i>0){
// return true;
// }else{
// return false;
// }
return i > 0 ? true : false;
}
@Override
public boolean update(Student student) {
int i = dao.update(student);
return i > 0 ? true : false;
}
@Override
public boolean delete(int id) {
int i = dao.delete(id);
return i > 0 ? true : false;
}
@Override
public Student findOne(int id) {
Student student = dao.findOne(id);
return student;
}
}
package com.hr.factory;
import com.hr.service.StudentService;
import com.hr.service.impl.StudentServiceImpl;
/**
* @ClassName MyFactory
* @Description: TODO
* @Author 汤永红
* @Date 2020/8/6 0006-9:00
* @Version V1.0
**/
public class MyFactory {
//静态工厂方法
//返回的是接口,new的是实现类,并且方法是静态的
static StudentServiceImpl service;
public static StudentService getStudentServiceInstance() {
if (service == null) {
service = new StudentServiceImpl();
}
return service;
}
}
package com.hr.test;
import com.hr.entity.Student;
import com.hr.factory.MyFactory;
import com.hr.service.StudentService;
/**
* @ClassName TestStudent
* @Description: TODO
* @Author 汤永红
* @Date 2020/8/5 0005-9:59
* @Version V1.0
**/
public class TestStudent {
public static void main(String[] args) {
//调服务层
//StudentService studentService = new StudentServiceImpl();
StudentService studentService = MyFactory.getStudentServiceInstance();
StudentService studentService2 = MyFactory.getStudentServiceInstance();
System.out.println(studentService == studentService2);
//添加
Student stu1 = new Student(10086, "张杰", "88888888");
studentService.add(stu1);
//修改密码
stu1.setPassword("123456");
studentService.update(stu1);
//删除
studentService.delete(stu1.getId());
//查询
stu1 = studentService.findOne(stu1.getId());
System.out.println(stu1);
}
}
运行结果