Hibernate简单学习总结

Stella981
• 阅读 582

1.hibernate.cfg.xml 配置

<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/student</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property><!-- 将SQL语句打印到控制台 -->
        <property name="hibernate.format_sql">true</property><!-- 将SQL语句格式化后,打印到控制台 -->
        
        
        <mapping resource="com/hibernate/User.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

2.User.hbm.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.hibernate.User">
        <id name="id">
        <generator class="uuid"/><!-- 自动生成一个32位的字符串,必须提供生成策略 -->
        </id>
        <property name="name"/>
        <property name="password"/>    
    </class>

</hibernate-mapping>

3.创建User实体

package com.hibernate;

public class User {
    
    private String id;
    private String name;
    private String password;
    //private String rights;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

4.ExportDB类(根据xml文件生成对应数据库的表)

package com.hibernate;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/*
 * 将hbm生成ddl
 */

public class ExportDB {

    public static void main(String[] args) {
        //默认读取hibernate.cfg.xml文件
        Configuration cfg = new Configuration().configure();
        SchemaExport export = new SchemaExport(cfg);
        export.create(true, true);    
    }
}

5.测试类Client

package com.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Client {
    public static void main(String[] args) {        
        // org.hibernate.cfg.Configuration类的作用:
        // 读取hibernate配置文件(hibernate.cfg.xml或hiberante.properties)的.
        // new Configuration()默认是读取hibernate.properties
        // 所以使用new Configuration().configure();来读取hibernate.cfg.xml配置文件
        // 默认读取hibernate.cfg.xml文件
        Configuration cfg = new Configuration().configure();

        // 创建SessionFactory
        // 一个数据库对应一个SessionFactory
        // SessionFactory是线线程安全的(最好创建一次)
        SessionFactory factory = cfg.buildSessionFactory();

        // 创建session
        // 此处的session并不是web中的session
        // session只有在用时,才建立concation,session还管理缓存。
        // session用完后,必须关闭。
        // session是非线程安全,一般是一个请求一个session.
        Session session = null;
        try {
            session = factory.openSession();
            //手动开启事务(可以在hibernate.cfg.xml配置文件中配置自动开启事务) 
            session.beginTransaction();
            
            // 保存数据,此处的数据是保存对象,这就是hibernate操作对象的好处,
            // 我们不用写那么多的JDBC代码,只要利用session操作对象,至于hibernat如何存在对象,这不需要我们去关心它,
            // 这些都有hibernate来完成。我们只要将对象创建完后,交给hibernate就可以了。
            User user =new User();
            user.setName("Lee");
            user.setPassword("00");
            session.save(user);
            //提交事物
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            //回滚事物
            session.getTransaction().rollback();
        }finally{
            if (session!=null) {
                if(session.isOpen())
                    //关闭session
                    session.close();
            }
        }
    }

}
点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
2年前
TAB切换效果
<!DOCTYPEhtmlPUBLIC"//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1transitional.dtd"<htmlxmlns"http://www.w3.org/1999/xhtml"<head<
Easter79 Easter79
2年前
SSM框架中的Mapper.xml文件中的增、删、改、查等操作
<?xmlversion"1.0"encoding"UTF8"?<!DOCTYPEmapperPUBLIC"//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis3mapper.dtd"<!代理方式
Stella981 Stella981
2年前
SSM框架中的Mapper.xml文件中的增、删、改、查等操作
<?xmlversion"1.0"encoding"UTF8"?<!DOCTYPEmapperPUBLIC"//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis3mapper.dtd"<!代理方式
Wesley13 Wesley13
2年前
Java project 中获取hibernate的Configuration的2种方式
   方式一、通过hibernate.cfg.xml文件配置        1.hibernate.cfg.xml<?xmlversion"1.0"encoding"UTF8"?<!DOCTYPEhibernateconfigurationPUBLIC"//Hiberna
Wesley13 Wesley13
2年前
##SSM框架整合中web.xml配置文件
<!DOCTYPEwebappPUBLIC"//SunMicrosystems,Inc.//DTDWebApplication2.3//EN""http://java.sun.com/dtd/webapp_2_3.dtd"<webapp<displaynameA
Stella981 Stella981
2年前
JQuery事件
1<!DOCTYPEhtmlPUBLIC"//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1transitional.dtd"2<htmlxmlns"http://www.w3.org/1999/
Stella981 Stella981
2年前
Hibernate:sessionFactory 对象的创建
packagecom.bjsxt.util;importorg.hibernate.SessionFactory;importorg.hibernate.cfg.Configuration;publicclassHibernateUtil{privatestatic
Stella981 Stella981
2年前
Hibernate
J2EE开发中,特别是使用了Hibernate的项目,在开发阶段,有时候开发人员想看看程序执行的时候实际执行的SQL和动态SQL传入的参数情况,以调试和判断程序逻辑。本文总结下怎么实现,希望对你有用。~hibernate打开SQL显示这个比较简单,大多说人都知道,呵呵,配置如下:hibernate.show\_sqltruehibe
Wesley13 Wesley13
2年前
1_eclipse导入hibernate 的DTD 文件
1、解压:找到dtd文件!(https://oscimg.oschina.net/oscnet/65874ac3d102e2ab1d6bfcfa2a074654b59.jpg)lib!(https://img2018.cnblogs.com/blog/1182352/201902/1182352201902160127415271
Stella981 Stella981
2年前
Hibernate学习之SessionFactory
由于SessionFactory是一个重量级的类,在一个应用中我们需要做成单例的,我选择的做法是:\importorg.hibernate.SessionFactory;importorg.hibernate.cfg.Configuration;