码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2017-08-12 16:10:31      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:build   update   res   comm   div   java   entity   private   standard   

一:.项目结构

技术分享

 

二、代码

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代码的优化)

标签:build   update   res   comm   div   java   entity   private   standard   

原文地址:http://www.cnblogs.com/Sunny-lby/p/7350463.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!