Spring+Hibernate系列教材 (六)- 连接oracle

在整合步骤的基础上,把本来连接Mysql改为连接oracle

步骤1:hbm
步骤2:applicationContext.xml
步骤3:sql 语句 
步骤4:TestSpring

步骤 1 : 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>

步骤 2 : 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>

步骤 3 : sql 语句 

首先用bigsword账号登录 
创建sequence 
创建table

create sequence bigsword.category_sequence

create table bigsword.category_(

id number,

name varchar2(30)

)

步骤 4 : 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);

    }

}


更多内容,点击了解: https://how2j.cn/k/spring-hibernate/spring-hibernate-oracle/106.html

原文地址:https://www.cnblogs.com/Lanht/p/12789353.html