说明:代码颜色为#ff6600
一、环境配置
新建一个Java Project项目,将下列jar文件导入其中:
mysql-connector-java-5.0.8-bin.jar
antlr-2.7.6.jar
commons-collections-3.1.jar
commons-dbcp-1.4.jar
commons-pool-1.5.6.jar
dom4j-1.6.1.jar
hibernate3.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
slf4j-api-1.6.1.jar
二、功能实现
说明,使用Hibernate实现将用户名和密码记录到userdb数据库的user数据表中。
a.新建com.test.example.hibernate.entity.User类,内容如下:
package com.test.example.hibernate.entity;
public class User {
private int id;
private String name;
private String password;
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 String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
b.在src/com/test/example/hibernate/entity目录下,新建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">
<!-- Generated 2011/07/28 17:03:12 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.test.example.hibernate.entity.User" table="USER">
<id name="id" type="int">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" />
</property>
</class>
</hibernate-mapping>
c.在/src目录下新建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">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/userdb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">mysql</property>
<property name="hibernate.connection.pool_size">100</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="com/test/example/hibernate/entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
d.新建com.test.example.hibernate.HibernateUtil类,内容如下:
package com.test.example.hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.out.println("you are error");
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
e.新将com.test.example.hibernate.client.HibernateClient客户端测试类,内容如下:
package com.test.example.hibernate.client;
import org.hibernate.Session;
import com.test.example.hibernate.HibernateUtil;
import com.test.example.hibernate.entity.User;
public class HibernateClient {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HibernateClient hibernateClient=new HibernateClient();
hibernateClient.createAndStoreUser("user3","password3");
HibernateUtil.getSessionFactory().close();
}
public void createAndStoreUser(String username,String password){
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
User user=new User();
user.setName(username);
user.setPassword(password);
session.save(user);
session.getTransaction().commit();
}
}
说明:上述类实现将用户名为user3,密码为password3保存到数据库中。
三、测试
运行HibernateClient类,当运行完成后,在数据库中若可以看到导入的数据 ,则说明保存成功,反之失败。