Spring3.1 整合hibernate4 简单设置
2016-09-20 21:20
429 查看
首先
hibernate.cfg.xml
怎么样?简单吧
然后
applicationContext.xml
就是这么基础!
下面代码测试:
dao:
dao实现类
junit测试 马上上结果:
输出:
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]
相关文章推荐
- Eclipse + MyEclipse整合Struts+Spring+Hibernate简单例子开发
- struts spring hibernate整合简单实现
- Spring整合Hibernate简单示例
- Spring整合Hibernate3后,hbm2ddl设置update无法自动建表的解决方法
- Struts1.2 Spring2.0 Hibernate3.1 MyEclipse整合可能的包冲突
- 最简单的 Struts2 Spring2 Hibernate3 整合 实例
- spring与hibernate整合-使用properties文件分离数据库连接设置
- spring3.1+hibernate3.6.5整合xml
- 整合开发Struts2,Hibernate,Spring简单框架的搭建详解
- hibernate4整合spring3.1出现nested exception is java.lang.NoClassDefFoundError: org/hibernate/engine/Sess
- Spring+Hibernate:使用注释和OSCache整合的简单实例
- Spring 3.0.5 + CXF 2.4 + Hibernate 3.6.0 + MySQL 5.5 整合(CXF/Spring/Hibernate)实现简单的WSS4J认证 + 测试
- 一个简单Spring+Hibernate+Struts2.0+Ajax整合获取客户端IP、URL和请求时间
- Spring整合Hibernate简单示例
- struts spring hibernate整合简单实现
- Struts2 Spring Hibernate 的简单整合
- MyEclipse整合SSH(Struts+Spring+Hibernate)简单登录范例(一)
- Spring2整合Hibernate3.1问题
- MyEclipse整合SSH(Struts+Spring+Hibernate)简单登录范例(一)
- Struts+Spring+Hibernate整合的简单实例