Java Hour 56 Spring 和 Hibernate 的集成

时间:2023-03-10 00:40:36
Java Hour 56 Spring 和 Hibernate 的集成

上一章节我们完成了一个简单的Spring 的试验品,这章要让Spring 上战场了,不要慌,步骤都是一样的。

Spring 对 Hibernate 的支持是很多方面的,第一个战场是SessionFactory.

集成前: 静态工厂方法

private static SessionFactory sessionFactory;

    static {
        try {
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        // Alternatively, we could look up in JNDI here
        return sessionFactory;
    }

干掉hibernate.cfg.xml

hibernate 映射有两种方式,对应的bean 就是两种不同的方式。

可以参考官方文档妥妥的映射:

http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html

修改静态工厂方法:

public static SessionFactory getSessionFactory() {
       ApplicationContext ctx = new FileSystemXmlApplicationContext("beans.xml");
       SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");
       return sessionFactory;
   }
<?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.xsd">
    <bean id="weather" class="mike.weather.core.WeatherBusiness"></bean>
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="url">
            <value>jdbc:mysql://localhost/hibernate</value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>root</value>
        </property>
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource">
            <ref local="dataSource" />
        </property>
        <property name="packagesToScan">
            <list>
                <value>mike.weather.model</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
                </prop>
            </props>
        </property>
    </bean>
</beans>

静态工厂方法返回一个ApplicationContext

这里涉及到这个ApplicationContext 的位置问题,一般需要一个静态的反复直接返回这个ApplicationContext 的实例即可。

public static void main(final String[] args) {
        ApplicationContext context = ApplicationContextUtils.getApplicationContext();
        WeatherBusiness weatherBusiness = context.getBean(WeatherBusiness.class);
        weatherBusiness.getWeather();
    }
public static ApplicationContext getApplicationContext() {
        return ctx;
    }

Note 莫名其妙就成功了

程序居然真run 起来了,不可思议。顺利的让楼主我都莫名其妙。