ITeye博客

spring3整合hibernate3

X_boy · 2011-10-01 23:19 · J2EE · 企业架构 · Markdown

spring3整合hibernate3的eclipse架构图

1、bean.xml文件

 

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" 

"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

 

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"

destroy-method="close">

<property name="driverClass" value="com.mysql.jdbc.Driver" />

<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hibernate?                   useUnicode=true&characterEncoding=UTF-8" />  

<property name="user" value="root" />

<property name="password" value="mysql" />

<property name="maxPoolSize" value="40" />

<property name="minPoolSize" value="1" />

<property name="initialPoolSize" value="1" />

<property name="maxIdleTime" value="20" />

</bean>

 

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="mappingResources">

<list>

<value>hibernatetest/News.hbm.xml</value>

</list>

</property>

 

<property name="hibernateProperties">

<value>

hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect

hibernate.hbm2ddl.auto=update

hibernate.shou_sql=true

hibernate.format_sql=true

</value>

 

</property>

 

</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">

<property name="sessionFactory" ref="sessionFactory"/>

</bean>

</beans>

 

2、hibernate.cfg.xml文件 

 

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC

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

        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>

<property name="connection.useUnicode">true</property> 

               <property name="connection.characterEncoding">UTF-8</property>

<property name="connection.username">root</property>

<property name="connection.password">mysql</property>

<property name="hibernate.c3p0.max_size">20</property>

<property name="hibernate.c3p0.min_size">1</property>

<property name="hibernate.c3p0.timeout">5000</property>

<property name="hibernate.c3p0.max_statements">100</property>

<property name="hibernate.c3p0.idle_test_period">3000</property>

<property name="hibernate.c3p0.acquire_increment">2</property>

<property name="hibernate.c3p0.validate">true</property>

<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

<property name="hbm2ddl.auto">update</property>

 

<mapping resource="hibernatetest/News.hbm.xml" />

 

</session-factory>

</hibernate-configuration>

 

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文件

<?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">

<!--

  Used to demonstrate the declarative configuration

  of both hbm files and annotated classes

  See hibernate.cfg.xml and ConfigurationTest

-->

<hibernate-mapping package="hibernatetest">

    <class name="News" table="news_table">

        <id name="id">

            <generator class="identity"/>

        </id>

        <property name="title"/>

        <property name="content"/>

    </class>

</hibernate-mapping>

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<list.size();i++){

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

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

}

}

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。