【Spring】Spring框架如何集成Hibernate框架
下面个整理一下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&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框架的更多相关文章
- JavaWeb_(Spring框架)在Struts+Hibernate框架中引入Spring框架
spring的功能:简单来说就是帮我们new对象,什么时候new对象好,什么时候销毁对象. 在MySQL中添加spring数据库,添加user表,并添加一条用户数据 使用struts + hibern ...
- 在已有spring的基础上集成hibernate
1.导入hibernate的包和spring的包 hibernate3.hibernate-jpa-2.0-api-.必须的包,log4j,log4j配置文件 1.1 导入Spring的依赖包 ...
- Spring和MyBatis的集成
Spring和MyBatis的整合 1. Spring和各个框架的整合 Spring目前是JavaWeb开发中最终的框架,提供一站式服务,可以其他各个框架整合集成 Spring整合方案 1.1. ...
- Hibernate框架第一天
**框架和CRM项目的整体介绍** 1. 什么是CRM * CRM(Customer Relationship Management)客户关系管理,是利用相应的信息技术以及互联网技术来协调企业与顾客间 ...
- 1.Hibernate框架核心组件 (转自冯岩)
Hibernate框架核心组件 在Hibernate框架简述中,演示了一个简单的Hibernate应用,但并没有深入说明其中程序,在这篇中将比较详细的介绍一下Hibernate的核心组件.首先最关键一 ...
- 【Hibernate】hibernate框架的搭建
1, Hibernate 是什么 Hibernate是java应用程序与数据库交互的开发的框架. Hibernate是一个开源,轻量级的ORM(对象关系映射)工具. 2,Hibernate框架的优点 ...
- 深入浅出学习Hibernate框架(二):JDBC基础操作
上篇博客<深入浅出学习Hibernate框架(一):从实例入手初识Hibernate框架>简单介绍了一下Hibernate框架,并且举了一个实例来了解Hibernate.这篇博客将介绍JD ...
- 深入浅出Struts2+Spring+Hibernate框架
一.深入浅出Struts2 什么是Struts2? struts2是一种基于MVC的轻量级的WEB应用框架.有了这个框架我们就可以在这个框架的基础上做起,这样就大大的提高了我们的开发效率和质量,为公司 ...
- Struts2+Spring+Hibernate框架整合总结详细教程
一.SSH三大框架知识总结 Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架.其全新的Struts 2的体系结构与S ...
随机推荐
- GitHub最新命令使用教程
一.创建github仓库并提交代码 1.在github创建public仓库 2.会生成一个git地址 https://github.com/ae6623/Zebra.git 3.在本地打开命令,在文件 ...
- mybaits动态SQL中的DECIMAL
数据库:mysql数据库字段类型:decimal(11,2)java程序类型:java.math.BigDecimal 使用mybatis的动态语句 <if test ="money! ...
- OpenGL ES 3.0之顶点缓冲
所谓顶点缓冲就是直接将顶点数据存储在gpu的一段缓冲区,不需要从cpu拷贝到gpu.提高了程序的运行效率. 操作步骤 1.创建顶点缓冲对象 GLuint vertexBufferID; 2.分配空间 ...
- Web UI 技术发展历程
本文内容 纯文本和静态 HTML 页面 服务器端技术 插件技术--ActiveX.Applet 和 Flash Ajax 异步时代和基于 JavaScript 的 UI 技术 RIA--Adobe F ...
- mysql数据库配置优化(占cpu过高问题)
MySQL5.6的my.ini配置 提供一个MySQL 5.6版本适合在1GB内存VPS上的my.cnf配置文件(点击这里下载文件): [client] port = 3306 socket = /t ...
- Spring+mybatis+postgresql整合
最近做了一个项目,需要使用Spring+mybatis+postgresql,下面记录一下整合步骤: 一.准备JAR包: 我使用的是maven,所以直接晒出pom.xml <project xm ...
- [转]基本Guava工具
转自:http://www.cnblogs.com/renchunxiao/p/3661918.html?utm_source=tuicool 使用Joiner类 将任意字符串通过分隔符进行连接到一起 ...
- 算法笔记_224:夺冠概率模拟(Java)
目录 1 问题描述 2 解决方案 1 问题描述 足球比赛具有一定程度的偶然性,弱队也有战胜强队的可能. 假设有甲.乙.丙.丁四个球队.根据他们过去比赛的成绩,得出每个队与另一个队对阵时取胜的概率表 ...
- java.lang.AbstractStringBuilder.enlargeBuffer
04-18 10:22:08.585 3653-3653/com.tongyan.lanzhou.rv I/dalvikvm: "main" prio=5 tid=1 RUNNAB ...
- cmake常用编译选项
1.openmp加载,cmake3.9之后内置了openmp find_package(OpenMP) if (OPENMP_FOUND OR OpenMP_CXX_FOUND) set (CMAKE ...