hibernate笔记(一)Hibernate_First

User.java

package com.goby.Hibernate.bean;

public class User {
	private int id;
	private String name;
	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;
	}
}

 

 

User.hbm.xml

<?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.goby.Hibernate.bean">

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

 

 

hibernate.cfg.xml

<!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.url">jdbc:mysql://localhost/hibernate_test</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">seebig</property>
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">false</property>
		
		<mapping resource="com/goby/Hibernate/bean/User.hbm.xml" />
	</session-factory>
</hibernate-configuration>

 

 

建立表代码 :ExportDB.JAVA

\package com.goby.Hibernate.bean;

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

public class ExportDB {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Configuration cfg = new Configuration().configure();
		SchemaExport se = new SchemaExport(cfg);
		se.create(true, true);
	}

}

 

测试程序代码:Test.java

package test;


import junit.framework.TestCase;

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

import com.goby.Hibernate.bean.User;

public class testHibernate_test extends TestCase {
	
		public void testSave(){
			Session session = null;
			try{
			Configuration cfg = new Configuration().configure();
			SessionFactory sf = cfg.buildSessionFactory();
			session = sf.openSession();
			session.beginTransaction();
			User user = new User();
			user.setName("goby");
			session.save(user);
			
			session.getTransaction().commit();
			}catch(Exception ex){
				ex.printStackTrace();
				session.getTransaction().rollback();
			}finally{
				if(session!=null){
					if(session.isOpen()){
						session.close();
					}
				}
			}
			
		}
		
		
		public void testload(){
			Session session = null;
			try{
			Configuration cfg = new Configuration().configure();
			SessionFactory sf = cfg.buildSessionFactory();
			session = sf.openSession();
			session.beginTransaction();
			
			User user = (User)session.load(User.class, 2);
			System.out.println(user.getName());
			session.getTransaction().commit();
			}catch(Exception ex){
				ex.printStackTrace();
				session.getTransaction().rollback();
			}finally{
				if(session!=null){
					if(session.isOpen()){
						session.close();
					}
				}
			}
			
		}
	
}

 

 

你可能感兴趣的:(sql,Hibernate,bean,mysql,jdbc)