詳解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); //分頁(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>
result:
總結(jié)
以上所述是小編給大家介紹的spring整合hibernate的方法,希望對(duì)大家有所幫助!
相關(guān)文章
基于selenium-java封裝chrome、firefox、phantomjs實(shí)現(xiàn)爬蟲(chóng)
這篇文章主要介紹了基于selenium-java封裝chrome、firefox、phantomjs實(shí)現(xiàn)爬蟲(chóng),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2020-10-10servlet實(shí)現(xiàn)文件下載的步驟及說(shuō)明詳解
這篇文章主要為大家詳細(xì)介紹了servlet實(shí)現(xiàn)文件下載的步驟及說(shuō)明,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09MyBatis-Plus如何通過(guò)注解使用TypeHandler
這篇文章主要介紹了MyBatis-Plus如何通過(guò)注解使用TypeHandler,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01springboot項(xiàng)目啟動(dòng)類(lèi)錯(cuò)誤(找不到或無(wú)法加載主類(lèi) com.**Application)
本文主要介紹了spring-boot項(xiàng)目啟動(dòng)類(lèi)錯(cuò)誤(找不到或無(wú)法加載主類(lèi) com.**Application),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05java定長(zhǎng)隊(duì)列的實(shí)現(xiàn)示例
定長(zhǎng)隊(duì)列是一種有限容量的隊(duì)列,對(duì)于某些應(yīng)用場(chǎng)景非常有用,本文主要介紹了java定長(zhǎng)隊(duì)列的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02