Oracle 增删改查

Oracle入门案例:

1.创建实体类Student 并重写ToString方法

package cn.happy.entity;

public class Student {

    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;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", id=" + id + "]";
    }
    private String name;
    private Integer age;
    private Integer id;
    

}

2.导入jar包

3.创建大配置关联小配置

<?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>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">scott</property>
<property name="connection.password">0123</property>

<!-- 输出所有 SQL 语句到控制台。 -->
<property name="hibernate.show_sql">true</property>

<!-- 在 log 和 console 中打印出更漂亮的 SQL。 -->
<property name="hibernate.format_sql">true</property>
<!-- 方言 -->
<property name="hibernate.dialect"> org.hibernate.dialect.Oracle10gDialect</property>

<property name="hbm2ddl.auto">update</property>


<mapping resource="cn/happy/entity/Student.hbm.xml"/>


</session-factory>

</hibernate-configuration>

4.编写小配置coding='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="id" type="int" column="id">
         </id>
         <property name="name" type="string" column="name"/>
         <property name="age" type="int" column="age"/>
     </class>    
 </hibernate-mapping>

5.工具类

package cn.happy.entity;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private static Configuration cfg=new Configuration().configure();
    private static SessionFactory factory=cfg.buildSessionFactory();
    
     //01.方法返回session
    public static Session getSession(){
        return factory.openSession();
    }
    
    //02.关闭session
    public static void closeSession(){
        getSession().close();
    }
    

}

6.测试类

    //  1.2 修改学生 
     
    @Test
    public void updateTest(){
         Session session = HibernateUtil.getSession();
         
         //不被上下文跟踪对象 
         /*Student stu=new Student();
         stu.setId(3);
         stu.setName("微冷的雨训练营");*/
         
         //方式二:如何用呗上下文跟踪的方式
         //检索出一条记录,一个实体对象
         Student stu= (Student)session.load(Student.class,3);
         stu.setName("金龙加油!!!!");

         Transaction tx = session.beginTransaction();
         session.update(stu);
         tx.commit();
         HibernateUtil.closeSession();
         System.out.println("update ok!");
    }
    
原文地址:https://www.cnblogs.com/Smile-123/p/5815070.html