Hibernate4入门学习过程记录一

使用IDEA,src下的资源文件无法自动编译输出到out目录下,网上给出4种解决方案,请参考http://blog.youkuaiyun.com/shifangwannian/article/details/48882201

本人采用建立resources文件夹并Mark Directory as --->Resources Root的方案,是实际项目中这种方法也是最常用的。


简单的过程不做记录,记下关键性配置文件,以及方法。


hibernate.cfg.xml文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="connection.url">jdbc:mysql://localhost:3306/hibernatetest</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.username">root</property> 
    <property name="connection.password">123456</property> 
    
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.show_sql">true</property>  
      
    <!-- DB schema will be updated if needed -->
    <property name="hbm2ddl.auto">update</property> 
    
    <mapping resource="com/domain/User.hbm.xml"></mapping>
  </session-factory>
</hibernate-configuration>

User.java

package com.domain;

public class User {
    private String id;
    private String name;
    private Integer age;
    private String address;

    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 Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        if (id != null ? !id.equals(user.id) : user.id != null) return false;
        if (name != null ? !name.equals(user.name) : user.name != null) return false;
        if (age != null ? !age.equals(user.age) : user.age != null) return false;
        if (address != null ? !address.equals(user.address) : user.address != null) return false;

        return true;
    }

    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (age != null ? age.hashCode() : 0);
        result = 31 * result + (address != null ? address.hashCode() : 0);
        return result;
    }
}


User.hbm.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="com.domain.User">
        <id name="id">
            <generator class="uuid"/>
        </id>
        <property name="name"/>
        <property name="age"/>
        <property name="address"/>
    </class>
</hibernate-mapping>

test01.java
package com.test;

import com.domain.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class test01 {

    /**
     * 根据hibernate配置文件,生成数据库
     */
    public static void exportEntityToDatabase(){
        Configuration configuration = new Configuration().configure();

        SchemaExport schemaExport = new SchemaExport(configuration);
        schemaExport.create(true, true);
    }

    /**
     * hibernate4 保存对象
     */
    public static void saveUserTest(){
        Configuration configuration = new Configuration().configure();
        
        Session session = null;
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
                .applySettings(configuration.getProperties())
                .buildServiceRegistry();

        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        
        try {
            session = sessionFactory.openSession();
            session.beginTransaction();
            
            User user = new User();
            user.setName("小新");
            user.setAge(22);
            user.setAddress("浙");
            session.save(user);
            session.getTransaction().commit();
        }catch (Exception e){
            e.printStackTrace();
            session.getTransaction().rollback();
        }finally {
            if (session != null && session.isOpen()){
                session.close();
            }
        }
    }
    
    public static void main(String[] args) {
//        exportEntityToDatabase();
        saveUserTest();
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值