package com.zxd.test;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.zxd.bean.House;
import com.zxd.util.QueryProperty;
/**
* HQL封闭查询的测试类
* @author zhang
*
*/
public class TestHouse {
public static void main(String[] args) {
//公共的成员变量
SessionFactory sf = null;
Session session = null;
QueryProperty qp = new QueryProperty();
//封装查询的数据
qp.setTitle("%好房%");
qp.setStreet_id("1002");
qp.setType_id("1004");
qp.setLow_price(20);
qp.setHigh_price(200);
qp.setSmall_floorage(50);
qp.setBig_floorage(180);
//HQL语句
StringBuffer sb = new StringBuffer();
sb.append("from House where ");
sb.append("(title like :title) ");
sb.append("and (type_id like :type_id) ");
sb.append("and (street_id like :street_id) ");
sb.append("and (price between :low_price and :high_price) ");
sb.append("and (floorage between :small_floorage and :big_floorage)");
try {
//开始执行查询
sf = new Configuration().configure().buildSessionFactory();
session = sf.openSession();
Query query = session.createQuery(sb.toString());
query.setProperties(qp);
List<house> list = query.list();
//第一种用:的循环
/*for(House house:list){
System.out.println("标题是:"+house.getTitle());
System.out.println("面积是:"+house.getFloorage());
System.out.println("价格是:"+house.getPrice());
System.out.println("区是:"+house.getStreet().getDistrict().getName());
System.out.println("街道是:"+house.getStreet().getName());
System.out.println("----------------------------------");
}*/
//第二种循环
for(int i = 0;i<list.size system.out.println e.printstacktrace session.close sf.close><p>上面例子中的第一种循环是我没有记住的,用了关键字符“:”,一般这种循环是用来对一个集合的遍历上的(list<house>、map)中的用的很方便。 <br><br>第二种循环是普通的循环我这是java中最常见的一种<a style="color:#f60; text-decoration:underline;" title="for循环" href="https://m.php.cn/zt/16466.html" target="_blank">for循环</a>。</house></p>
<p>更多Java中的两种for循环介绍相关文章请关注PHP中文网!</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/ai/1792" title="360鸿图"><img
src="https://img.php.cn/upload/ai_manual/000/000/000/175680365410876.png" alt="360鸿图" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/ai/1792" title="360鸿图" class="overflowclass">360鸿图</a>
<p class="overflowclass">一款面向中文用户的AI图像生成工具,通过生成式AI帮助用户根据文字创意制作图片,适合日常视觉创作。</p>
</div>
<a rel="nofollow" href="/ai/1792" title="360鸿图" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div></list.size></house>
Java免费学习笔记:立即使用
解锁 Java 大师之旅:从入门到精通的终极指南










