hibernate和spring的完整结合使用service层
来源:互联网 发布:数据统计分析怎么写 编辑:程序博客网 时间:2024/06/05 04:41
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);
}
}
}
- hibernate和spring的完整结合使用service层
- Spring+hibernate dao和service层的单元测试
- Spring+HIbernate通用层Dao和Service实现
- Spring与Hibernate结合的使用
- Hibernate和spring结合的OpenSessionInViewFilter
- 利用hibernate对dao层和service层的重用
- struts+spring+hibernate的web应用 Service层代码编写
- struts+spring+hibernate的web应用 Service层代码编写
- struts+spring+hibernate的web应用 Service层代码编写
- 关于spring+hibernate+struts架构service层的作用
- struts+spring+hibernate的web应用 Service层代码编写
- 关于spring+hibernate+struts架构service层的作用
- Spring整合hibernate(5)之Service层的分析
- struts,hibernate , spring结合使用
- Spring与Hibernate结合使用
- Hibernate和Spring结合使用时的不能lazy loading的问题
- spring+struts2是怎么结合的使用的,spring+Hibernate是如何结合使用的?
- 基于Spring、Hibernate的通用DAO层与Service层的实现
- POJ 4355 Party All the Time(三分)
- struts2的学习笔记二
- uc高级编程之权限
- 树状数组
- 声场模型如何区分近场和远场
- hibernate和spring的完整结合使用service层
- 从零开始搭建JAVA编程环境
- grails中Template
- 220. Contains Duplicate III—medium
- 获取前端数据,后台的处理
- Linux下C线程池的实现(转)
- 漫步微积分十四——增、减函数和极大、极小值
- c#基础入门(3)——异常、三元表达式、转义符、类型转换
- FastJson的使用