溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

spring整合hibernate的方法是什么

發(fā)布時(shí)間:2021-10-13 09:57:44 來(lái)源:億速云 閱讀:150 作者:柒染 欄目:編程語(yǔ)言

本篇文章給大家分享的是有關(guān)spring整合hibernate的方法是什么,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

結(jié)構(gòu):

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>      <!--  數(shù)據(jù)源-->  <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>

測(cè)試:

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); //分頁(yè)--取0-5個(gè)返回//    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的方法是什么,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI