【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; } }
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>
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>
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
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); } }
到这里就配置到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); }