Hibernate学习详解

2019-06-23  本文已影响0人  黑马_b717

1、环境搭建

image.png

2、项目结构

image.png

3、hibernate.cfg.xml配置文件

<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!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 >

        <!--1、配置数据库连接的四个参数-->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql:///hibernate_day1?serverTimezone=UTC</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123</property>

        <!--是否显示sql语句-->
        <property name="show_sql">true</property>

        <!--是否格式化sql语句-->
        <property name="format_sql">true</property>

        <!--是否自动提交事务 -->
        <property name="connection.autocommit">true</property>

        <!--
            hibernate.hbm2ddl.auto
            update:如果数据库没表,自动帮你创建表
                    如果数据库表与hbm不一致,则进行更新
            create:每次启动都帮你创建表
            create-drop:每次启动都帮你创建表,执行后删除
            配置映射文件和表的关系-->
        <property name="hibernate.hbm2ddl.auto">update</property>

        <!--数据方言-->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <!--2、配置JavaBean与表的映射-->
        <mapping resource="com/wsx/hibernate/Entity/User.hbm.xml"></mapping>
    </session-factory>
</hibernate-configuration>

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 + '\'' +
                '}';
    }
}

<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <!-- name :模型的全名称  table:表的全名称-->
    <class name="com.wsx.hibernate.Entity.User" table="t_user">
        <!--name:模型属性名 coulumn:列名-->
        <id name="uid" column="id">
            <!--generatot:id生成策略 native:如果是mysql数据库,id会自动增长-->
            <generator class="native"></generator>
        </id>
        <!--如果模型属性和数据库列名就不用写coulumn-->
        <property name="username" ></property>
        <property name="password"></property>
    </class>
</hibernate-mapping>

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、项目结构

上一篇下一篇

猜你喜欢

热点阅读