Hibernate_Criteria QBC查询

package com.sun.test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.junit.After;
import org.junit.Before;

import com.sun.dao.impl.HibernateSessionFactory;
import com.sun.entity.House;

public class QBCTest {
	private Session session;

	@Before
	public void before() {
		session = HibernateSessionFactory.getSession();
	}

	@After
	public void after() {
		session.close();
	}
    
	@org.junit.Test
	public void baseQuery(){
		Map map=new HashMap();
		map.put("title", "江荆路房屋出租");
		Criteria c=session.createCriteria(House.class);
		//c.add(Restrictions.like("title", "出租",MatchMode.ANYWHERE))
		//.add(Restrictions.between("price", 2000l, 12000l));
		c.add(Restrictions.allEq(map)).addOrder(Order.desc("price"));
		List<House> houseList=c.list();
		for(House h:houseList){
			System.out.println(h.getTitle());
		}
	}
	
	@org.junit.Test
	public void baseExample(){
		Criteria c=session.createCriteria(House.class);
		House house=new House();
		house.setTitle("江");
		//house.setPrice(3000l);
		house.setDescription("家电");
		List<House> houseList=c.add(Example.create(house).enableLike(MatchMode.ANYWHERE).excludeZeroes()).list();
		for(House h:houseList){
			System.out.println(h.getTitle());
		}
	}
	
}

 

你可能感兴趣的:(Hibernate)