• Hibernate4学习day0--hibernate封装--注解--单元测试


    day02
    2.第一节:HibernateUtil封装

    private static final SessionFactory sessionFactory=buildSessionFactory();

        private static SessionFactory buildSessionFactory(){
            Configuration configuration=new Configuration().configure(); // 实例化配置文件
            ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); // 实例化服务登记
            return configuration.buildSessionFactory(serviceRegistry); // 获取Session工厂
        }
        
        public static SessionFactory getSessionFactory(){
            return sessionFactory;
        }

    public class HibernateUtil {
        
        private static final SessionFactory sessionFactory=buildSessionFactory();
    
        private static SessionFactory buildSessionFactory(){
            Configuration configuration=new Configuration().configure(); // 实例化配置文件
            ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); // 实例化服务登记
            return configuration.buildSessionFactory(serviceRegistry); // 获取Session工厂
        }
        
        public static SessionFactory getSessionFactory(){
            return sessionFactory;
        }
    }


    调用--------
    private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();

    private void add(){
    Session session=sessionFactory.openSession();//生成一个session
    session.beginTransaction();//开启事务

    Student s=new Student();
    s.setName("张三");
    session.save(s);

    session.getTransaction().commit();//提交事务
    session.close();//关闭session

    }
    第二节:XML版CRUD实现

    package com.java.service;
    
    import java.util.List;
    
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    
    import com.java.model.Student;
    import com.java.util.HibernateUtil;
    
    public class StudentTest {
        
        private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
        
        private void add(){
            Session session=sessionFactory.openSession(); // 生成一个session
            session.beginTransaction(); // 开启事务
            
            Student s=new Student();
            s.setName("张三");
            session.save(s);
            
            session.getTransaction().commit(); // 提交事务
            session.close(); // 关闭session
        }
        
        private void delete(){
            Session session=sessionFactory.openSession(); // 生成一个session
            session.beginTransaction(); // 开启事务
            
            Student student=(Student)session.get(Student.class, Long.valueOf(1));
            session.delete(student);
            
            session.getTransaction().commit(); // 提交事务
            session.close(); // 关闭session
        }
        
        private void update(){
            Session session=sessionFactory.openSession(); // 生成一个session
            session.beginTransaction(); // 开启事务
            
            Student student=(Student)session.get(Student.class, Long.valueOf(2));
            student.setName("张三2");
            session.save(student);
            
            session.getTransaction().commit(); // 提交事务
            session.close(); // 关闭session
        }
        
        private void getAllStudent(){
            Session session=sessionFactory.openSession(); // 生成一个session
            session.beginTransaction(); // 开启事务
            
            String hql="from Student";
            Query query=session.createQuery(hql);
            List<Student> studentList=query.list();
            for(Student student:studentList){
                System.out.println(student);
            }
            
            session.getTransaction().commit(); // 提交事务
            session.close(); // 关闭session
        }
    
        public static void main(String[] args) {
            StudentTest studentTest=new StudentTest();
            // studentTest.add();
            // studentTest.delete();
            // studentTest.update();
            studentTest.getAllStudent();
        }
    }



    第三节:注解版CRUD实现

    @Entity----映射实体
    @Table(name="t_teacher")---映射到某一个表
    编写在model实体类前

    @Id--主键注解
    @GenerateValue(generator)--使用
    @GenericGenerator(name="_native",strategy="native")---生成策略

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    import org.hibernate.annotations.GenericGenerator;
    
    @Entity
    @Table(name="t_teacher")
    public class Teacher {
    
        private long id;
        private String name;
        
        @Id
        @GeneratedValue(generator="_native")
        @GenericGenerator(name="_native",strategy="native")
        public long getId() {
            return id;
        }
        public void setId(long id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        @Override
        public String toString() {
            return "Teacher [id=" + id + ", name=" + name + "]";
        }
        
        
    }


    ----TeacherTest.java
    ---hibernate.cfg.xml
    <mapping class="com.java.model.Teacher路径"/>

    第四节:Junit4单元测试框架引入
    ---添加junit4.jar
    -----方法改为public
    创建---junit Test case--勾选所要测试的类方法---在测试方法中添加事务连接

  • 相关阅读:
    Software Solutions CACHE COHERENCE AND THE MESI PROTOCOL
    CACHE COHERENCE AND THE MESI PROTOCOL
    Multiprocessor Operating System Design Considerations SYMMETRIC MULTIPROCESSORS
    Organization SYMMETRIC MULTIPROCESSORS
    PARALLEL PROCESSING
    1分钟内发送差评邮件
    Secure Digital
    SYMMETRIC MULTIPROCESSORS
    A Taxonomy of Parallel Processor Architectures
    parallelism
  • 原文地址:https://www.cnblogs.com/fdxjava/p/10661478.html
Copyright © 2020-2023  润新知