ssh之hibernate+spring的整合

<?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:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd

                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


<!--配置实现接口的bean-->
<bean id="employeeImpl" class="com.hsp.imp.EmployeeImpl">
  <property name="sessionFactory" ref="sessionFactory"/>
</bean>


<!--配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
         <property name="url" value="jdbc:mysql://localhost:3306/spring"/>
         <property name="username" value="root"/>
         <property name="password" value="xiongliang"/>
         <!-- 连接池启动时的初始值 -->
         <property name="initialSize" value="30"/>
         <!-- 连接池的最大值 -->
         <property name="maxActive" value="500"/>
         <!-- 连接池中释放连接所剩余的最大数 -->
         <property name="maxIdle" value="2"/>
         <!--当连接池空闲连接达到1时,会预先申请连接。 -->
         <property name="minIdle" value="1"/>
</bean>


<!-- 配置sessionFactory -->
<bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mappingResources">
         <list>
               <value>com/hsp/domain/Employee.hbm.xml</value>
         </list>
        </property>
        <property name="hibernateProperties">
             <value>
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
                hibernate.hbm2ddl.auto=update
                hibernate.show_sql=true
             </value>
        </property>
</bean>

<!-- 配置事务管理器,统一管理sessionFactory的事务。class不要写错了,name是固定的。 -->
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
               <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <!-- 启动事务注解 -->
    <tx:annotation-driven transaction-manager="txManager" />

</beans>


employeeImpl类:

package com.hsp.imp;

import java.util.List;

import org.hibernate.Session;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.transaction.annotation.Transactional;


import com.hsp.domain.Employee;
import com.hsp.inter.EmployeeInter;
//将事务交给spring来管理
@Transactional
public class EmployeeImpl implements EmployeeInter {
    //通过配置注入sessionFactory,也可以使用注解注入sessionFactory
    
    private SessionFactory sessionFactory;
    
    public EmployeeImpl() {
        
    }
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
    //业务逻辑方法
    
    public void addEmploy(Employee employee) {
        Session s=sessionFactory.openSession();
        s.save(employee);
    }
}

测试类App:

public class App {
    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        EmployeeInter employeeInter=(EmployeeInter) ac.getBean("employeeImpl");
        Employee e=new Employee("张三","123",new java.util.Date(),220f);
        employeeInter.addEmploy(e);  //自己开启事务了啊。
    }
}


对应数据库的结果:

自此hibernate+spring 的整合已经完成,下面整合struts2+hibernate+spring.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值