详解spring整合hibernate的方法

 更新时间:2020年02月03日 08:58:47   作者:crazy戴夫  
这篇文章主要介绍了spring整合hibernate的方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

结构:

Spring和Hibernate整合借助于HibernateTemplate

applicationContext.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:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/aop 
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  http://www.springframework.org/schema/context   
  http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
  <bean name="c" class="com.lc.pojo.Category">
    <property name="name" value="yyy" />
  </bean>
  
  <bean name="dao" class="com.lc.dao.CategoryDAO">
    <property name="sessionFactory" ref="sf" />
  </bean>

  <bean name="sf"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="ds" />
    <property name="mappingResources">
      <list>
        <value>com/lc/pojo/Category.hbm.xml</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <value>
        hibernate.dialect=org.hibernate.dialect.MySQLDialect
        hibernate.show_sql=true
        hbm2ddl.auto=update
        </value>
    </property>
  </bean>  
    
<!--  数据源-->
  <bean name="ds"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=GBK" />
    <property name="username" value="root" />
    <property name="password" value="123456" />
  </bean>  
 
</beans>

测试:

ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
    CategoryDAO bean =(CategoryDAO) context.getBean("dao");

    DetachedCriteria criteria = DetachedCriteria.forClass(Category.class);

//    List<Category> list = bean.findByCriteria(criteria, 0, 5); //分页--取0-5个返回
//    System.out.println(list);

 Category.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.lc.pojo">
  <class name="Category" table="category_">
    <id name="id" column="id">
      <generator class="native">
      </generator>
    </id>
    <property name="name" />
  </class>
</hibernate-mapping>

result:

总结

以上所述是小编给大家介绍的spring整合hibernate的方法,希望对大家有所帮助!

相关文章

  • 深度解析Java中volatile的内存语义实现以及运用场景

    深度解析Java中volatile的内存语义实现以及运用场景

    这篇文章主要介绍了Java中volatile的内存语义实现以及运用场景,通过JVM的机制来分析volatile关键字在线程编程中的作用,需要的朋友可以参考下
    2015-12-12
  • Dwr3.0纯注解(纯Java Code配置)配置与应用浅析三之后端反向调用前端

    Dwr3.0纯注解(纯Java Code配置)配置与应用浅析三之后端反向调用前端

    Dwr是为人所熟知的前端框架,其异步推送功能是为人所津津乐道的,下来主要研究一下它的这个功能是怎么应用的;
    2016-04-04
  • SpringBoot整合EasyExcel实现Excel表格导出功能

    SpringBoot整合EasyExcel实现Excel表格导出功能

    这篇文章主要介绍了SpringBoot整合EasyExcel实现Excel表格导出功能,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的朋友可以参考一下
    2022-07-07
  • Mybatis延迟加载的实现方式

    Mybatis延迟加载的实现方式

    这篇文章主要介绍了Mybatis延迟加载的实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • Java抓包工具fiddler实现请求转发

    Java抓包工具fiddler实现请求转发

    Fiddler是一个http协议调试代理工具,本文主要介绍了Java抓包工具fiddler实现请求转发,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • JDK1.8中的ConcurrentHashMap使用及场景分析

    JDK1.8中的ConcurrentHashMap使用及场景分析

    这篇文章主要介绍了JDK1.8中的ConcurrentHashMap使用及场景分析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01
  • SpringMVC参数的传递之如何接收List数组类型的数据

    SpringMVC参数的传递之如何接收List数组类型的数据

    这篇文章主要介绍了SpringMVC参数的传递之如何接收List数组类型的数据,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10
  • SpringBoot使用Sa-Token实现路径拦截和特定接口放行

    SpringBoot使用Sa-Token实现路径拦截和特定接口放行

    这篇文章主要介绍了SpringBoot使用Sa-Token实现路径拦截和特定接口放行,文中通过代码示例讲解的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
    2024-06-06
  • iBatis习惯用的16条SQL语句

    iBatis习惯用的16条SQL语句

    iBatis 是apache 的一个开源项目,一个O/R Mapping 解决方案,iBatis 最大的特点就是小巧,上手很快.这篇文章主要介绍了iBatis习惯用的16条SQL语句的相关资料,需要的朋友可以参考下
    2016-10-10
  • SpringBoot整合OpenCV的实现示例

    SpringBoot整合OpenCV的实现示例

    这篇文章主要介绍了SpringBoot整合OpenCV的实现示例。文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12

最新评论