hibernate-001

package com.hibernate.domain;

import java.util.Date;

public class User {
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	private int id;
	private String name;
	private Date birthday;
}

 

<?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 package="com.hibernate.domain">

	<class name="User" table="user">
		<id name="id">
			<generator class="native" />
		</id>
		<property name="name"/>
		<property name="birthday"/>
	</class>
</hibernate-mapping>

 

<!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="connection.url">jdbc:mysql:///hibernate</property>
		<property name="connection.username">root</property>
		<property name="connection.password">789+</property>
		
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		
		<property name="connection.password">789+</property>
		<property name="hbm2ddl.auto">create</property>

		<!--  
		<class-cache class="com.hibernate.domain.User" usage="read-only"/>
		-->
		<mapping resource="com/hibernate/domain/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

 

package com.utils;

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

public final class HibernateUtil {
	private static SessionFactory sessionFactory;
	private HibernateUtil(){
		
	}
	
	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	static{
		Configuration cfg = new Configuration();
		cfg.configure();
		sessionFactory = cfg.buildSessionFactory();
	}
	
	public static Session getSession(){
		return sessionFactory.openSession();
	}
}

 

package com.hibernate;

import java.util.Date;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.hibernate.domain.User;
import com.utils.HibernateUtil;

public class Base {
	public static void main(String[] args) {
		User user = new User();
		user.setBirthday(new Date());
		user.setName("Eric");
		
		addUser(user);
		System.out.println("id"+user.getId());
	}
	
	static void addUser(User user){
		Session s = null;
		Transaction tx = null;
		try{
			s = HibernateUtil.getSession();
			tx = s.beginTransaction();
			s.save(user);
			tx.commit();
		}catch (HibernateException e) {
			if(tx != null){
				tx.rollback();
			}
			throw e;
		}finally{
			if(s != null){
				s.close();
			}
		}
	}
}

  

你可能感兴趣的:(sql,Hibernate,mysql,jdbc,配置管理)