hibernate

1 o/r映射 面向对象编程(hibernate toplink jdo ibaties jpa)

2 跨数据库

 

hibernate 第一个小程序


hibernate
 

<!DOCTYPE hibernate-configuration PUBLIC  
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration>
	<session-factory>
	    <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver
		</property>
		<property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:sdtest
		</property>
		<property name="hibernate.connection.username">system</property>
		<property name="hibernate.connection.password">admin</property>
		<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.hbm2ddl.auto">update</property>
		<mapping resource="com/org/hibernate/Users.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

 

<?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  auto-import="false" package="com.org.hibernate">
	<class name="MyUser" table="users">
		<id name="userid" column="userid"></id>
		<property name="username"/>
		<property name="password"/>
	</class>
</hibernate-mapping>

 

package com.org.hibernate;
public class MyUser {
	int userid ;
	String username ;
	String password ;
	
	public int getUserid() {
		return userid;
	}
	public void setUserid(int userid) {
		this.userid = userid;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}	
}

 

package com.org.hibernate;

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

public class testHibernate {	
	public static void main(String[] args){

		MyUser users = new MyUser();
		users.setUserid(1);
		users.setPassword("aaa");
		users.setUsername("aaa");

		Configuration cfg = new Configuration();
		SessionFactory sf = cfg.configure().buildSessionFactory();
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(users);
		session.getTransaction().commit();
		session.close();
		sf.close();
	}
}

 

你可能感兴趣的:(Hibernate)