spring集成hibernate配置

spring配置文件:

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


<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
        default-autowire="byName">

<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/wangfu"/>     
<property name="user" value="root"/>     
<property name="password" value="root"/>     
</bean>  
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mappingDirectoryLocations">
            <list>
                <value>classpath*:hbconfig/</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
            </props>
        </property>
</bean>

</beans>

 

 

测试配置文件:

使用XmlBeanFactory获取bean.xml的配置信息

XmlBeanFactory factory=new XmlBeanFactory(new ClassPathResource("bean.xml"));

获取spring的JdbcTemplate模板用于jdbc操作

org.springframework.jdbc.core.JdbcTemplate template=(org.springframework.jdbc.core.JdbcTemplate)factory.getBean("jdbcTemplate");
        String sql="insert into test(name) values ('name1asdfasdfasdfasdf')";
        template.execute(sql);

 

获取org.hibernate.SessionFactory对象

org.hibernate.SessionFactory
        sessionFactory=(org.hibernate.SessionFactory)factory.getBean("sessionFactory");

 

出始化org.springframework.orm.hibernate3.HibernateTemplate模板

org.springframework.orm.hibernate3.HibernateTemplate ht=new org.springframework.orm.hibernate3.HibernateTemplate();
        ht.setSessionFactory(sessionFactory);

 

你可能感兴趣的:(Hibernate)