Hibernate框架可以用在java项目中,java web项目又得.
最简单的程序有这几样东西:
1.bean类,2.对象映射文件,bean.hbm.xml,3.hibernate配置文件(接数据库的参数)hibernate.cfg.xml,4.测试类
User.java
package com.zhao;
public class User {
private String id = null;
private String username = null;
private String password = null;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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;
}
}
UserInfo.java
package com.zhao;
public class UserInfo {
private int id;
private String userName;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
}
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>
<class name="com.zhao.User" table="user">
<id name="id" type="string" unsaved-value="null">
<column name="id" sql-type="char(32)" />
<generator class="uuid.hex"/>
</id>
<property
name="password"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="password"
length="32"
/>
<property
name="username"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="username"
length="32"
/>
</class>
</hibernate-mapping>
UserInfo.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>
<!--类和表之间的关联-->
<class name="com.zhao.UserInfo" table="userInfo">
<!--类对象的主键和表的主键的关联-->
<id name="id" type="integer">
<column name="id" />
<!--指明主键的自增长类型-->
<generator class="identity"/>
</id>
<!--以下为普通字段的关联-->
<property name="userName" type="string">
<column name="name" length="100" />
</property>
<property name="password" type="string">
<column name="password" length="100" />
</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="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://127.0.0.1/test
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">mysql</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
<mapping resource="com/zhao/User.hbm.xml" />
<mapping resource="com/zhao/UserInfo.hbm.xml" />
</session-factory>
</hibernate-configuration>
TestHibernate.java
package com.zhao.test;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.zhao.User;
public class TestHibernate {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SessionFactory sessionFatory = new Configuration().configure().buildSessionFactory();
User user = new User();
user.setUsername("zhaozhao");
user.setPassword("123");
Session session = sessionFatory.openSession();
Transaction transaction = session.beginTransaction();
session.save(user);
transaction.commit();
session.close();
}
}
HibernateTest.java
package com.zhao;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateTest {
public static void main(String[] args) {
SessionFactory sessions=new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
Session session = sessions.openSession();
Transaction tx = null;
try {
//开始事务
tx = session.beginTransaction();
// //给对象设定值
UserInfo u = new UserInfo();
u.setUserName("FuJingZhou");
u.setPassword("123");
System.out.println("开始插入数据到数据库……");
// //保存数据到数据库
session.save(u);
//从持久化类UserInfo中读取数据
String hql="from UserInfo userInfo where userInfo.userName like ?";
Query query=session.createQuery(hql);
query.setParameter(0,"fujingzhou");
List list=query.list();
Iterator it=list.iterator();
while(it.hasNext()){
UserInfo userInfo=(UserInfo)it.next();
System.out.println(userInfo.getUserName());
}
UserInfo ul = (UserInfo)session.load(UserInfo.class, new Integer(1));
System.out.println("从数据库加裁数据的用户名为:"+ul.getUserName() );
//结束事务
tx.commit();
tx = null;
System.out.println("hi,恭喜你,第一个程序运行成功!");
} catch (HibernateException e) {
e.printStackTrace();
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
}
}