框架整合____Spring整合hibernate

2 篇文章 0 订阅

备注:本人写博客就是想_让一些技术点和配置  遵循官方标准配置和更容易理解 为基础 

以最精简例子入门,更容易的掌握一些东西.

整合结构图


1.添加spring支持配置spring的applicationContext.xml文件

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xmlns:context="http://www.springframework.org/schema/context"  
  7.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  8.     xmlns:tx="http://www.springframework.org/schema/tx"  
  9.     xmlns:c="http://www.springframework.org/schema/c"  
  10.     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
  11.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd  
  12.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  13.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
  14.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">  
  15.       
  16.       
  17.     <!-- 加载多个资源配置文件   加载多个资源配置文件   加载多个资源配置文件   加载多个资源配置文件   加载多个资源配置文件   加载多个资源配置文件  -->  
  18.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  19.         <property name="locations">  
  20.             <list>  
  21.                 <value>classpath:frame_jdbc.properties</value>  
  22.             </list>  
  23.         </property>  
  24.     </bean>    
  25.     <!-- 使用注解的方式装配置bean -->  
  26.     <context:annotation-config />  
  27.     <context:component-scan base-package="com.frame"></context:component-scan>  
  28.     <!-- 通过注解,把URL映射到Controller上,该标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->  
  29.     <mvc:annotation-driven />  
  30.       
  31.     <!-- 配置dbcp数据源  配置dbcp数据源  配置dbcp数据源  配置dbcp数据源  配置dbcp数据源  配置dbcp数据源  配置dbcp数据源 -->  
  32.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >  
  33.         <property name="driverClassName" value="${jdbc.driverClassName}" />  
  34.         <property name="url" value="${jdbc.url}" />  
  35.         <property name="username" value="${jdbc.username}" />  
  36.         <property name="password" value="${jdbc.password}" />  
  37.     </bean>  
  38.       
  39.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
  40.         <property name="dataSource" ref="dataSource" />  
  41.         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>  
  42.     </bean>  
  43.       
  44.     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
  45.         <property name="sessionFactory" ref="sessionFactory" />  
  46.     </bean>  
  47.     <tx:annotation-driven transaction-manager="transactionManager" />  
  48.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  49.         <tx:attributes>  
  50.             <tx:method name="get*" read-only="true" propagation="REQUIRED" />  
  51.             <tx:method name="add*" propagation="REQUIRED" />  
  52.             <tx:method name="update*" propagation="REQUIRED" />  
  53.             <tx:method name="save*" propagation="REQUIRED" />  
  54.             <tx:method name="*" propagation="REQUIRED" />  
  55.         </tx:attributes>  
  56.     </tx:advice>  
  57.       
  58.       
  59. </beans>  
2.添加hibernate支持配置hibernate文件

[html]  view plain  copy
  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.           "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
  5. <!-- Generated by MyEclipse Hibernate Tools.                   -->  
  6. <hibernate-configuration>  
  7.   
  8.     <session-factory>  
  9.   
  10.         <property name="format_sql">true</property>  
  11.         <property name="show_sql">true</property>  
  12.   
  13.         <mapping resource="com/frame/student/bean/Student.hbm.xml" />  
  14.     </session-factory>  
  15.   
  16. </hibernate-configuration>  

//配置jdbc

[html]  view plain  copy
  1. ################Oracle_JDBC################  
  2. jdbc.driverClassName=oracle.jdbc.driver.OracleDriver  
  3. jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl  
  4. jdbc.username=oracle  
  5. jdbc.password=123456  

//配置basedao

[java]  view plain  copy
  1. package com.frame.base.dao;  
  2.   
  3. import com.frame.student.bean.Student;  
  4.   
  5. public interface BaseDao {  
  6.       
  7.     public void save(Student transientInstance);  
  8.       
  9. }  
//配置basedaoimpl

[java]  view plain  copy
  1. package com.frame.base.dao;  
  2.   
  3.   
  4. import javax.annotation.Resource;  
  5.   
  6. import org.hibernate.Session;  
  7. import org.hibernate.SessionFactory;  
  8. import org.slf4j.Logger;  
  9. import org.slf4j.LoggerFactory;  
  10. import org.springframework.transaction.annotation.Transactional;  
  11.   
  12. import com.frame.student.bean.Student;  
  13.   
  14. @Transactional  
  15. public class BaseDaoImpl implements BaseDao{  
  16.       
  17.     private static final Logger log = LoggerFactory.getLogger(BaseDaoImpl.class);  
  18.       
  19.     @Resource  
  20.     private SessionFactory sessionFactory;  
  21.   
  22.     private Session getCurrentSession() {  
  23.         return sessionFactory.getCurrentSession();  
  24.     }  
  25.   
  26.     @Override  
  27.     public void save(Student transientInstance) {  
  28.         log.debug("saving Student instance");  
  29.         try {  
  30.             getCurrentSession().save(transientInstance);  
  31.             log.debug("save successful");  
  32.         } catch (RuntimeException re) {  
  33.             re.printStackTrace();  
  34.         }  
  35.     }  
  36.   
  37. }  
//student实体类和映射文件

[java]  view plain  copy
  1. package com.frame.student.bean;  
  2.   
  3. /** 
  4.  * Student entity. @author MyEclipse Persistence Tools 
  5.  */  
  6.   
  7. public class Student implements java.io.Serializable {  
  8.   
  9.     // Fields  
  10.   
  11.     private String stuid;  
  12.     private String stuname;  
  13.     private String stutime;  
  14.   
  15.     // Constructors  
  16.   
  17.     /** default constructor */  
  18.     public Student() {  
  19.     }  
  20.   
  21.     /** minimal constructor */  
  22.     public Student(String stuid) {  
  23.         this.stuid = stuid;  
  24.     }  
  25.   
  26.     /** full constructor */  
  27.     public Student(String stuid, String stuname, String stutime) {  
  28.         this.stuid = stuid;  
  29.         this.stuname = stuname;  
  30.         this.stutime = stutime;  
  31.     }  
  32.   
  33.     // Property accessors  
  34.   
  35.     public String getStuid() {  
  36.         return this.stuid;  
  37.     }  
  38.   
  39.     public void setStuid(String stuid) {  
  40.         this.stuid = stuid;  
  41.     }  
  42.   
  43.     public String getStuname() {  
  44.         return this.stuname;  
  45.     }  
  46.   
  47.     public void setStuname(String stuname) {  
  48.         this.stuname = stuname;  
  49.     }  
  50.   
  51.     public String getStutime() {  
  52.         return this.stutime;  
  53.     }  
  54.   
  55.     public void setStutime(String stutime) {  
  56.         this.stutime = stutime;  
  57.     }  
  58.   
  59. }  
[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
  4. <!--  
  5.     Mapping file autogenerated by MyEclipse Persistence Tools 
  6. -->  
  7. <hibernate-mapping>  
  8.     <class name="com.frame.student.bean.Student" table="STUDENT" schema="ORACLE">  
  9.         <id name="stuid" type="java.lang.String">  
  10.             <column name="STUID" length="32" />  
  11.             <generator class="assigned" />  
  12.         </id>  
  13.         <property name="stuname" type="java.lang.String">  
  14.             <column name="STUNAME" length="32" />  
  15.         </property>  
  16.         <property name="stutime" type="java.lang.String">  
  17.             <column name="STUTIME" length="32" />  
  18.         </property>  
  19.     </class>  
  20. </hibernate-mapping>  
//studentdao和impl

[java]  view plain  copy
  1. package com.frame.student.dao;  
  2.   
  3. import com.frame.base.dao.BaseDao;  
  4.   
  5. public interface StudentDao extends BaseDao{  
  6.   
  7. }  
[java]  view plain  copy
  1. package com.frame.student.dao;  
  2.   
  3. import org.springframework.stereotype.Repository;  
  4. import org.springframework.transaction.annotation.Transactional;  
  5.   
  6. import com.frame.base.dao.BaseDaoImpl;  
  7.   
  8. @Repository  
  9. public class StudentDaoImpl extends BaseDaoImpl implements StudentDao{  
  10.   
  11. }  

//测试类

[java]  view plain  copy
  1. package com.frame.student.test;  
  2.   
  3.   
  4. import org.junit.Before;  
  5. import org.junit.runner.RunWith;  
  6. import org.springframework.beans.factory.annotation.Autowired;  
  7. import org.springframework.test.context.ContextConfiguration;  
  8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  9.   
  10. import com.frame.student.bean.Student;  
  11. import com.frame.student.dao.StudentDao;  
  12.   
  13. @RunWith(SpringJUnit4ClassRunner.class)    
  14. @ContextConfiguration(locations={"classpath:applicationContext.xml"})    
  15. public class Test {  
  16.   
  17.     @Autowired    
  18.     StudentDao dao;  
  19.       
  20.     Student student;     
  21.       
  22.     @Before  
  23.     public void setUp() throws Exception {  
  24.          //做一些初始化操作 比如创建对象等    
  25.         student=new Student();    
  26.             student.setStuid("99");    
  27.             student.setStuname("zhagnsan");    
  28.             student.setStutime("2017-1-31");  
  29.     }  
  30.   
  31.     @org.junit.Test  
  32.     public void insertStudent() {  
  33.         try {  
  34.             dao.save(student);  
  35.         } catch (Exception e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.     }  
  39. }  







//源代码

http://pan.baidu.com/s/1hsvKafa

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、下4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、下4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值