zoukankan      html  css  js  c++  java
  • 初识hibernate框架之一:进行简单的增删改查操作

    Hibernate的优势

    l 优秀的Java 持久化层解决方案  (DAO

    l 主流的对象—关系映射工具产品

    l 简化了JDBC 繁琐的编码

    l 将数据库的连接信息都存放在配置文件

    l 自己的ORM框架

    l 一定要手动实现Hibernate(模拟Hibernate实现)

    一:创建一个java project项目

    如下图

    2:创建一个大配置文件

    <?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.OracleDriver</property><!-- 数据库驱动语言 -->
            <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property><!-- 数据库连接字符串 -->
            <property name="connection.username">Y2162</property><!-- 用户名 -->
            <property name="connection.password">1</property><!-- 密码 -->
            <!-- SQL dialect (sql的方言)-->
            <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property><!-- 通过oracle数据库的版本规定sql方言 -->
            <!-- Echo all executed SQL to stdout 在控制台打印后台的sql语句-->
            <property name="show_sql">true</property><!-- 可省,展示生成的sql语句 -->
            <!-- 格式化显示sql -->
            <property name="format_sql">true</property><!-- 可省,将展示出的sql语句格式化-->
            <!-- Drop and re-create the database schema on startup 序列化-->
            <property name="hbm2ddl.auto">update</property><!-- 数据库的建表语句首选create,之后选择update -->
            
            <mapping resource="Student.hbm.xml" /><!-- 关联小配置文件 -->
    
        </session-factory>
    
    </hibernate-configuration>

    继而创建一个小配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="cn.happy.entity"><!-- 关联哪个包下的类 -->
        <class name="Student" table="STUDENT"><!-- 符合一类对应一表 -->
            <id name="sid" column="SID"><!-- 生成主键(唯一标识) -->
                <!-- 主键生成策略:native: native:如果后台是Oracle 后台是MySQL,自动应用自增 -->
                <generator class="increment" />
            </id>
            <property name="name" type="string" column="NAME" />
            <property name="age"  type="int" column="AGE"/>
        </class>
    </hibernate-mapping>

    编写程序的时候,以面向对象的方式处理数据

    保存数据的时候,却以关系型数据库的方式存储

    三:Test类中的测试数据

    package cn.happy.Test;
    
    
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    import org.junit.After;
    import org.junit.Before;
    
    import cn.happy.entity.Student;
    import cn.happy.until.Hibernate_until;
    
    public class Test {
    
        public static void main(String[] args) {
            //addAll();
            //delete();
            find();
        }
        
        
        public static void addAll()
        {
            Student stu=new Student();
            stu.setSid(1);
            stu.setAge(18);
            stu.setName("我是狗");
            
            //获取session对象
            Session session =  Hibernate_until.getSession();
            //开启事务
            Transaction tran = session.beginTransaction();
            //hibernate保存
            session.save(stu);
            System.out.println("成功保存!");
            tran.commit();
            Hibernate_until.closeSession();
        }
        public static void delete()
        {
            //打开session
            Session session =  Hibernate_until.getSession();
            //开始一个事务
            Transaction tx=session.beginTransaction();
            //获取部门的对象
            Student stu=(Student)session.get(Student.class, new Integer(1));
            //删除对象(持久化操作)
            if(stu!=null)
            {
              session.delete(stu);
            }
            try {
                 //提交事务
                tx.commit();    
                System.out.println("删除成功");
            } catch (Exception e) {
                 //回滚事务
                tx.rollback();
                System.out.println("删除回滚");
            }
            Hibernate_until.closeSession();
        }
        public static void update()
        {
            Session session =  Hibernate_until.getSession();
            //开始一个事务
            Transaction tx=session.beginTransaction();
            //获取部门的对象
            Student stu=(Student)session.get(Student.class, new Integer(1));
            //删除对象(持久化操作)
            if(stu!=null)
            {
              stu.setName("武松");
              session.update(stu);
            }
            try {
                 //提交事务
                tx.commit();    
                System.out.println("删除成功");
            } catch (Exception e) {
                 //回滚事务
                tx.rollback();
                System.out.println("删除回滚");
            }
            Hibernate_until.closeSession();
        }
        public static void find()
        {
                Session session =  Hibernate_until.getSession();
                //获取部门的对象
                Student stu=(Student)session.get(Student.class, new Integer(2));
                System.out.println(stu);
        }
        
        
        
    
    }

    通过以上代码,我们就能轻松的使用框架向db端操作数据了

  • 相关阅读:
    预习作业一
    20145234黄斐《java程序设计基础》第一周
    预习作业3
    开源 人脸识别 openface 实用介绍 实例演示 训练自己的模型
    ubuntu 开机 输入密码 无法进入
    linux anaconda 管理 python 包
    如何从零开始做一个蓝牙机械键盘
    keil中结构体跨文件调用
    GPRS 通信
    如何查找EI 及SCI 索引
  • 原文地址:https://www.cnblogs.com/chimingyang/p/5815179.html
Copyright © 2011-2022 走看看