詳解spring整合hibernate的方法
結(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); //分頁--取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>
result:

總結(jié)
以上所述是小編給大家介紹的spring整合hibernate的方法,希望對(duì)大家有所幫助!
相關(guān)文章
深度解析Java中volatile的內(nèi)存語義實(shí)現(xiàn)以及運(yùn)用場(chǎng)景
這篇文章主要介紹了Java中volatile的內(nèi)存語義實(shí)現(xiàn)以及運(yùn)用場(chǎng)景,通過JVM的機(jī)制來分析volatile關(guān)鍵字在線程編程中的作用,需要的朋友可以參考下2015-12-12
Dwr3.0純注解(純Java Code配置)配置與應(yīng)用淺析三之后端反向調(diào)用前端
Dwr是為人所熟知的前端框架,其異步推送功能是為人所津津樂道的,下來主要研究一下它的這個(gè)功能是怎么應(yīng)用的;2016-04-04
SpringBoot整合EasyExcel實(shí)現(xiàn)Excel表格導(dǎo)出功能
這篇文章主要介紹了SpringBoot整合EasyExcel實(shí)現(xiàn)Excel表格導(dǎo)出功能,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07
Java抓包工具fiddler實(shí)現(xiàn)請(qǐng)求轉(zhuǎn)發(fā)
Fiddler是一個(gè)http協(xié)議調(diào)試代理工具,本文主要介紹了Java抓包工具fiddler實(shí)現(xiàn)請(qǐng)求轉(zhuǎn)發(fā),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
JDK1.8中的ConcurrentHashMap使用及場(chǎng)景分析
這篇文章主要介紹了JDK1.8中的ConcurrentHashMap使用及場(chǎng)景分析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
SpringMVC參數(shù)的傳遞之如何接收List數(shù)組類型的數(shù)據(jù)
這篇文章主要介紹了SpringMVC參數(shù)的傳遞之如何接收List數(shù)組類型的數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
SpringBoot使用Sa-Token實(shí)現(xiàn)路徑攔截和特定接口放行
這篇文章主要介紹了SpringBoot使用Sa-Token實(shí)現(xiàn)路徑攔截和特定接口放行,文中通過代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-06-06
SpringBoot整合OpenCV的實(shí)現(xiàn)示例
這篇文章主要介紹了SpringBoot整合OpenCV的實(shí)現(xiàn)示例。文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12

