idea Oracle hibernate5 DML操作

简单的Hibernate示例DML操作

项目目录结构:
在这里插入图片描述hibernate.cfg.xml配置文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>

    <!--    连接Oracle数据库的四个串-->
    <property name="connection.url">jdbc:oracle:thin:@localhost:1521:ORCL</property>
    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <property name="connection.username">scott</property>
    <property name="connection.password">tiger</property>

    <!--    显示执行的SQL语句、format格式-->
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <!--    数据库方言-->
    <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>

    <!--    读取映射文件-->
    <mapping resource="org/aptech/hdax/pojo/Depttwo.hbm.xml"/>
    <mapping resource="org/aptech/hdax/pojo/Emptwo.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

Emptwo 实体类
实体类使用的lombok插件简化getter\setter方法,提供getter、setter方法

package org.aptech.hdax.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;

/**
 * 员工实体类
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emptwo implements Serializable {
    private Long empno;
    private String ename;
    private String job;
    private Long mgr;
    private Date hiredate;
    private Long sal;
    private Long comm;
    private Long deptno;

}

Depttwo 实体类

package org.aptech.hdax.pojo;

import lombok.Data;

import java.io.Serializable;

/**
 * 部门实体类
 */
@Data
public class Depttwo implements Serializable {
    private Long deptno;
    private String dname;
    private String loc;

}

Emptwo.hbm.xml 员工映射文件

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

    <class name="org.aptech.hdax.pojo.Emptwo" table="EMPTWO" schema="SCOTT">
        <id name="empno" column="EMPNO"/>
        <property name="ename" column="ENAME"/>
        <property name="job" column="JOB"/>
        <property name="mgr" column="MGR"/>
        <property name="hiredate" column="HIREDATE"/>
        <property name="sal" column="SAL"/>
        <property name="comm" column="COMM"/>
        <property name="deptno" column="DEPTNO"/>
    </class>
</hibernate-mapping>

Depttwo.hbm.xml 部门映射文件

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

    <class name="org.aptech.hdax.pojo.Depttwo" table="DEPTTWO" schema="SCOTT">
        <id name="deptno" column="DEPTNO">
        	<!-- 这里使用Oracle sequence序列 主键自增  -->
            <generator class="sequence">
                <param name="sequence">seq_dept</param>
            </generator>
        </id>
        <property name="dname" column="DNAME"/>
        <property name="loc" column="LOC"/>
    </class>
</hibernate-mapping>

TestDML 测试类

package org.aptech.hdax.test;

import org.aptech.hdax.pojo.Depttwo;
import org.aptech.hdax.pojo.Emptwo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

public class TestDML {
    //查询
    @Test
    public void testqueryby(){
        //1、读取配置文件
        Configuration config = new Configuration().configure();
        //2、构建session工厂
        SessionFactory sessionFactory = config.buildSessionFactory();
        //3、打开session
        Session session = sessionFactory.openSession();
        //4、操作事物
        Transaction tx = session.beginTransaction();

        //根据编号查询单个员工信息
        Emptwo emptwo = session.get(Emptwo.class,7839L);
        System.out.println(emptwo.getEmpno()+"\t"+emptwo.getEname()+"\t"+emptwo.getJob()+"\t"+emptwo.getMgr()+"\t"+emptwo.getHiredate());
        System.out.println(emptwo);

        //5、提交事务
        tx.commit();
        //关闭session
        session.close();
    }

    //新增部门
    @Test
    public void testdeptadd(){
        //1、读取配置文件
        Configuration config = new Configuration().configure();
        //2、构建session工厂
        SessionFactory sessionFactory = config.buildSessionFactory();
        //3、打开session
        Session session = sessionFactory.openSession();
        //4、操作事物
        Transaction tx = session.beginTransaction();

        //根据编号删除单个员工信息
        Depttwo depttwo = new Depttwo();
//        depttwo.setDeptno(40L);
        //部门主键编号使用的是Oracle自增序列
        depttwo.setDname("牛逼事业部");
        depttwo.setLoc("磁县白土");

        session.save(depttwo);

        //5、提交事务
        tx.commit();
        //关闭session
        session.close();
    }

    //新增员工
    @Test
    public void testadd(){
        //1、读取配置文件
        Configuration config = new Configuration().configure();
        //2、构建session工厂
        SessionFactory sessionFactory = config.buildSessionFactory();
        //3、打开session
        Session session = sessionFactory.openSession();
        //4、操作事物
        Transaction tx = session.beginTransaction();

        //根据编号删除单个员工信息
        Emptwo emptwo = new Emptwo();
        emptwo.setEmpno(7900L);
        emptwo.setEname("王天霸");
        emptwo.setJob("保镖");
        emptwo.setMgr(4567L);
        emptwo.setDeptno(20L);

        session.save(emptwo);

        //5、提交事务
        tx.commit();
        //关闭session
        session.close();
    }
    //删除
    @Test
    public void testdeleteby(){
        //1、读取配置文件
        Configuration config = new Configuration().configure();
        //2、构建session工厂
        SessionFactory sessionFactory = config.buildSessionFactory();
        //3、打开session
        Session session = sessionFactory.openSession();
        //4、操作事物
        Transaction tx = session.beginTransaction();

        //根据编号删除单个员工信息
        Emptwo emptwo = new Emptwo();
        emptwo.setEmpno(7900L);
        session.delete(emptwo);

        //5、提交事务
        tx.commit();
        //关闭session
        session.close();
    }

    //更新员工信息
    @Test
    public void testupdateby(){
        //1、读取配置文件
        Configuration config = new Configuration().configure();
        //2、构建session工厂
        SessionFactory sessionFactory = config.buildSessionFactory();
        //3、打开session
        Session session = sessionFactory.openSession();
        //4、操作事物
        Transaction tx = session.beginTransaction();

        //根据编号更新员工信息
        Emptwo emptwo = new Emptwo();
        emptwo.setEmpno(7900L);
        emptwo.setEname("小武");
        emptwo.setJob("小偷");
        emptwo.setMgr(4567L);
        emptwo.setDeptno(30L);
        session.update(emptwo);

        //5、提交事务
        tx.commit();
        //关闭session
        session.close();
    }
}

pom.xml 文件:项目依赖jar

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.aptech.hdax</groupId>
    <artifactId>TestHiberDML</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- hibernate核心-->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.10.Final</version>
        </dependency>
        <!--    Oracle JDBC驱动连接    -->
        <dependency>
            <groupId>oracle</groupId>
            <artifactId>oracle6g</artifactId>
            <version>11.12</version>
        </dependency>
        <!--    junit单元测试    -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <!--        简化getter\setter-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>


</project>
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读