Hibernate -- 连接oracle 数据库小型案例

首先 ,编写实体类 。

package main.cn.happy.entity;

/**
 * Created by lenovo on 2017/9/16.
 */
public class Dog {
    private  Integer id;
    private  String name;
    private  Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

   此处 ,我创建了一个Dog 实体类 。


给大家看一下 结构图 : 




接下来  ,编写大配置文件 (hibernate.cfg.xml)


<?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 connection settings -->
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

        <property name="connection.url">jdbc:oracle:thin://localhost:1521/orcl
            <![CDATA[?useUnicode=true&characterEncoding=utf82]]></property>
        <property name="connection.username">scott</property>
        <property name="connection.password">orcl</property>

        <!-- JDBC connection pool (use the built-in) -->
        <!-- <property name="connection.pool_size">1</property> -->

        <!-- SQL dialect -    Sql  方言 -->
        <!--    <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>-->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <!-- 和   线程绑定  session-->
        <!-- Enable Hibernate's automatic session context management -->
        <!-- <property name="current_session_context_class">thread</property> -->

        <!--二级缓存是否开启-->
        <!--  Disable the second-level cache -->
        <!--     <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>-->

        <!-- Echo all executed SQL to stdout  是否在控制台打印Sql  -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <!-- Drop and re-create the database schema on startup      -->
         <property name="hbm2ddl.auto">update</property>

        <!--小配置路径-->
        <mapping resource="main/cn/happy/entity/Dog.hibernate.xml"></mapping>
    </session-factory>

</hibernate-configuration>
最后  , 我们来进行最后的测试步骤 :


package cn.happy.test;


import main.cn.happy.entity.Dog;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by lenovo on 2017/9/17.
 */
public class FirstTest {
    public static void main(String[] args) {
        //创建配置 对象
        Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
        SessionFactory sessionFactory = cfg.buildSessionFactory();
        //创建sessions
        Session session = sessionFactory.openSession();
        //创建事务  开启
        Transaction transaction = session.beginTransaction();

        //01   添加
        Dog dog= new Dog();

        dog.setName("郭华");
        dog.setAge(15);
        session.save(dog);


        //提交事务
        transaction.commit();
        //关闭session
        session.close();


    }
    /*
    *  修改记录
    * */
    @Test
    public  void  update()
    {
        Configuration cfg = new Configuration().configure();
    SessionFactory sessionFactory=cfg.buildSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction transaction = session.beginTransaction();
    //事务

        Dog dog = new Dog();
        dog.setId(2);
        dog.setAge(20);
        dog.setName("小黄");
        session.update(dog);
        transaction.commit();
        session.close();
        System.out.println(" 修改成功 !!");
    }
//删除
    @Test
    public  void delete(){
        Configuration cfg= new Configuration().configure();
        SessionFactory sessionFactory = cfg.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        Dog dog= new Dog();;
        dog.setId(21);
        session.delete(dog);
        tx.commit();
        session.close();
        System.out.println("删除 OK ! ");


    }




}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值