zoukankan      html  css  js  c++  java
  • 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");*/
        }
    
    }

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

  • 相关阅读:
    火狐firefox进行post提交测试
    spring cloud:config-eureka-refresh
    spring cloud:config
    使用Docker部署Gitlab
    Docker配置加速器
    spring cloud:eureka
    Sql Server 出现此数据库没有有效所有者问题
    将VS2012的项目转化成VS2010
    SQL Server 2008将数据库数据导出到脚本
    Sql Server 显示插入Identity字段
  • 原文地址:https://www.cnblogs.com/Sunny-lby/p/7350463.html
Copyright © 2011-2022 走看看