以下程序是Hiberante入门程序:代码如下:首先说hibernate开发流程.A、准备一个POJO类 B、创建类的映射和配置文件(hibernate.cfg.xml class.hbm.xml)class.hbm.xml此配置文件是必须与POJO类中的属性一一对应.
现在我以我创建的程序为例来进行说明:数据库为demo,表的名字为admin
1、POJO类
package com.wch.pojo;
public class Admin {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
class.hbm.xml映射文件存放位置必须于POJO位置一致.也就是说必须放在同一个目录.hibernate.cfg.xml放在src根目录下.
2、创建hibernate.cfg.xml和class.hbm.xml(class指的是POJO类的名字)
class.hbm.xml:
<?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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.wch.pojo.Admin" table="admin">
<id name="id" type="integer">
<column name="id" />
<generator class="assigned"></generator>
</id>
<property name="username" type="string">
<column name="username" length="32" not-null="false" />
</property>
<property name="password" type="string">
<column name="password" length="20" not-null="false" />
</property>
</class>
</hibernate-mapping>
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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/demo
</property>
<property name="connection.username">root</property>
<property name="connection.password">****</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">mysql5.0</property>
<property name="show_sql">true</property>
<mapping resource="com/wch/pojo/Admin.hbm.xml" />
</session-factory>
</hibernate-configuration>
3、创建应用程序并进行代码测试:
package com.wch.op;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.wch.pojo.Admin;
public class UserOperation {
public static void main(String[] args)throws Exception{
Configuration config = new Configuration().configure();
// 创建工厂
SessionFactory factory = config.buildSessionFactory();
// 打开session
Session session = factory.openSession();
// 事务提交
try{
Transaction tx = session.beginTransaction();
// 创建对象
Admin hb = new Admin();
hb.setId(3);
hb.setUsername("Hibernate3.2");
hb.setPassword("20081202");
//hb.setUsername("WCH");
//hb.setPassword("Hibernate");
// 执行插入语句,在hibernat中操作的是一个对象
session.save(hb);
// 提交事务
tx.commit();
}catch(Exception e)
{
System.out.println("error Msg:"+e.getMessage());
}finally{
// close session
session.close();
}
}
}
更为详细的解释请参照Hibernate官方网站:www.hibernate.org