初学hibernate

今天初学了hiberate的基本使用,做了一个简单的demo

需要注意点如下:
1. 创建任意java项目,然后点击右键,添加hiberate组件
2. 配置数据库,本人使用mysql, url, username, password, etc
3. 在pakage model下,增加实体类User
4. 在User同级目录下编写User.hbm.xml配置文件
5. 生成HibernateUtil类
6. 编写HibernateDemo类,获得session, 开始session.beginTransaction()
   session.save(user)
   session.commit(), 提交到数据库
  详见相关代码


User.java
package model;


/**
 * @author sean
 * 
 *
 */
public class User {
    private Long id;
    private String name;
    private Long age;
 
    public User() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
 
    public Long getAge() {
        return age;
    }

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




hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="connection.username">root</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hello</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="myeclipse.connection.profile">mysql</property>
        <property name="connection.password">root</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>    
    
    <mapping resource="model/User.hbm.xml"/> 
    </session-factory>

</hibernate-configuration>


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="model.User" table="T_USER"> 

        <id name="id" column="id"> 
            <generator class="native"/> 
        </id> 

        <property name="name" column="name"/> 

        <property name="age" column="age"/> 

    </class> 

</hibernate-mapping>



HibernateUtils


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

/**
 * @author sean
 * 
 *
 */
public class HibernateUtil {

	private static SessionFactory sessionFactory;
	
	static{
		sessionFactory = new Configuration().configure().buildSessionFactory();
	}
	
	public static Session getSessionFactory(){
		return sessionFactory.openSession();
	}
	
	public static void shutdown(){
		getSessionFactory().close();
	}
}


HibernateDemo



import model.User;

import org.hibernate.Transaction;
import org.hibernate.classic.Session;

/**
 * @author sean
 * 
 * @since 2009/06/02
 *
 */
public class HibernateDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		User user = new User();
		user.setName("sean");
		user.setAge(new Long(33));
		
		Session session = HibernateUtil.getSessionFactory();
		
		Transaction tx = session.beginTransaction();
		
		session.save(user);
		tx.commit();
		session.close();
		System.out.println("新增资料,查看数据库");
		HibernateUtil.shutdown();
	}

}




你可能感兴趣的:(java,Hibernate,mysql,xml,MyEclipse)