五、spring和Hibernate整合

一、测试spring

1.导入spring的jar包,junit的jar包

2.在src目录下新建application.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation=
    "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    ">
    
    <bean id="date" class="java.util.Date"></bean>
    
    
</beans>

3.新建源文件夹test,其下新建包com.myz.confirm,新建Junit test case

package com.myz.confirm;


import java.util.Date;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
    private static ApplicationContext ac=null;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        //加载配置文件
        ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    @Test
    public void testSpring(){
        Date date = (Date) ac.getBean("date");
        System.out.println(date);
    }
}

4.测试成功,后台打出日期。

 

二、测试Hibernate

1.导入hibernate的jar包,junit的jar包

2.在src目录下新建hibernate.cfg.cml配置文件,配置数据库信息

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="connection.url">
            jdbc:mysql://localhost:3306/mysql
        </property>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="myeclipse.connection.profile">mysql</property>

    </session-factory>

</hibernate-configuration>

3.包com.myz.confirm下新建Junit test case

package com.myz.confirm;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.junit.Test;


public class HibernateTest {
    
    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    
    @Test
    public void testHibernate(){
        Session session = sessionFactory.openSession();
        System.out.println(session);
        session.close();
    }
}

4.测试成功,后台打出session地址信息

 

三、整合测试

1.配置application.xml文件,在期内配置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"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation=
    "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    ">
    
    <bean id="date" class="java.util.Date"></bean>
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    </bean>
    
</beans>

2.包com.myz.confirm下新建Junit test case

package com.myz.confirm;

import org.hibernate.SessionFactory;
import org.hibernate.impl.SessionFactoryImpl;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class SHTest {
    private static ApplicationContext ac=null;
    
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    }
    
    @Test
    public void test(){
        SessionFactoryImpl sessionFactory=(SessionFactoryImpl) ac.getBean("sessionFactory");
        System.out.println(sessionFactory);
    }
}

3.测试成功,后台打出sessionFactory信息

posted @ 2018-02-24 14:59  菜鸡蔡文姬  阅读(177)  评论(0编辑  收藏  举报