码迷,mamicode.com
首页 > 编程语言 > 详细

sh_Spring整合Hibernate

时间:2015-06-29 22:16:48      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

    分别介绍了Sping和Hibernate,下面是将它们整合到一块去了。


    一、Hibernate内容


    1.创建PO类。

    

package cn.tgb.domain;

//User实体
public class User {
	private Integer id;
	private String username;
	private String password;	
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}		
}

    2.编写User.hbm.xml    

    

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
   
<hibernate-mapping>
	<class name="cn.tgb.domain.User" table="t_user">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="username"></property>
		<property name="password"></property>
	</class>
</hibernate-mapping>

    3.编写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">
	
<hibernate-configuration>
	<session-factory>
		<!-- #1核心四项 -->
		<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
		<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433; DatabaseName=hibernate02</property>
		<property name="hibernate.connection.username">sa</property>
		<property name="hibernate.connection.password">123456</property>
		
		<!-- #2设置方言 -->
		<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
		
		<!-- #3 sql -->
		<!-- * 输出sql-->
		<property name="hibernate.show_sql">true</property>
		<!-- * 格式化sql -->
		<property name="hibernate.format_sql">true</property>
		<!-- * 是否创建表 -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		
		<!-- #4整合c3p0 -->
		<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
		
		<!-- #5 web项目6.0 取消bean校验 -->
		<property name="javax.persistence.validation.mode">none</property>			
		
		<!-- #6添加映射文件 -->
		<mapping resource="cn/tgb/domain/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration> 

    二、Spring内容


    1.Dao实现类:使用HibernateTemplate对PO类进行操作。

    

package cn.tgb.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;

import cn.tgb.dao.UserDao;
import cn.tgb.domain.User;

public class UserDaoImpl implements UserDao{

	//使用hibernateTemplate对PO类进行操作
	private HibernateTemplate hibernateTemplate;
	//提供set方法
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	@Override
	public void save(User user) {
		this.hibernateTemplate.save(user);
	}

	@Override
	public void update(User user) {
		this.hibernateTemplate.update(user);
	}

	@Override
	public void delete(User user) {
		this.hibernateTemplate.delete(user);
	}

	@Override
	public User findById(Integer uid) {
		return this.hibernateTemplate.get(User.class, uid);
	}

	@Override
	public List<User> findAllUser() {
		return this.hibernateTemplate.find("from User");
	}
	
}

    2.Service实现类:直接使用dao层,通过spring tx管理事务


package cn.tgb.service.impl;

import java.util.List;

import cn.tgb.dao.UserDao;
import cn.tgb.domain.User;
import cn.tgb.service.UserService;

public class UserServiceImpl implements UserService{
	//引入UserDao
	private UserDao userDao;
	//提供set方法
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public void addUser(User user) {
		this.userDao.save(user);
	}

	@Override
	public void updateUser(User user) {
		this.userDao.update(user);
	}

	@Override
	public void deleteUser(User user) {
		this.userDao.delete(user);
	}

	@Override
	public User findUserById(Integer uid) {
		return this.userDao.findById(uid);
	}

	@Override
	public List<User> findAllUser() {
		return this.userDao.findAllUser();
	}

}


    3.配置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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.xsd">
	
     <!-- #1配置sessionFactory,
     	通过LocalSessionFactoryBean加载配置获得SessionFactory 
     -->  
     
     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
     	<!-- #1.1确定配置文件的位置 -->
     	<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
     </bean>
     <!-- #2 hibernateTemplate -->  
     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
     	<!-- 需要使用sessionFactory获得session,操作PO对象 -->
     	<property name="sessionFactory" ref="sessionFactory"></property>
     </bean>	  	
     <!-- #3 dao -->  	
     <bean id="userDao" class="cn.tgb.dao.impl.UserDaoImpl">
     	<property name="hibernateTemplate" ref="hibernateTemplate"></property>
     </bean>
     <!-- #4 service -->  	
     <bean id="userService" class="cn.tgb.service.impl.UserServiceImpl">
     	<property name="userDao" ref="userDao"></property>
     </bean>
     
     <!-- #5 事务管理 -->
     <!-- #5.1事务管理器 -->
     <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
     	<!-- 必须确定sessionFactory。从而获得session,再获得事务 -->
     	<property name="sessionFactory" ref="sessionFactory"></property>
	 </bean>
	 
	 <!-- #5.2事务通知(增强),对指定目标方法进行增强 -->
	 <tx:advice id="txAdvice" transaction-manager="txManager">
	 	<tx:attributes>
	 		<tx:method name="add*"/>
	 		<tx:method name="update*"/>
	 		<tx:method name="delete*"/>
	 		<tx:method name="find*" read-only="true"/>
	 	</tx:attributes>	 
	 </tx:advice>	
	 
	 <!-- #5.3使用aop生成代理,进行增强 -->
	 <aop:config>
	 	<!-- 确定切入点 -->
	 	<aop:pointcut expression="execution(* cn.tgb.service..*.*(..))" id="txPointCut"/>
	 	<!-- 声明切面 -->
	 	<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
	 </aop:config>				  
</beans>

    至此,Spring和Hibernate就算整合好了,下面编写一个测试类测试下。


package cn.tgb.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.tgb.domain.User;
import cn.tgb.service.UserService;

public class TestApp {
	public static void main(String[] args) {
		
		//1.创建建用户
		User user = new User();
		user.setUsername("jiangxiao");
		user.setPassword("123");
		
		//2.获取Sping配置文件
		String xmlPathString="applicationContext.xml";
		ApplicationContext applicationContext= new ClassPathXmlApplicationContext(xmlPathString);
		
		//3.通过Ioc获得UserService
		UserService userService = (UserService) applicationContext.getBean("userService");
		
		//4.执行添加用户操作
		userService.addUser(user);
	}
}

   

    分析总结:

    技术分享


    这张思维导图大概介绍了一下配置文件的配置内容和加载过程,其中蓝色的云朵部分是Spring整合Hibernate的内容。程序运行时,通过加载配置文件,便将程序中所有的配置信息都加载了。如果有个扩展修改什么的也方便。日后会继续介绍它们两个与Struts的整合。


版权声明:本文为博主原创文章,未经博主允许不得转载。

sh_Spring整合Hibernate

标签:

原文地址:http://blog.csdn.net/zjx86320/article/details/46686605

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!