温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

spring整合hibernate的方法是什么

发布时间:2021-10-13 09:57:44 来源:亿速云 阅读:147 作者:柒染 栏目:编程语言

本篇文章给大家分享的是有关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>


以上就是spring整合hibernate的方法是什么,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI