Hibernate ORM框架——续第一章:Hibernate的增删改查(第一个hibernate代码的优化)

Posted 梁宝谊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate ORM框架——续第一章:Hibernate的增删改查(第一个hibernate代码的优化)相关的知识,希望对你有一定的参考价值。

一:.项目结构

 

二、代码

1)HibernateUtil

package util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class HibernateUtil {
    private static SessionFactory sf;
    
    static {
        StandardServiceRegistry registry =
                new StandardServiceRegistryBuilder()
                .configure()//实验方法给个参数,看配置文件名能否不是hibernate.cfg.xml
                .build();
        
        sf = new MetadataSources(registry)
                .buildMetadata()
                .buildSessionFactory();
    }
    
    public static SessionFactory getSessionFactory(){
        return sf;
    }
}

 

2)配置文件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>
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
        <property name="connection.username">myuser</property>
        <property name="connection.password">123</property>
        
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="hbm2ddl.auto">create</property>
        
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        
        <mapping resource="entity/StudentMapping.xml"/>
    </session-factory>
</hibernate-configuration>        

 

3.1)实体对象

package entity;

public class Student {
    private String sno;
    private String sname;
    public String getSno() {
        return sno;
    }
    public void setSno(String sno) {
        this.sno = sno;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    
}

 

3.2)实体映射文件

<?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="entity">
    <class name="Student" table="Students">
        <id name="sno" column="id">
            <generator class="assigned"></generator>
        </id>
        <property name="sname"></property>
    </class>    
</hibernate-mapping>
        

 

4)dao方法

package dao;



import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import entity.Student;
import util.HibernateUtil;

public class StudentDao {

    private SessionFactory sf;

    public StudentDao() {
        sf = HibernateUtil.getSessionFactory();
    }
    
    /*添加*/
    public void insert(Student stu){
        Session s = sf.openSession();
        
        Transaction ts = s.beginTransaction();
        /*添加*/
        s.save(stu);
        
        ts.commit();
        s.close();
    }
    
    /*修改*/
    public void udate(Student stu){
        Session s = sf.openSession();
        
        Transaction ts = s.beginTransaction();
        /*修改*/
        s.update(stu);
        
        ts.commit();
        s.close();   
    }
    
    /*删除*/
    public void delete(String sno){
        Session session = sf.openSession();
        Transaction tx = session.beginTransaction();
        
        Student stu = session.get(Student.class, sno);
        /*删除*/
        session.delete(stu);
        tx.commit();
        session.close();
    }
    
    /*查询*/
    public List<Student> getAll(){
        Session s = sf.openSession();
        String hql = "from Student";
        
        /*查询*/
        List<Student> list =  s.createQuery(hql).list();
        s.close();
        return list;
    }
    
    

}

 

5)test测试

package test;

import dao.StudentDao;
import entity.Student;

public class Main {

    public static void main(String[] args) {
        StudentDao dao = new StudentDao();
        
        /*添加*/
        Student stu = new Student();
        stu.setSno("22");
        stu.setSname("z22");
        dao.insert(stu);
        
        /*修改*/
        /*Student stu = new Student();
        stu.setSno("11");
        stu.setSname("2222222");
        dao.udate(stu);*/
        
        /*查询*/
        /*
        List<Student> list = dao.getAll();
        for(Student s: list){
            System.out.println(s.getSname());
        }*/

        
        /*删除*/
        /*dao.delete("22");*/
    }

}

 

 

/*以上个人整理笔记,如果有误或者有不懂的地方,欢迎评论与指出*/

以上是关于Hibernate ORM框架——续第一章:Hibernate的增删改查(第一个hibernate代码的优化)的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate ORM框架——第一章:Hibernate简介与操作基础

hibernate-第一章-基础

MyBatis第一章

Hibernate 基本使用

Hibernate快速上手

hibernate入门知识-01