service是业务逻辑节点,方法中会包含业务逻辑处理过程,调用不同的Dao接口进行数据库的作业。
而且在Service一层会对业务逻辑进行事务控制。
package com.ntqingniao.sm.service;
import com.ntqingniao.sm.bean.Student; import com.ntqingniao.sm.dao.IStudentDao; import com.ntqingniao.sm.dao.impl.StudentDaoImpl;
public class StudentService { public static void main(String[] args) throws Exception { StudentService service = new StudentService(); //service.regStudent(new Student(null,"王五","wangwu","320611111111","123@qq.com",null)); System.out.println(service.activeStudent(214)?"success":"error"); } private IStudentDao studentDao = new StudentDaoImpl(); /** * 学生注册 * @param stu * @return * @throws Exception */ public Student regStudent(Student stu) throws Exception { stu.setState(0); // 给学生的状态设置初始值0 return studentDao.addStudent(stu); } /** * 学生激活 * @param id * @return * @throws Exception */ public boolean activeStudent(int id) throws Exception { Student stu = studentDao.findStudentById(id); // 如果能找到学生对象 if (null != stu) { // 只有是初始状态下才能被激活 if (stu.getState() == 0) { stu.setState(1); boolean flag = studentDao.updateStudent(stu); // 如果找到一个更新记录表示成功 if (flag) { return true; } } } return false; } /** * 学生基本信息编辑 * @param stu * @return */ public boolean editStudent(Student stu) { return false; } } |