您的位置:首页 > 编程语言 > Java开发

Spring3.1 整合hibernate4 简单设置

2016-09-20 21:20 429 查看
首先

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">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>

</session-factory>

</hibernate-configuration>


怎么样?简单吧

然后

applicationContext.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:tx="http://www.springframework.org/schema/tx"
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.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd <!--扫描包 -->
<context:component-scan base-package="com.darling.dao"></context:component-scan>

<!--配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/mysql"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
</bean>

<!--配置sessionfactory -->
<bean id="sessionFactory"   class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!--数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!--指定hibernate核心配置文件 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!--指定hibernate映射配置文件 -->
<property name="mappingLocations" value="classpath:com/oracle/vo/*.hbm.xml"></property>
</bean>

<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />

</beans>


就是这么基础!

下面代码测试:

dao:

package com.darling.dao;

import com.oracle.vo.Dept;

public interface DeptDao {

Dept getDept(int deptno);

}
//不装逼 不闹心的做法  简单


dao实现类

package com.darling.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.oracle.vo.Dept;

@Repository
public class DeptDapImp implements DeptDao {

@Autowired
private SessionFactory sessionFactory;

private Session getSession(){
return sessionFactory.openSession();
}

@Override
public Dept getDept(int deptno) {
return (Dept) getSession().get(Dept.cl
b1fc
ass, deptno);
}

}


junit测试 马上上结果:

package com.spring.test;

import static org.junit.Assert.*;

import javax.sql.DataSource;

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

import com.darling.dao.DeptDao;

public class SpringHibernateTest {

private ApplicationContext ctx = null;
private DeptDao dao=null;
{
ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
dao=ctx.getBean(DeptDao.class);
}

@Test
public void testSessionFactory() {
System.out.println(dao.getDept(10));
}

}
//简单好用  ^_^


输出:

Dept [deptno=10, dname=ACCOUNTING, loc=NEW YORK]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  整合