---
title: "spring3整合hibernate3"
author: "X_boy"
date: "2011-10-01T23:19:21Z"
tags: []
categories: ["企业架构"]
slug: "spring3-hibernate3"
draft: false
lastmod: "2011-10-04T01:50:31Z"
id: 1184397
url: "https://x-boy.iteye.com/blog/1184397"
---

![spring3整合hibernate3的eclipse架构图](http://dl.iteye.com/upload/attachment/564075/d3bc65dd-7e02-325f-8d94-119cf4c80a94.jpg)

1、bean.xml文件

 

 

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" 

"http://www.springframework.org/dtd/spring-beans-2.0.dtd"&gt;

&lt;beans&gt;

 

&lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"

destroy-method="close"&gt;

&lt;property name="driverClass" value="com.mysql.jdbc.Driver" /&gt;

&lt;property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hibernate?                   useUnicode=true&amp;characterEncoding=UTF-8" /&gt;  

&lt;property name="user" value="root" /&gt;

&lt;property name="password" value="mysql" /&gt;

&lt;property name="maxPoolSize" value="40" /&gt;

&lt;property name="minPoolSize" value="1" /&gt;

&lt;property name="initialPoolSize" value="1" /&gt;

&lt;property name="maxIdleTime" value="20" /&gt;

&lt;/bean&gt;

 

&lt;bean id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;

&lt;property name="dataSource" ref="dataSource" /&gt;

&lt;property name="mappingResources"&gt;

&lt;list&gt;

&lt;value&gt;hibernatetest/News.hbm.xml&lt;/value&gt;

&lt;/list&gt;

&lt;/property&gt;

 

&lt;property name="hibernateProperties"&gt;

&lt;value&gt;

hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect

hibernate.hbm2ddl.auto=update

hibernate.shou\_sql=true

hibernate.format\_sql=true

&lt;/value&gt;

 

&lt;/property&gt;

 

&lt;/bean&gt;

&lt;bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"&gt;

&lt;property name="sessionFactory" ref="sessionFactory"/&gt;

&lt;/bean&gt;

&lt;/beans&gt;

 

2、hibernate.cfg.xml文件 

 

 

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;!DOCTYPE hibernate-configuration PUBLIC

        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;

&lt;hibernate-configuration&gt;

&lt;session-factory&gt;

&lt;property name="connection.driver\_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;

&lt;property name="connection.url"&gt;jdbc:mysql://localhost:3306/hibernate&lt;/property&gt;

&lt;property name="connection.useUnicode"&gt;true&lt;/property&gt; 

               &lt;property name="connection.characterEncoding"&gt;UTF-8&lt;/property&gt;

&lt;property name="connection.username"&gt;root&lt;/property&gt;

&lt;property name="connection.password"&gt;mysql&lt;/property&gt;

&lt;property name="hibernate.c3p0.max\_size"&gt;20&lt;/property&gt;

&lt;property name="hibernate.c3p0.min\_size"&gt;1&lt;/property&gt;

&lt;property name="hibernate.c3p0.timeout"&gt;5000&lt;/property&gt;

&lt;property name="hibernate.c3p0.max\_statements"&gt;100&lt;/property&gt;

&lt;property name="hibernate.c3p0.idle\_test\_period"&gt;3000&lt;/property&gt;

&lt;property name="hibernate.c3p0.acquire\_increment"&gt;2&lt;/property&gt;

&lt;property name="hibernate.c3p0.validate"&gt;true&lt;/property&gt;

&lt;property name="dialect"&gt;org.hibernate.dialect.MySQLInnoDBDialect&lt;/property&gt;

&lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt;

 

&lt;mapping resource="hibernatetest/News.hbm.xml" /&gt;

 

&lt;/session-factory&gt;

&lt;/hibernate-configuration&gt;

 

3、News.java文件

package hibernatetest;

public class News {

private Integer id;

private String title;

private String content;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getContent() {

return content;

}

public void setContent(String content) {

this.content = content;

}

}

4、News.hbm.xml文件

&lt;?xml version="1.0"?&gt;

&lt;!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"&gt;

&lt;!--

  Used to demonstrate the declarative configuration

  of both hbm files and annotated classes

  See hibernate.cfg.xml and ConfigurationTest

--&gt;

&lt;hibernate-mapping package="hibernatetest"&gt;

    &lt;class name="News" table="news\_table"&gt;

        &lt;id name="id"&gt;

            &lt;generator class="identity"/&gt;

        &lt;/id&gt;

        &lt;property name="title"/&gt;

        &lt;property name="content"/&gt;

    &lt;/class&gt;

&lt;/hibernate-mapping&gt;

5、测试java类文件

package springmanager;

import java.util.List;

import hibernatetest.News;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.orm.hibernate3.HibernateTemplate;

public class SpringTest {

public static void main(String\[] args){

ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

HibernateTemplate ht=(HibernateTemplate)context.getBean("hibernateTemplate");

                //向数据库插入记录

News n=new News();

n.setTitle("整合成功了");

n.setContent("网站地址http://www.iteye.com");

ht.save(n);

                //列出数据库中的内容（只列出title）

List list=ht.find("from News");

for(int i=0;i&lt;list.size();i++){

News news=(News)list.get(i);

System.out.println(news.getTitle());

}

}

}