下面个整理一下hibernate和Spring框架的结合。

首先是引入hibernate框架的包、Spring框架的包、数据库驱动包。

User.java文件

package cn.shop.bean;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table; @Entity
@Table(name="user")
public class User { @Id
@Column(name="uid")
private int id; @Column(name="uname")
private String name; @Column(name="upass")
private String password; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

User.java

hibernate.cfg.xml文件

<?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"> <!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration> <session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property> <!-- 加载映射描述信息 -->
<mapping class="cn.shop.bean.User" /> </session-factory>
</hibernate-configuration>

hibernate.cfg.xml

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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <context:component-scan base-package="cn.shop"></context:component-scan> <bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db-config.properties</value>
</list>
</property>
</bean> <bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
<property name="driverClass" value="${db.dirverClass}"></property>
<property name="jdbcUrl" value="${db.url}"></property>
</bean> <!-- 配置hibernatetemplate -->
<bean id="template" class="org.springframework.orm.hibernate4.HibernateTemplate">
<!-- 注入一个SqlSessionFactory对象 -->
<property name="sessionFactory" ref="sessionFactory">
</property>
</bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 指定hibernate.cfg.xml -->
<property name="configLocations" value="classpath:hibernate.cfg.xml">
</property>
<property name="dataSource" ref="c3p0"></property>
</bean> </beans>

applicationContext.xml

db-config.properties文件

db.url=jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf8
db.username=root
db.password=517839
db.dirverClass=com.mysql.jdbc.Driver

db-config.properties

UserDao.java文件

package cn.shop.dao;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository; import cn.shop.bean.User; @Repository("userDao")
public class UserDao { @Resource
private HibernateTemplate template; public List findUserById(String name,String password){
return template.find("from User where name=? and password=?",name,password);
}
}

UserDao.java

到这里就配置到Dao层了,使用如下代码进行测试:

        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userdao= ac.getBean("userDao",UserDao.class);
List<User> find = userdao.findUserById("honny","123456");
for(User user:find){
System.out.println(user);
}

【Spring】Spring框架如何集成Hibernate框架的更多相关文章

  1. JavaWeb_(Spring框架)在Struts+Hibernate框架中引入Spring框架

    spring的功能:简单来说就是帮我们new对象,什么时候new对象好,什么时候销毁对象. 在MySQL中添加spring数据库,添加user表,并添加一条用户数据 使用struts + hibern ...

  2. 在已有spring的基础上集成hibernate

    1.导入hibernate的包和spring的包    hibernate3.hibernate-jpa-2.0-api-.必须的包,log4j,log4j配置文件  1.1 导入Spring的依赖包 ...

  3. Spring和MyBatis的集成

    Spring和MyBatis的整合   1. Spring和各个框架的整合 Spring目前是JavaWeb开发中最终的框架,提供一站式服务,可以其他各个框架整合集成 Spring整合方案 1.1.  ...

  4. Hibernate框架第一天

    **框架和CRM项目的整体介绍** 1. 什么是CRM * CRM(Customer Relationship Management)客户关系管理,是利用相应的信息技术以及互联网技术来协调企业与顾客间 ...

  5. 1.Hibernate框架核心组件 (转自冯岩)

    Hibernate框架核心组件 在Hibernate框架简述中,演示了一个简单的Hibernate应用,但并没有深入说明其中程序,在这篇中将比较详细的介绍一下Hibernate的核心组件.首先最关键一 ...

  6. 【Hibernate】hibernate框架的搭建

    1, Hibernate 是什么 Hibernate是java应用程序与数据库交互的开发的框架. Hibernate是一个开源,轻量级的ORM(对象关系映射)工具. 2,Hibernate框架的优点 ...

  7. 深入浅出学习Hibernate框架(二):JDBC基础操作

    上篇博客<深入浅出学习Hibernate框架(一):从实例入手初识Hibernate框架>简单介绍了一下Hibernate框架,并且举了一个实例来了解Hibernate.这篇博客将介绍JD ...

  8. 深入浅出Struts2+Spring+Hibernate框架

    一.深入浅出Struts2 什么是Struts2? struts2是一种基于MVC的轻量级的WEB应用框架.有了这个框架我们就可以在这个框架的基础上做起,这样就大大的提高了我们的开发效率和质量,为公司 ...

  9. Struts2+Spring+Hibernate框架整合总结详细教程

    一.SSH三大框架知识总结 Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架.其全新的Struts 2的体系结构与S ...

随机推荐

  1. 无法ping通自身

    今日,ping localhost 超时,ping 127.0.0.1 显示一般故障.无法访问自己电脑上的tomcat.... 居然,居然是防火墙打开了,关了就好了.我勒个去!!!!

  2. IT行业简报 2014-2-8

    1.微信在“我的银行卡”页面接入嘀嘀打车,三天内微信打车突破10万单,日均订单为70万,其中微信支付订单超过48万单2.三大运营商手机支付用户仅366.3万,与腾讯单月发展手机支付用户500万户相比, ...

  3. 一家VC支持企业的发展轨迹——了解每次融资后股权和期权的变化,以及股份是如何被稀释的【转载】

    来源:ReachVc 该文是 ReachVC 上一篇文章,是某个投资公司副总裁的独立博客,文章写得不错.如果你是一个不太了解融资的创业者,那么本文将对你很有用.通过武林外传同福客栈的例子,了解每次融资 ...

  4. Android开发——Android M(6.0) 权限解决方案

    Android开发--Android M(6.0) 权限解决方案 自从Android M(6.0)发布以来,权限管理相比以前有了很大的改变,很多程序员发现之前运行的好好的Android应用在Andro ...

  5. 【Android界面实现】使用PagerTabStrip实现有滑动标签的Viewpager

    在ViewPager这样的能够滑动的控件上,总是有非常多的文章能够做.上次的文章,我们实现了一个自己定义的ViewPager的指示器,这篇文章,我们主要是想利用Android自带的控件,实现一个指示器 ...

  6. JavaScript常用设计模式

    单例模式:确保类只能被实例化一次. var obj = {} 2.函数返回值 var func = function () {return {}} var obj = func(); 3.构造函数初始 ...

  7. 微信小程序 - 上拉加载更多组件

    详情用例看demo,点击下载示例:loadmore

  8. 微软BI 之SSRS 系列 - 使用 LookupSet 和 Adjacent Group 等高级技巧在报表中跨 Dataset 分组查询

    SSRS 报表中有一些高级的技巧,平常很少用到,下面我通过这个案例来展现一下如何在实际开发中使用它们,并且如何解决一些实际的需求. 这张报表分别统计了不同的 Product 产品在不同的月份的 Ord ...

  9. adb remount 失败:remount failed: Operation not permitted

    adb remount 失败:remount failed: Operation not permitted     关于ADB的使用,这里再说明下:经常使用命令 adb shell - 登录设备sh ...

  10. C语言操作Redis总结

    #include "hiredis.h" #define NO_QFORKIMPL #pragma comment(lib,"hiredis.lib") #pr ...