1、 在mysql中创建数据库test,创建表t_user (id[自动增长],name,sex)
2、 在eclipse中配置database explore
3、 打开database explore,建立连接
4、 创建java工程(注意将hibernate、Junit、mysql jdbc驱动程序都添加上,如下图)
5、 为工程添加hibernate支持
注意这里如果选中add hibernate2.1 libraries to project? 则前面创建工程时可以不添加hibernate类库
6、 回到database explore,创建hibernate映射
7、 查看工程
在hibernate.cfg.xml文件中加入
<property name="hibernate.show_sql">True</property>
<property name="hibernate.transaction.factory_class">
net.sf.hibernate.transaction.JDBCTransactionFactory
</property>
8、 建立测试文件HibernateTest.java
package com.tc.test;
import java.util.List;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import junit.framework.Assert;
import junit.framework.TestCase;
public class HibernateTest extends TestCase {
Session session = null;
protected void setUp() {
try {
session=HibernateSessionFactory.currentSession();
} catch (HibernateException e) {
e.printStackTrace();
}
}
protected void tearDown() {
try {
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
public void testInsert() {
try {
TUser user = new TUser();
user.setName("Emma2");
user.setSex(new Integer(0));
session.save(user);
session.flush();
} catch (HibernateException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
public void testSelect() {
String hql = " from TUser where name='Emma'";
try {
List userList = session.find(hql);
TUser user = (TUser) userList.get(0);
Assert.assertEquals(user.getName(), "Emma");
} catch (HibernateException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
}
9、 运行测试
10.数据检索Criteria Query
具体的Criteria 查询表达式语法请参考hibernate手册或夏昕写的hibernate开发指南,这里描述的是: Criteria 高级特性 限定返回的记录范围
package com.tc;
import java.util.Iterator;
import java.util.List;
import net.sf.hibernate.Criteria;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.expression.Order;
public class TestCriteria {
/**
* @param args
* @throws HibernateException
*/
public static void main(String[] args) throws HibernateException {
// TODO Auto-generated method stub
Session session = null;
session=HibernateSessionFactory.currentSession();
Criteria criteria = session.createCriteria(TUser.class);
// 从5条结果开始的5条记录
criteria.setFirstResult(5);
criteria.setMaxResults(5);
criteria.addOrder(Order.asc("id"));
List list=criteria.list();
System.out.println(list.size());
for (Iterator iter = list.iterator(); iter.hasNext();) {
TUser element = (TUser) iter.next();
System.out.println(element.getName());
}
}
}
11、 数据检索HQL
相对Criteria,HQL提供了更接近传统SQL语句的查询语法。
package com.tc.test;
import java.util.List;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
public class TestHQL {
public static void main(String[] args) throws HibernateException {
Session session = null;
session=HibernateSessionFactory.currentSession();
//String hql = "from com.tc.test.TUser";
String hql =
"from com.tc.test.TUser as user where user.name='kk'";
Query query = session.createQuery(hql);
List userList = query.list();
System.out.println(userList.size());
}
}