Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7068300
  • 博文数量: 702
  • 博客积分: 10821
  • 博客等级: 上将
  • 技术积分: 12031
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-02 10:41
个人简介

中科院云平台架构师,专注于数字化、智能化,技术方向:云、Linux内核、AI、MES/ERP/CRM/OA、物联网、传感器、大数据、ML、微服务。

文章分类

全部博文(702)

分类:

2006-06-15 11:13:34

1、  mysql中创建数据库test,创建表t_user  (id[自动增长],name,sex)

2、  eclipse中配置database explore

3、  打开database explore,建立连接

4、  创建java工程(注意将hibernateJunitmysql jdbc驱动程序都添加上,如下图)

5、  为工程添加hibernate支持

注意这里如果选中add hibernate2.1 libraries to project? 则前面创建工程时可以不添加hibernate类库

6、  回到database explore,创建hibernate映射

7、  查看工程

hibernate.cfg.xml文件中加入

       True

      

           net.sf.hibernate.transaction.JDBCTransactionFactory

      

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());

}

}

阅读(2015) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~