1.使用service层;
2.StudetnService类;
package com.eduask.service;
import java.util.List;
import com.eduask.pojo.Student;
public interface StudentService {
//增加;
void insertStudent(Student student ) throws Exception;
//删除;
void delectStudent(int id) throws Exception;
//修改;
void updateStudent(Student student) throws Exception;
//查询根据id;
Student selectStudentById(int id) throws Exception;
//查询所有;
List<Student> selectAllStudent() throws Exception;
}
3.StudentServiceImpl类;
package com.eduask.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.springframework.stereotype.Service;
import com.eduask.dao.StudentDao;
import com.eduask.pojo.Student;
import com.eduask.service.StudentService;
@Service
public class StudentServiceImpl implements StudentService {
@Resource(name="studentDaoImpl")
private StudentDao studentDao;
public void insertStudent(Student student) throws Exception {
studentDao.insertStudent(student);
}
public void delectStudent(int id) throws Exception {
studentDao.deleteStudent(id);
}
public void updateStudent(Student student) throws Exception {
studentDao.updateStudent(student);
}
public Student selectStudentById(int id) throws Exception {
Student student=studentDao.getStudentById(id);
return student;
}
public List<Student> selectAllStudent() throws Exception {
return studentDao.getStudents();
}
}
4.StudentTest类;
package com.eduask.test;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.eduask.dao.StudentDao;
import com.eduask.dao.impl.StudentDaoImpl;
import com.eduask.pojo.Student;
import com.eduask.service.StudentService;
import com.eduask.service.impl.StudentServiceImpl;
public class StudentTest {
//引入spring中的bean.xml配置文件;
private ClassPathXmlApplicationContext cx=null;
@Before
public void setUp(){
cx=new ClassPathXmlApplicationContext("spring/bean.xml");
}
//增加;
@Test
public void testInsertStudent() throws Exception{
StudentService studentService=cx.getBean(StudentServiceImpl.class);
Student student=new Student();
student.setName("linux");
student.setPwd("123456");
studentService.insertStudent(student);
}
//删除
//删除需要获得id,在StudentDaoImpl类中删除方法中调用获得id的方法;
@Test
public void testDeleteStudent() throws Exception{
StudentService studentService=cx.getBean(StudentServiceImpl.class);
studentService.delectStudent(2);
}
//修改;
//修改需要获得id;
@Test
public void testUpdateStudent() throws Exception{
StudentService studentService=cx.getBean(StudentServiceImpl.class);
Student student=new Student();
student=studentService.selectStudentById(2);
student.setPwd("11111");
studentService.updateStudent(student);
}
//查询通过id;
@Test
public void testSelectStudent() throws Exception{
StudentService studentService=cx.getBean(StudentServiceImpl.class);
System.out.println(studentService.selectStudentById(2));
}
//查询所有;
@Test
public void testSelectAllStudent() throws Exception{
StudentService studentService=cx.getBean(StudentServiceImpl.class);
List<Student> students=studentService.selectAllStudent();
for (Student student : students) {
System.out.println(student);
}
}
}