spring 集成hibernate
生活随笔
收集整理的這篇文章主要介紹了
spring 集成hibernate
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?http://tianya23.blog.51cto.com/1081650/275301
Spring整合Hibernate
由于Spring和Hibernate處于不同的層次,Spring關心的是業(yè)務邏輯之間的組合關系,Spring提供了對他們的強大的管理能力, 而Hibernate完成了OR的映射,使開發(fā)人員不用再去關心SQL語句,直接與對象打交道。 將Hibernate做完映射之后的對象交給Spring來管理是再合適不過的事情了, Spring也同時提供了對Hibernate的SessionFactory的集成功能。實例如下:1、首先Hibernate完成OR映射功能
POJO類:Person.java 【注意:必須要保留一個無參的構造方法】 public class Person {??private Integer id;
??private String name;
??
??public Person(){}
??
??public Person(String name) {
????this.name = name;
??}
??
??public Integer getId() {
????return id;
??}
??public void setId(Integer id) {
????this.id = id;
??}
??public String getName() {
????return name;
??}
??public void setName(String name) {
????this.name = name;
??}
} 完成從對象到數據庫字段的對應關系的映射配置:Person.hbm.xml <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
????????????????"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
????????????????"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.comp.bean">
????????<class name="Person" table="person">
??????????<cache usage="read-write" region="cn.comp.bean.Person"/>
????????????????<id name="id">
????????????????????????<generator class="native"/>
????????????????</id>
????????????????<property name="name" length="10" not-null="true"/>
????????</class>
</hibernate-mapping>
2、讓Hibernate完成后的對象交給Spring來管理
配置Hibernate與Spring的關系:beans.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:context="http://www.springframework.org/schema/context"
???????????? xmlns:aop="http://www.springframework.org/schema/aop"
???????????? xmlns:tx="http://www.springframework.org/schema/tx"
???????????? xsi:schemaLocation="http://www.springframework.org/schema/beans
???????????????????? http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
???????????????????? http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
???????????????????? http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
???????????????????? http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
????<context:annotation-config/>
????<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
???????? <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
???????? <property name="url" value="jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=UTF-8"/>
???????? <property name="username" value="root"/>
???????? <property name="password" value="123456"/>
????????????<!-- 連接池啟動時的初始值 -->
???? <property name="initialSize" value="1"/>
???? <!-- 連接池的最大值 -->
???? <property name="maxActive" value="500"/>
???? <!-- 最大空閑值.當經過一個高峰時間后,連接池可以慢慢將已經用不到的連接慢慢釋放一部分,一直減少到maxIdle為止 -->
???? <property name="maxIdle" value="2"/>
???? <!--????最小空閑值.當空閑的連接數少于閥值時,連接池就會預申請去一些連接,以免洪峰來時來不及申請 -->
???? <property name="minIdle" value="1"/>
???? </bean>
????
??<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
????????????<property name="dataSource" ref="dataSource"/>
???? <property name="mappingResources">
????????????<list>
????????????????<value>cn/itcast/bean/Person.hbm.xml</value>
????????????</list>
???? </property>
????????????<property name="hibernateProperties">
????????????<value>
????????????????????hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
????????????????????hibernate.hbm2ddl.auto=update
????????????????????hibernate.show_sql=false
????????????????????hibernate.format_sql=false
????????????????????hibernate.cache.use_second_level_cache=true
????????????????????????????????hibernate.cache.use_query_cache=false
???????????????????????? hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
????????????????</value>
????????????</property>
??</bean>
??<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
????????<property name="sessionFactory" ref="sessionFactory"/>
??</bean>
??<tx:annotation-driven transaction-manager="txManager"/>
??<bean id="personService" class="cn.comp.service.impl.PersonServiceBean"/>
??<bean id="personList" class="cn.comp.web.PersonAction"/>
</beans> 抽取業(yè)務接口:PersonService.java public interface PersonService {
??public void save(Person person);
??public void update(Person person);
??public Person getPerson(Integer personid);
??public void delete(Integer personid);
??public List<Person> getPersons();
} 實現類完成對業(yè)務邏輯的操作:PersonServiceBean.java @Transactional
public class PersonServiceBean implements PersonService {
??@Resource private SessionFactory sessionFactory;
??public void save(Person person){
????sessionFactory.getCurrentSession().persist(person);
??}
??
??public void update(Person person){
????sessionFactory.getCurrentSession().merge(person);
??}
??@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
??public Person getPerson(Integer personid){
????return (Person)sessionFactory.getCurrentSession().get(Person.class, personid);
??}
??public void delete(Integer personid){
????sessionFactory.getCurrentSession().delete(
????????sessionFactory.getCurrentSession().load(Person.class, personid));
??}
??@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
??@SuppressWarnings("unchecked")
??public List<Person> getPersons(){????
????return sessionFactory.getCurrentSession().createQuery("from Person").list();
??}
??
}
3、完成對實現類的測試
對實現類的測試類:?PersonServiceTest.java public class PersonServiceTest {??private static PersonService personService;
??
??@BeforeClass
??public static void setUpBeforeClass() throws Exception {
????try {
??????ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
??????personService = (PersonService)applicationContext.getBean("personService");
????} catch (RuntimeException e) {
??????e.printStackTrace();
????}
??}
??@Test
??public void testSave() {
????personService.save(new Person("小張"));
??}
??@Test
??public void testUpdate() {
????Person person = personService.getPerson(1);
????//....
????person.setName("小麗");
????personService.update(person);
??}
??@Test
??public void testGetPerson() {
????Person person = personService.getPerson(2);
????System.out.println(person.getName());
????try {
??????System.out.println("請關閉數據庫");
??????Thread.sleep(1000*15);
????} catch (InterruptedException e) {
??????e.printStackTrace();
????}
????System.out.println("第二次開始獲取");
????person = personService.getPerson(2);
????System.out.println(person.getName());
??}
??@Test
??public void testDelete() {
????personService.delete(1);
??}
??@Test
??public void testGetPersons() {
????List<Person> persons = personService.getPersons();
????for(Person person : persons){
??????System.out.println(person.getName());
????}
??}
} 4、附件 中間使用到DBCP作為數據庫連接緩存,關于其性能參考本文章的附件部分
?
總結
以上是生活随笔為你收集整理的spring 集成hibernate的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring中事务配置的3种方式
- 下一篇: spring viewResolver