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

hibernate和spring的整合

 
阅读更多

1.导入spring和hibernate必备的jar包文件

2.新建一个源文件config

3.项目分包和建类


4.hibernate.cfg.xml的总配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 配置方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- 自动创建表 -->
<property name="hbm2ddl.auto">update</property>

<!-- 在控制台打印sql -->
<property name="show_sql">true</property>
<!-- 格式化sql -->
<property name="format_sql">true</property>
</session-factory>
</hibernate-configuration>

5.spring中的bean.xml总配置文件;

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 开启注解扫描 -->
<context:component-scan
base-package="com.eduask" />
<mvc:annotation-driven />
<!-- 引入外部资源 数据库配置文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置dbcp连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="${url}" />
<property name="username" value="${user}" />
<property name="password" value="${pwd}" />
<property name="driverClassName" value="${driver}" />
</bean>

<!-- 配置sessionfactory hibernate总的配置文件-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate/hibernate.cfg.xml"></property>
<!-- 设置数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置扫描 com/eduask/pojo/mapper路径下的 mapper.xml文件-->
<property name="mappingLocations" value="classpath:com/eduask/pojo/mapper/*.hbm.xml"></property>
</bean>
<!-- 配置声明式事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 启用事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- hibernateTemplate-->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>

6.StudentDao类

package com.eduask.dao;
import java.util.List;
import com.eduask.pojo.Student;
//定义一个StudentDao的接口;
public interface StudentDao {
//根据id查询;
Student getStudentById(int id) throws Exception ;
//查询所有;
List<Student> getStudents() throws Exception;
//添加;
void insertStudent(Student student) throws Exception;
//删除;
void deleteStudent(int id) throws Exception;
//修改;
void updateStudent(Student student) throws Exception;
}

7.StudentDaoImpl类;

package com.eduask.dao.impl;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;
import com.eduask.dao.StudentDao;
import com.eduask.pojo.Student;
@Repository
public class StudentDaoImpl implements StudentDao {
private Session session=null;
@Resource(name="sessionFactory")
public void setSessionFactory(SessionFactory sessionFactory){
this.session=sessionFactory.openSession();
}
public Student getStudentById(int id) throws Exception {
// TODO Auto-generated method stub
return (Student) session.get(Student.class, id);
}
public List<Student> getStudents() throws Exception {
//Student为大写;
String hql="from Student";
Query query=session.createQuery(hql);
return query.list();
}
public void insertStudent(Student student) throws Exception {
Transaction tr=session.beginTransaction();
session.save(student);
tr.commit();
session.close();
}
public void deleteStudent(int id) throws Exception {
// TODO Auto-generated method stub
Student student=getStudentById(id);
Transaction tr=session.beginTransaction();
session.delete(student);
tr.commit();
session.close();
}
public void updateStudent(Student student) throws Exception {
// TODO Auto-generated method stub
Transaction tr=session.beginTransaction();
session.update(student);
tr.commit();
session.close();
}
}

8.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;
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{
StudentDao studentDao=cx.getBean(StudentDaoImpl.class);
Student student=new Student();
student.setName("link");
student.setPwd("123456");
studentDao.insertStudent(student);
}
//删除
//删除需要获得id,在StudentDaoImpl类中删除方法中调用获得id的方法;
@Test
public void testDeleteStudent() throws Exception{
StudentDao studentDao=cx.getBean(StudentDaoImpl.class);
studentDao.deleteStudent(1);
}
//修改;
//修改需要获得id;
@Test
public void testUpdateStudent() throws Exception{
StudentDao studentDao=cx.getBean(StudentDaoImpl.class);
Student student=new Student();
student=studentDao.getStudentById(2);
student.setPwd("12345678");
studentDao.updateStudent(student);
}
//查询通过id;
@Test
public void testSelectStudent() throws Exception{
StudentDao studentDao=cx.getBean(StudentDaoImpl.class);
System.out.println(studentDao.getStudentById(2));
}
//查询所有;
@Test
public void testSelectAllStudent() throws Exception{
StudentDao studentDao=cx.getBean(StudentDaoImpl.class);
List<Student> students=studentDao.getStudents();
for (Student student : students) {
System.out.println(student);
}

}
}

分享到:
评论

相关推荐

    第6章 Struts2、Hibernate和Spring整合应用 课堂

    "Struts 2、Hibernate和Spring整合应用" 在本章中,我们将学习如何将Struts 2、Hibernate和Spring三个框架整合应用于一个项目中。Struts 2是一个基于MVC模式的Web应用框架,Hibernate是一个ORM框架,Spring是一个...

    hibernate和spring整合Java项目

    在企业级Java应用中,如电商系统、CRM系统等,Hibernate和Spring的整合可以提供以下优势: - 提高开发效率:通过ORM和DI,开发者可以专注于业务逻辑,而不是底层的数据库操作。 - 简化事务管理:Spring的AOP事务管理...

    10.6struts2+hibernate+spring的整合源代码

    SSH(Struts2+Spring+Hibernate)框架整合是Java Web开发中常见的一种技术栈,能有效地提高开发效率和代码质量。 Struts2作为MVC框架,主要负责处理HTTP请求,控制应用程序的流程,提供视图和控制器的分离。它的...

    Struts 2+Hibernate+Spring整合开发技术详解随书源码18

    虽然具体的章节内容未知,但以上是基于Struts 2、Hibernate和Spring整合开发的常见知识点。在阅读或学习这些源码时,你可以逐步理解每个框架如何协同工作,以及如何通过整合提高开发效率和应用质量。

    Struts_Hibernate_Spring整合JAR包

    当Struts、Hibernate和Spring整合时,通常Spring作为核心框架负责整个应用的管理,包括初始化Struts和Hibernate。Spring可以注入Action类中的依赖,比如DAO(Data Access Object)或Service层对象,这样就可以实现...

    hibernate与spring整合demo

    将Hibernate与Spring整合可以充分利用两者的优点,提高开发效率并降低复杂性。 一、Hibernate概述 Hibernate是Java世界中领先的ORM框架之一,它允许开发者用Java对象来操作数据库记录,而无需编写SQL语句。通过配置...

    Struts2+Hibernate+Spring整合开发深入剖析与范例应用03

    Struts2、Hibernate和Spring是Java企业级应用中三大核心框架,它们的整合使用能够构建出高效、可维护性高的Web应用程序。本篇将深入剖析这三者如何协同工作,并提供实际范例应用。 首先,Struts2作为MVC(模型-视图...

    Struts 2+Hibernate+Spring整合开发技术详解sample.pdf

    Struts 2+Hibernate+Spring整合开发技术详解sample.pdf

    Struts2+Hibernate+Spring整合开发技术详解19章网上书店完整源码

    Struts2+Hibernate+Spring整合开发技术详解19章网上书店完整源码(内附数据库导出文件) 与书上最后一章内容完全对应 可以结合书上教程进行最后一章学习

    ssh整合视频ssh整合视频

    SSH整合是Java开发中一种常见的框架集成方式,它由Struts、Hibernate和Spring三个开源框架组成,用于构建高效、可维护的企业级Web应用程序。在这个"ssh整合视频"中,我们将会学习如何将这三个强大的框架结合在一起,...

    精通J2EE--Eclipse、Struts、Hibernate及Spring整合应用案例全书

    《精通J2EE--Eclipse、Struts、Hibernate及Spring整合应用案例全书》是一本深入探讨J2EE Web应用程序开发的专业书籍。J2EE,全称Java 2 Platform, Enterprise Edition,是Java平台上用于构建企业级分布式应用程序的...

    Flex+hibernate+spring整合

    将Flex、Hibernate和Spring整合在一起,目的是为了构建一个三层架构的应用:表示层(Flex)、业务逻辑层(Spring)和数据访问层(Hibernate)。以下是整合过程中的关键知识点: 1. **通信协议**:Flex与Spring之间...

    HibernateADD (hibernate和spring整合,使用dbcp连接池方式连接数据库)

    在IT行业中,数据库连接管理是应用系统开发中的关键环节,特别是在使用Java进行企业级开发时。...总之,整合Hibernate、Spring和DBCP连接池,可以构建出一个高效、灵活、易于维护的企业级数据库应用。

    SpringMVC+Hibernate+Spring整合实例

    总的来说,"SpringMVC+Hibernate+Spring整合实例"提供了一个全面的学习平台,帮助开发者深入理解Java企业级应用的开发模式和最佳实践。通过这个实例,你可以提升自己的技能,为未来的项目开发打下坚实的基础。

    Struts+Hibernate+Spring整合项目之登录部分

    总的来说,"Struts+Hibernate+Spring整合项目之登录部分"涉及到Java Web开发中的多个核心技术点,包括MVC架构的理解、ORM工具的使用、依赖注入的实践以及安全机制的实施。通过这个项目的实现,开发者可以深入理解...

    Hibernate Spring整合入门程序

    当我们将Hibernate和Spring整合时,Spring可以管理Hibernate的SessionFactory和Session,从而简化事务管理。Spring通过其声明式事务管理特性,可以自动处理事务的开始、提交或回滚,避免了手动编写事务管理代码。...

    struts2、hibernate、spring整合

    将Struts2、Hibernate和Spring整合,可以构建出一个完整的MVC架构应用。Spring作为基础框架,负责IoC和AOP,管理所有组件的生命周期。Hibernate作为数据访问层,处理数据库交互。Struts2作为展现层,接收用户的HTTP...

    精通J2EE——Eclipse+Struts+Hibernate+Spring整合应用案例(pdf完整版).part2-1

    精通J2EE——Eclipse+Struts+Hibernate+Spring整合应用案例 pdf电子书, 包含所有18个章节的完整版, 美中不足的是扫描质量不是很高,且是第一版的,不是目前最新的第二版! 请看清楚了再下,免得后悔! 共有两个部分-这...

    Struts 2+Hibernate+Spring整合开发技术详解 12~17章

    这本由蒲子明编著的《Struts 2+Hibernate+Spring整合开发技术详解》深入探讨了如何将这三个框架有效结合,以构建高效、模块化的Web应用。书中第12到17章的内容涵盖了以下几个关键知识点: 1. **Struts 2框架**: -...

Global site tag (gtag.js) - Google Analytics