hibernate和spring的完整结合使用service层

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);
}
}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值