hibernate学习笔记之二 基本环境搭建

1.创建一个普通的java项目

2.加入Hibernate所有的jar包

3.建立包名com.bjpowernode.hibernate

4.建立实体类User.java

package com.bjpowernode.hibernate;

import java.util.Date;

public class User {

    private String id;
    
    private String name;
    
    private String password;
    
    private Date createTime;
    
    private Date expireTime;

    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;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(Date expireTime) {
        this.expireTime = expireTime;
    }
    
    
}

5.提供User.hbm.xml文件,完成对User实体类的映射(和User.java在同一目录)

<?xml version="1.0"?>
<!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.bjpowernode.hibernate.User">
        <id name="id">
            <generator class="uuid"></generator>
        </id>
        <property name="name"></property>
        <property name="password"></property>
        <property name="createTime"></property>
        <property name="expireTime"></property>
    </class>
</hibernate-mapping>

6.提供hibernate.cfg.xml文件,把User.hbm.xml文件加入(hibernate.cfg.xml放置的src文件下)

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/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://172.16.100.10:3306/hibernate_first?useUnicode=true&amp;characterEncoding=GBK</property>
        <property name="hibernate.connection.username">hibernate</property>
        <property name="hibernate.connection.password">hibernate</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        
        <mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/>
        <mapping resource="com/bjpowernode/hibernate/Student.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

7.编写工具类ExoprtDB.java,将hbm生成ddl

package com.bjpowernode.hibernate;

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


public class ExportDB {

    public static void main(String[] args){
        Configuration cfg = new Configuration().configure();
        SchemaExport export = new SchemaExport(cfg);
        export.create(true, true);
        
    }
}

8.建立客户端类Client,添加用户数据到mysql

package com.bjpowernode.hibernate;

import java.util.Date;

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

public class Client {

    public static void main(String[] args){
        Configuration cfg = new Configuration().configure();
        SessionFactory factory = cfg.buildSessionFactory();
        Session session = null;
        try{
            session = factory.openSession();
            session.beginTransaction();
            User user = new User();
            user.setName("张三");
            user.setPassword("123");
            user.setCreateTime(new Date());
            user.setExpireTime(new Date());
            session.save(user);
            session.getTransaction().commit();
            
        }catch (Exception e){
            e.printStackTrace();
            session.getTransaction().rollback();
            
        } finally {
            if(session != null && session.isOpen()){
                session.close();
            }
        }
    }
    
}

9.项目目录树

原文地址:https://www.cnblogs.com/djoker/p/6250519.html