`
吕金含
  • 浏览: 87886 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

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

}

分享到:
评论

相关推荐

    java+hibernate+spring

    在描述中提到的"例子"可能是一个包含配置文件、实体类、DAO(Data Access Object)层、Service层和Controller层的完整项目结构。配置文件通常包括Spring的bean配置文件和Hibernate的持久化配置文件。实体类是对应...

    hibernate+spring注解例子

    本教程将详细阐述`Hibernate`注解和`Spring`注解的使用,以及如何在一个登录示例中结合它们。 `Hibernate`是一个强大的对象关系映射(ORM)框架,它允许开发者使用面向对象的方式来处理数据库操作,而无需直接编写...

    用 Hibernate 和 Spring 开发持久层.zip

    本教程将深入探讨如何结合使用Hibernate和Spring来构建高效、灵活的持久层。 **Hibernate** 是一个对象关系映射(ORM)框架,它简化了Java应用程序与数据库的通信。通过Hibernate,开发者可以使用面向对象的编程...

    struts2,hibernate,spring整合源码,配置文件,jar包

    在这个模块中,用户注册信息可能通过Struts2的Action处理后,通过Spring的Service层调用Hibernate来与数据库交互,完成用户信息的存储。 学习这个整合项目,开发者可以深入理解三大框架如何协同工作,提高代码的可...

    Spring中使用Hibernate

    以下将详细介绍如何在Spring中配置和使用Hibernate。 首先,我们需要添加相应的库依赖。Spring和Hibernate的最新版本通常可以在Maven或Gradle的依赖仓库中找到。确保在你的`pom.xml`或`build.gradle`文件中包含...

    Spring + Hibernate + Spring mvc +Ajax 整合

    这个项目可能是为了演示如何在一个简单的添加删除操作中使用这些技术,通过整合Spring、Hibernate、Spring MVC和Ajax,实现一个高效、用户友好的后台管理系统。开发者可以通过学习这个示例,掌握如何在实际项目中...

    Hibernate+Spring增删改查小例子

    本示例以“Hibernate+Spring增删改查小例子”为主题,旨在帮助初学者快速理解如何将这两个框架结合使用来实现对数据库的基本操作。 首先,Hibernate是一个对象关系映射(ORM)框架,它允许开发者使用面向对象的方式...

    用Hibernate和Spring开发事务持久层.rar_spring

    结合Spring和Hibernate进行事务管理,通常会采用Spring的JDBC抽象层或者Hibernate的SessionFactory来处理数据库交互。Spring通过AOP代理在合适的时候调用Hibernate的Session方法,控制事务的开启、提交、回滚等行为...

    SSH整合 struts+hibernate+spring

    在SSH整合中,如何将Struts与Spring结合使用是一个难点。默认情况下,Struts负责Action类的创建,这与Spring容器的管理方式不符。因此,需要对Struts进行一定的定制。 - **Struts与Spring整合的关键点**: - **...

    SpringMVC+Hibernate+Spring

    在SpringMVC和Hibernate的集成中,Spring可以配置和管理这两个组件的bean,例如,通过Bean定义加载Hibernate的SessionFactory,以及为Controller注入Service层的对象。 在实际的项目中,整合SpringMVC、Hibernate和...

    Spring+hibernate整合源代码

    可以使用 Spring 提供的 TestContext 框架,结合 JUnit 或 TestNG,对 Service 和 DAO 层进行测试。 10. **最佳实践**:在实际项目中,为了提高性能和可维护性,通常会采用 MyBatis 或 JPA 作为更轻量级的数据访问...

    Spring3.2.5Hibernate4.0.1Integration 完整範例

    4. **Service 层**:定义业务逻辑接口和实现,使用 Spring 的 `@Service` 注解,通常调用 DAO 进行数据操作。 5. **Controller 层**:使用 Spring MVC 的 `@Controller` 注解,负责接收请求并调用 Service 层的方法...

    网络硬盘(Struts 2+Hibernate+Spring实现)

    Struts 2的动作(Action)通常会依赖Spring管理的Service层,而Service层又会借助Hibernate进行数据库交互。Spring还负责初始化和管理Struts 2的相关配置,如拦截器、Action的实例化等。这种集成方式使得系统具备...

    jsp servlet struts hibernate spring

    【标题】: "Java Web开发中的JSP、Servlet、Struts、Hibernate和Spring技术详解" 【描述】: "本文深入探讨Java Web开发中常见的技术,包括JSP、Servlet、Struts、Hibernate和Spring,讲解它们的工作原理和实用技巧...

    SpringMVC+Hibernate+Spring+maven框架搭建

    Spring与Spring MVC和Hibernate结合,可以实现服务层和数据访问层的无缝集成,提供一套完整的解决方案。 在使用Maven进行项目构建时,Maven是Apache软件基金会的一个项目管理工具,它通过定义项目对象模型(POM)来...

    J2EE--Eclipse+Struts+Hibernate+Spring

    Spring与Hibernate结合,可以简化数据访问层的实现,通过Spring JDBC或MyBatis等,可以更方便地处理数据库操作。此外,Spring的MVC模块可以与Struts一起使用,增强应用的控制层功能。 在"J2EE--Eclipse+Struts+...

    hibernate+spring复习大纲

    1. **Spring管理Hibernate Session**: 使用`HibernateTemplate`或`SessionFactoryBean`,在Spring容器中管理`SessionFactory`和`Session`。 2. **事务管理集成**: Spring的`PlatformTransactionManager`负责...

    Struts2+hibernate+spring整合泛型DAO

    整合Struts2、Hibernate和Spring,我们通常会使用Spring的ApplicationContext来管理Bean,包括Action类、Service层和DAO层。Service层作为业务逻辑层,调用DAO层的方法进行数据操作。而DAO层通过泛型接口和实现类,...

    Spring与Hibernate结合使用

    下面将详细介绍Spring和Hibernate结合使用的核心知识点。 1. **Spring框架**: - **依赖注入(Dependency Injection, DI)**:Spring的核心特性之一,通过DI,我们可以将对象间的依赖关系解耦,使代码更易于测试和...

    Struts2+Hibernate+Spring 整合示例

    它们各自负责应用程序的不同层面:Struts2处理MVC(Model-View-Controller)架构中的控制层,Hibernate专注于数据持久化,而Spring则提供了全面的依赖注入和事务管理,以及AOP(面向切面编程)支持。将这三个框架...

Global site tag (gtag.js) - Google Analytics