标签:java访问数据库 j2ee 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">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/zhouxiang/model/User.hbm.xml"/>
</session-factory>
</hibernate-configuration><?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zhouxiang.model">
<class name="User" polymorphism="explicit">
<id name="id">
<generator class="uuid" ></generator>
</id>
<property name="name" column="username"></property>
<property name="password" column="password"></property>
</class>
</hibernate-mapping>
<!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">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="com/zhouxiang/model/User.hbm.xml"/> </session-factory> </hibernate-configuration>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zhouxiang.model">
<class name="User" polymorphism="explicit">
<id name="id">
<generator class="uuid" ></generator>
</id>
<property name="name" column="username"></property>
<property name="password" column="password"></property>
</class>
</hibernate-mapping>/**
*
*/
package com.zhouxiang.model;
/**
* @ClassName: User
* @Description: TODO
* @author zx
* @date 2014年5月15日 上午10:40:43
*
*/
public class User {
private String id;
private String name;
private String password;
public User()
{}
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 String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}/**
*
*/
package com.zhouxiang.test;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.zhouxiang.model.User;
/**
* @ClassName: Test1
* @Description: TODO
* @author zx
* @date 2014年5月16日 上午10:09:55
*
*/
public class Test1 {
public static void main(String args[])
{
SessionFactory factory=null;
Session session=null;
try {
Configuration cfg = new Configuration().configure();
factory=cfg.buildSessionFactory();
session=factory.openSession();
session.getTransaction().begin();
User user=new User();
user.setName("bbb");
user.setPassword("123456");
session.save(user);
session.getTransaction().commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
session.getTransaction().rollback();
}
finally
{
if(session!=null)
{
if(session.isOpen())
{
session.close();
}
}
}
}
}
hibernate环境配置和使用,布布扣,bubuko.com
标签:java访问数据库 j2ee hibernate 面向对象编程 框架
原文地址:http://blog.csdn.net/leimengyuanlian/article/details/27111605