【j2ee spring】26、巴巴运动网-整合hibernate4(1)

巴巴运动网-整合hibernate4(1)

1、项目图解

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

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

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、我们配置一下hibernate的连接文件

<?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>

        <!--  Database connectionsettings   -->

        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

        <property name="connection.url">jdbc:oracle:thin:@localhost:1522:ORCL</property>

        <property name="connection.username">这里填账号</property>

        <property name="connection.password">你的密码</property>

        <!--   JDBC connection pool (usethe built-in)  -->

        <property name="connection.pool_size">1</property>

        <!--  SQL dialect   -->

        <property name="dialect">org.hibernate.dialect.OracleDialect</property>

        <!--  Enable Hibernate'sautomatic session context management   -->

        <property name="current_session_context_class">thread</property>

        <!--  Disable the second-levelcache    -->

        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!--  Echo all executed SQL to stdout   -->

        <property name="show_sql">true</property>

        <!--  Drop and re-create thedatabase schema on startup   -->

        <property name="hbm2ddl.auto">update</property>

        <mapping resource="com/cutter_point/bean/product/Product.hbm.xml"/> <!--这个是你刚刚陪的java类的路径对象 -->

   </session-factory>

</hibernate-configuration>

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

/**

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

 * 文件:ProductTest.java

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

 * 作者:cutter_point

 */

package junit.test;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

import org.junit.BeforeClass;

import org.junit.Test;

import com.cutter_point.bean.product.ProductType;

publicclassProductTest

{

    @BeforeClass

    publicstaticvoid setUpBeforeClass() throws Exception

    {

    }

    @SuppressWarnings("deprecation")

    @Test

    publicvoid test()

    {

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

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

        Configurationcfg = newConfiguration();    //得到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();

    }

}

6、总结

这个配置hibernate还是很简单的,但是这里注意一点,如果你的数据库里面没有建立相应的表的话,又或者你sql语句不太会写,又或者你觉得自己写的sql语句不准,那么这里还有一个方法,吧hibernate配置文件hibernate.cfg.xml里面的

<property name="hbm2ddl.auto">update</property>
中的update改为create,那么hibernate就会自动帮你创建相应的表

时间: 2024-08-08 16:59:12

【j2ee spring】26、巴巴运动网-整合hibernate4(1)的相关文章

【j2ee spring】28、巴巴运动网-整合hibernate4+spring4(3)使用注解

巴巴运动网-整合hibernate4+spring4(3)使用注解 1.项目图解 2.首先我们引入相应的jar包 使用注解的好处不言而喻,我们就不用再数据库中再建表,可以依赖jpa或者hibernate帮我们建表了 3.我们配置一下数据库中相应的实体对象 ProductType.java /** * 功能:这是产品类别的 * 文件:ProductType.java * 时间:2015年5月12日10:16:21 * 作者:cutter_point */ package com.cutter_po

【j2ee spring】29、巴巴运动网-整合hibernate4+spring4(4)DAO层

巴巴运动网-整合hibernate4+spring4(3)DAO层 1.项目图解 2.首先我们引入相应的jar包 3.我们配置一下数据库中相应的实体对象 ProductType.java /** * 功能:这是产品类别的 * 文件:ProductType.java * 时间:2015年5月12日10:16:21 * 作者:cutter_point */ package com.cutter_point.bean.product; import javax.persistence.Column;

【j2ee spring】30、巴巴运动网-整合hibernate4+spring4(5)分页

巴巴运动网-整合hibernate4+spring4(5)分页 1.项目图解 2.首先我们引入相应的jar包 3.我们配置一下数据库中相应的实体对象 ProductType.java /** * 功能:这是产品类别的 * 文件:ProductType.java * 时间:2015年5月12日10:16:21 * 作者:cutter_point */ package com.cutter_point.bean.product; import java.io.Serializable; import

【j2ee spring】27、巴巴运动网-整合hibernate4+spring4(2)

巴巴运动网-整合hibernate4+spring4(2) 1.项目图解 2.首先我们引入相应的jar包 这里用的是oracle 11g,所以我们使用的数据库连接jar包是ojdbc6, 实际上ojdbc5和6的差别就是支持的数据版本的问题,只要你安装了相应的数据库,对应的版本里面就有相应的数据库jar包,不行百度绝壁有!!! 3.我们配置一下数据库中相应的实体对象 ProductType.java /** * 功能:这是产品类别的 * 文件:ProductType.java * 时间:2015

【j2ee spring】31、巴巴运动网整合S2SH

整合hibernate4+spring4+struts2 1.项目图解 2.首先我们引入相应的jar包 3.我们配置对应的web.xml 当我们整合struts2的时候,就要扯到web方面的东西,那么就必须得配置web.xml的东西   <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:sche

【j2ee spring】34、巴巴运动网的产品类别管理

巴巴运动网的产品类别管理 1.项目图解 这次我们做的是产品管理的功能,也就是增删改查... 2.首先我们引入相应的jar包 3.我们开始做我们的相应的功能模块 页面的素材我会上传的,链接是:http://download.csdn.net/detail/cutter_point/8803985 这里我们先把上节的有几个工具类补足上来 PageIndex.java /** * 功能:存放第一个页码和最后一个页码 * 时间:2015年5月19日09:06:26 * 文件:PageIndex.java

【j2ee spring】38、巴巴运动网的产品文件的上传

巴巴运动网的产品文件的上传 1.项目图解 2.我们开始做我们的相应的功能模块 页面的素材我会上传的,链接是:http://download.csdn.net/detail/cutter_point/8803985 上传文件的接口类与实现 /** * 功能:这个是文件业务处理的功能接口 * 时间:2015年5月25日09:29:56 * 文件:UploadFileService.java * 作者:cutter_point */ package com.cutter_point.service.u

【j2ee spring】39、巴巴运动网的产品信息

巴巴运动网的产品信息 1.项目图解 2.我们开始做我们的相应的功能模块 页面的素材我会上传的,链接是:http://download.csdn.net/detail/cutter_point/8803985 产品显示的接口实现 /** * 功能:这个是产品业务的接口 * 文件:ProductInfoService.java * 时间:2015年5月22日16:48:25 * 作者:cutter_point */ package com.cutter_point.service.product;

【j2ee spring】32、巴巴运动网的产品类别页面

巴巴运动网的产品类别页面 1.项目图解 这里面好多页面都是错的,只是个模板,暂时用不上,需要的代码我会全部贴出来,最后做完的时候我会把项目发上来 2.首先我们引入相应的jar包 3.首先我们组装整个页面的整体格局 页面的素材我会上传的,链接是:http://download.csdn.net/detail/cutter_point/8803985 top.jsp <%@ page contentType="text/html;charset=UTF-8"%> <%@