Spring+Hibernate框架整合-连接oracle

在整合步骤的基础上,把本来连接Mysql改为连接oracle
jar: ojdbc14_g.jar
  • hbm

    设置自增长id为sequence
    <?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.how2java.pojo">
        <class name="Category" table="bigsword.category_">
            <id name="id" column="id">
                <generator class="native">        
                    <param name="sequence">bigsword.category_sequence</param>
                </generator>
            </id>
            <property name="name" />
     
        </class>
         
    </hibernate-mapping>
  • applicationContext.xml

    设置连接oracle的 
    驱动 
    url 
    方言 
    账号密码
    <?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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context     
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
      
        <bean name="c" class="com.how2java.pojo.Category">
            <property name="name" value="yyy" />
        </bean>
         
        <bean name="dao" class="com.how2java.dao.CategoryDAO">
            <property name="sessionFactory" ref="sf" />
        </bean>
     
        <bean name="sf"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="ds" />
            <property name="mappingResources">
                <list>
                    <value>com/how2java/pojo/Category.hbm.xml</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <value>
                    hibernate.dialect=org.hibernate.dialect.OracleDialect
                    hibernate.show_sql=true
                    hbm2ddl.auto=update
                </value>
            </property>
        </bean>   
             
        <bean name="ds"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
            <property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" />
            <property name="username" value="bigsword" />
            <property name="password" value="bigsword" />
        </bean>  
     
    </beans>
  • sql 语句

    首先用bigsword账号登录 
    创建sequence 
    创建table
    create sequence bigsword.category_sequence
     
    create table bigsword.category_(
    id number,
    name varchar2(30)
     
    )
  • TestSpring

    运行测试代码
    package com.how2java.test;
      
    import java.util.List;
     
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
     
    import com.how2java.dao.CategoryDAO;
    import com.how2java.pojo.Category;
      
    public class TestSpring {
      
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext(
                    new String[] { "applicationContext.xml" });
            CategoryDAO dao = (CategoryDAO) context.getBean("dao");
            Category c =new Category();
            c.setName("category 1");
            dao.save(c);
             
            List<Category> cs= dao.find("from Category c");
            System.out.println(cs);
        }
    }
    


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值