其他分享
首页 > 其他分享> > Hibernate4学习day0--hibernate封装--注解--单元测试

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--勾选所要测试的类方法---在测试方法中添加事务连接

标签:Hibernate4,hibernate,name,单元测试,private,session,Student,import,public
来源: https://www.cnblogs.com/fdxjava/p/10661478.html