巴巴荆楚网-整合hibernate4+spring4(2)

1、图文项目

2、首先我们引入对应的jar包

这里用的是oracle 11g,所以我们使用的数据库连接jar包是ojdbc6,

实际上ojdbc5和6的区别就是支持的数据版本号的问题,仅仅要你安装了对应的数据库,对应的版本号里面就有对应的数据库jar包。不行百度绝壁有!

3、我们配置一下数据库中对应的实体对象

ProductType.java

/**

 * 功能:这是产品类别的

 * 文件:ProductType.java

 * 时间:2015年5月12日10:16:21

 * 作者:cutter_point

 */

package com.cutter_point.bean.product;

publicclassProductType

{

    private Integer typeid;

    public Integer getTypeid()

    {

        returntypeid;

    }

    publicvoid setTypeid(Integertypeid)

    {

        this.typeid = typeid;

    }

}

Product.hbm.xml

<?

xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.cutter_point.bean.product">

    <class name="ProductType" table="ProductType">

        <id name="typeid"column="typeid">

            <generator class="sequence" />

        </id>

    </class>

</hibernate-mapping>

4、我们配置一下spring的配置文件beans.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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.1.xsd

       http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsd

       http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.1.xsd

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

    <!-- 扫描带有spring特殊机制的类,这是把这些包以下全部的类都加入到spring中进行管理 -->

    <context:component-scan base-package="com.cutter_point" />

    <!-- 属性遍历器 -->

    <!-- <context:property-placeholderlocation="classpath:jdbc.properties" /> -->

    <!-- 连接数据库属性配置,destroy-method="close"就是说在这个bean被摧毁的情况下能够调用这个bean默认的close方法-->

    <bean id="myDataSource" class="org.apache.commons.dbcp2.BasicDataSource"destroy-method="close">

        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>

        <property name="url"value="jdbc:oracle:thin:@localhost:1522:orcl"/>

        <property name="username"value="账号"/>

        <property name="password"value="密码"/>

        <!-- 连接池启动时的初始值 -->

        <property name="initialSize" value="1"/>

        <!-- 连接池的最大值  dbcp2里面似乎没有-->

        <!-- <property name="maxActive"value="500"/> -->

        <!-- 最大空暇值.当经过一个高峰时间后。连接池能够慢慢将已经用不到的连接慢慢释放一部分,一直降低到maxIdle为止 -->

        <property name="maxIdle" value="2"/>

        <!--  最小空暇值.当空暇的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->

        <property name="minIdle" value="1"/>

    </bean>

    <!-- hibernate二级缓存的配置 -->

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    <!-- configuration elided for brevity -->

        <property name="dataSource" ref="myDataSource" />

        <property name="mappingResources">

            <list>  <!-- 映射文件 -->

                <value>com/cutter_point/bean/product/Product.hbm.xml</value>

            </list>

        </property>

        <property name="hibernateProperties">   <!-- 用来配置hibernate的属性配置 -->

            <value>

                org.hibernate.dialect.OracleDialect

                hibernate.hbm2ddl.auto=update  <!--其它取值 create、create-drop、update、validate none-->

                hibernate.show_sql=true

                hibernate.format_sql=true

                <!-- 开启二级缓存功能 -->

                hibernate.cache.use_second_level_cache= true

                hibernate.cache.use_query_cache= false

                hibernate.cache.region.factory_class= org.hibernate.cache.ehcache.EhCacheRegionFactory

                <!-- hibernate3的二级缓存配置 --> 

                <!-- <propertyname="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>--> 

            </value>

        </property>

    </bean>

    <!-- 事务管理器,吧上面配置的bean注入到这个里面 -->

    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">

        <property name="sessionFactory" ref="sessionFactory" />

    </bean>

    <!-- 我们採用注解的方式来使用这个事务,首先我们开启事务 -->

    <tx:annotation-driven transaction-manager="transactionManager" />

</beans>

5、接下来我们測试一下hibernate+spring是否配置成功

/**

 * 功能:这是产品类别的单元測试

 * 文件:ProductTest.java

 * 时间:2015年5月12日10:27:24

 * 作者:cutter_point

 */

package junit.test;

import javax.sql.DataSource;

import org.hibernate.HibernateException;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

import org.junit.BeforeClass;

import org.junit.Test;

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

importcom.cutter_point.bean.product.ProductType;

public class ProductTest

{

    @BeforeClass

    publicstatic void setUpBeforeClass() throws Exception

    {

    }

    @Test

    publicvoid test()

    {

        ProductTypept = new ProductType(); //new一个对象

        pt.setTypeid(78);   //设置id号码

        Configurationcfg = new Configuration();    //得到Configuration

       SessionFactorysf =cfg.configure("/config/hibernate/hibernate.cfg.xml").buildSessionFactory();    //取得session工厂

        Sessionsession = sf.openSession();

        session.beginTransaction();

        session.save(pt);

        session.getTransaction().commit();

        session.close();

        sf.close();

    }

    @Test

    publicvoid testSpring()

    {

        //測试spring能否够运作

        ApplicationContextcxt = new ClassPathXmlApplicationContext("config/spring/beans.xml");

        DataSourcedatasource = (DataSource)cxt.getBean("myDataSource");    //取出一个对象

        System.out.println(datasource); //推断是不是为空,

    }

}

6、总结

这个spring里面配置了数据源是用的org.apache.commons.dbcp2.BasicDataSource

这个jar包是,当然你们能够用其它的,我这里仅仅是一个举例,假设你报一个神奇version5.1的错误的话,请换一个数据源jar包,比方commons-dbcp.jar

这个里面就把org.apache.commons.dbcp2.BasicDataSource改为org.apache.commons.dbcp.BasicDataSource

这个事实上就是版本号问题。其它的地方应该是不用改了!

。。!

【j2ee spring】27、巴巴荆楚网-整合hibernate4+spring4(2)的更多相关文章

  1. 【j2ee spring】30、巴巴荆楚网-综合hibernate4+spring4(5)分页

    巴巴荆楚网-综合hibernate4+spring4(5)分页 1.图文项目 2.首先我们引入对应的jar包 3.我们配置一下数据库中对应的实体对象 ProductType.java /** * 功能 ...

  2. (六)Spring4 整合Hibernate4,Struts2

    第一节:S2SH 整合所需Jar 包 Struts2.3.16,Spring4.0.6,Hibernate4.3.5 整合所需jar 包: Struts2.3.16 jar 包 Spring4.0.6 ...

  3. 【j2ee spring】44、巴巴运动网前台产品显示

    [j2ee spring]44.巴巴运动网前台产品显示 项目结构 项目代码 界面显示 <%@ page language="java" isELIgnored="f ...

  4. spring MVC框架入门(外加SSM整合)

    spring MVC框架 一.什么是sping MVC Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 W ...

  5. 解决Spring Boot(2.1.3.RELEASE)整合spring-data-elasticsearch3.1.5.RELEASE报NoNodeAvailableException[None of the configured nodes are available

    Spring Boot(2.1.3.RELEASE)整合spring-data-elasticsearch3.1.5.RELEASE报NoNodeAvailableException[None of ...

  6. Spring整合hibernate4:事务管理

    Spring整合hibernate4:事务管理 Spring和Hibernate整合后,通过Hibernate API进行数据库操作时发现每次都要opensession,close,beginTran ...

  7. (转)SpringMVC学习(四)——Spring、MyBatis和SpringMVC的整合

    http://blog.csdn.net/yerenyuan_pku/article/details/72231763 之前我整合了Spring和MyBatis这两个框架,不会的可以看我的文章MyBa ...

  8. Spring Boot 2.0 快速集成整合消息中间件 Kafka

    欢迎关注个人微信公众号: 小哈学Java, 每日推送 Java 领域干货文章,关注即免费无套路附送 100G 海量学习.面试资源哟!! 个人网站: https://www.exception.site ...

  9. Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合(注解及源码)

    Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合(注解及源码) 备注: 之前在Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合中 ...

随机推荐

  1. c++ virturn function -- 虚函数

    c++ virturn function -- 虚函数 pure irtual function  -- 纯虚函数   先看例子 #include <iostream> using nam ...

  2. MySQL内存表(MEMORY)说明 | 一个PHP程序员的备忘录

    MySQL内存表(MEMORY)说明 | 一个PHP程序员的备忘录 MySQL内存表(MEMORY)说明

  3. 基于FPGA的红外遥控解码与PC串口通信

    基于FPGA的红外遥控解码与PC串口通信 zouxy09@qq.com http://blog.csdn.net/zouxy09 这是我的<电子设计EDA>的课程设计作业(呵呵,这个月都拿 ...

  4. 王立平--android中的anim(动画)

    简单有用步骤: 1.新建anim目录. 2.在anim下新建xml文件, 3.在xml下编写自己须要动画. 简单样例: 给Imageview加入动画 public class MainActivity ...

  5. AS3开发必须掌握的内容

    1.事件机制 2.显示列表 3.垃圾回收 4.常用方法 5.网络通信 6.位图动画 7.渲染机制 8.API结构 9.沙箱机制 10.资源管理 11.内存管理 12.性能优化 13.资源选择 14.安 ...

  6. 从WinMain开始

    一.抽象渗漏法则 根据Joel的抽象渗漏法则,所有重大的抽象机制在某种程度上都是有漏洞的.Joel举过一个例子: C++字符串类型应该能让你假装字符串是个基本类型,它们尝试“字串很难处理”这个事实抽象 ...

  7. Delphi中WebBrowser自动填表模板

    unit Unit1;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ...

  8. 14.4.3.3 Making the Buffer Pool Scan Resistant

    14.4.3.3 Making the Buffer Pool Scan Resistant 让Buffer Pool 扫描 相比使用一个严格的LRU算法, InnoDB 使用一个技术来最小化数据的总 ...

  9. asp.net(C#)之NPOI&quot;操作Excel

    1.首先到网上下载"NPOI.DLL".引用. 2.新建一个操作类"ExcelHelper.cs": using System.Collections.Gene ...

  10. Static关键字的作用及使用

    1.使用static声明属性 如果希望一个属性被所有对象共同拥有,可以将其声明为static类型. 声明为static类型的属性或方法,此属性或方法也被称为类方法,可以由类名直接调用. class P ...