Hibernate学习详解

1、环境搭建

Hibernate学习详解_第1张图片
image.png

2、项目结构

Hibernate学习详解_第2张图片
image.png

3、hibernate.cfg.xml配置文件





    

        
        com.mysql.jdbc.Driver
        jdbc:mysql:///hibernate_day1?serverTimezone=UTC
        root
        123

        
        true

        
        true

        
        true

        
        update

        
        org.hibernate.dialect.MySQL5Dialect

        
        
    

4、User类和User.hbm.xml配置文件

package com.wsx.hibernate.Entity;

public class User {
    private int uid;
    private String username;
    private String password;
    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public User() {

    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", username='" + username + '\'' +
                ", userpass='" + password + '\'' +
                '}';
    }
}



    
    
        
        
            
            
        
        
        
        
    

5、HibernateUtil工具类

public class HibernateUtil {

    private static  SessionFactory sessionFactory;

    static {
        //1、获取核心配置文件对象,默认是加载src的hibernate.cfg.xml文件
        //new Configuration()默认读properties文件 new Configuration().configure默认读xml文件
        //若xml文件没有配置映射可通过代码添加映射可通过 cf.addResource(String path);
        Configuration cf=new Configuration().configure();

        //2、创建会话工厂
         sessionFactory=cf.buildSessionFactory();

         //监听程序关闭
        Runtime.getRuntime().addShutdownHook(new Thread(){
            @Override
            public void run() {
                System.out.println("程序关闭");
                sessionFactory.close();
            }
        });
    }

    public static Session openSession(){
        return sessionFactory.openSession();
    }

    public static Session getCurrentSession(){
        return sessionFactory.getCurrentSession();
    }
}

6、运行测试

1、插入一条数据

Session session= HibernateUtil.openSession();
        //开启事务
        session.getTransaction().begin();
        //保存
        User user=new User();
        user.setUsername("张三丰1");
        user.setPassword("123");
        session.save(user);
        //提交事务
        session.getTransaction().commit();
        //7、关闭会话
        session.close();

2、事务自动提交【配置事务提交】,可不用Begin和commit;

Session session= HibernateUtil.openSession();
        User user=new User();
        user.setUsername("事务自动提交");
        user.setPassword("123");
        session.save(user);
        //关闭
        session.close();

7、项目结构

你可能感兴趣的:(Hibernate学习详解)