下面个整理一下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. 关于ZedGraph几个难点

    1.引言 由于工作原因,需要对数据进行图表展示,.NET默认的控件库或者DotNetBar的控件库又不能满足需求.去网上找到了这个开源的ZedGraph绘制2D图表的程序集,之所以选择这个ZedGra ...

  2. 百度搜索URL参数

    http://www.baidu.com/s?wd=关键字wd(Keyword):查询的关键词:http://www.baidu.com/s?wd=关键字&cl=3cl(Class):搜索类型 ...

  3. Android 在闹钟开机时,如何解决开机动画没有播完就进入Launcher M

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...

  4. Converter Standardalone 5 versus Converter Boot CD

    The new Converter Standalone 5 lacks the Converter Boot CD. Fortunately you can still use the old ve ...

  5. XAML中特殊符号书写

    XAML中特殊符号书写     表示换行.      表示空格.

  6. 002-Go通过ioutil 读写文件

    1.读取文件内容 package main import( "io/ioutil" "fmt" ) func main(){ b,err := ioutil.R ...

  7. linux2.6.30.4内核移植(3)——yaffs文件系统移植

    内核源码:linux2.6.30.4 交叉编译工具:3.4.5 移植linux内核至:TQ2440 工作基础:http://www.cnblogs.com/nufangrensheng/p/36696 ...

  8. gradle 配置及设置本地仓库

    安装Gradle 从官方网站下载安装包,解压到目录 设置环境变量 GRADLE_HOME=D:\gradle\gradle-3.4.1 PATH=;%GRADLE_HOME%\bin 设置本地仓库目录 ...

  9. SQL Server中order by的使用,我们来填坑

    看似很简单是不是? 单列排序,没有任何问题 select * from tableA where age>1 order by age /*后面可以跟上ASC.DESC,默认是ASC升序排列*/ ...

  10. C++11新特性之 std::forward(完美转发)(转)

    我们也要时刻清醒,有时候右值会转为左值,左值会转为右值. (也许“转换”二字用的不是很准确) 如果我们要避免这种转换呢? 我们需要一种方法能按照参数原来的类型转发到另一个函数中,这才完美,我们称之为完 ...