hibernate框架hql的条件查询(九)

给出3个实例,以便忘记怎么写的时候参照一下

package com.hib.test;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import com.hib.domain.User;
import com.hib.utils.HibernateUtils;

public class Demo2 {

	@Test
	public void testQuery2() {
		Session session = HibernateUtils.getSession();
		Transaction tx = session.beginTransaction();

		// 查询的方式
		Query query = session.createQuery(" from User where age > :age ");
		// 设置值
		query.setInteger("age", 23);
		// 查询
		List list = query.list();

		for (User user : list) {
			System.out.println(user);
		}

		tx.commit();
		session.close();
	}

	@Test
	public void testQuery1() {
		Session session = HibernateUtils.getSession();
		Transaction tx = session.beginTransaction();

		// 查询的方式
		Query query = session.createQuery(" from User where name like ? ");
		// 设置值
		query.setString(0, "%xiao%");
		// 查询
		List list = query.list();

		for (User user : list) {
			System.out.println(user);
		}

		tx.commit();
		session.close();
	}

	@Test
	public void testQuery() {
		Session session = HibernateUtils.getSession();
		Transaction tx = session.beginTransaction();

		// 查询的方式
		Query query = session.createQuery(" from User where age>? ");
		// 设置值
		query.setInteger(0, 20);
		// 查询
		List list = query.list();

		for (User user : list) {
			System.out.println(user);
		}

		tx.commit();
		session.close();
	}
}
源码下载

你可能感兴趣的:(Hibernate)