查询数据-ESQL方式

2018-05-31 09:45 更新

框架中提供了一个专门用于执行esql和sql的api类。esql就是sql语句,但是只从where条件关键字开始编写的后面部分,该api类是

org.myhibernate.core.method.Query , 获取方式如下

Template<Product> template=new ProxyTemplate(Product.class).getInstance();
		Query<Product> query=template.getQuery();
		try 
		{
			// ???
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			template.close();
		}


第一组查询api方法如下

public int query(String esql)  该方法返回esql查询后返回的结果数 ,查询的是所有数据的id值,是框架内存分页

public List<T> getResults(int start,int end)  该方法用以对上面查询的结果进行分页获取


public  List<T> queryAll()  获取所有结果

public  List<T> queryAll(String esql,boolean isCache) 获取所有结果,并可以传入esql条件过滤,和指定是否缓存

如下获取满足条件的第10到20条记录示例

Template<Product> template=new ProxyTemplate(Product.class).getInstance();
		Query<Product> query=template.getQuery();
		try 
		{
			int count=query.query("where price>50");
			List<Product> products=query.getResults(10, 20);
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			template.close();
		}


如下获取满足条件的所有记录 示例

Template<Product> template=new ProxyTemplate(Product.class).getInstance();
		Query<Product> query=template.getQuery();
		try 
		{
			List<Product> products= query.queryAll("where price>50", true);
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			template.close();
		}


第二组查询api方法如下

public List<T> where(String esql,int start,int size)  查询满足esql条件的记录,并且分页,采用的是数据库分页

public List<T> where(String esql,Object[] values,int start,int size) 查询满足esql 预编译方式条件的记录,并且分页

这两个方法和getCount() 获取记录条数一起使用

示例如下

Template<Product> template=new ProxyTemplate(Product.class).getInstance();
		Query<Product> query=template.getQuery();
		try 
		{
			List<Product> products=query.where("where price>50", 0, 10);
			int count=query.getCount();
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			template.close();
		}


第三组查询api

   Query中提供了几个封装的查询方法,以find开头的,读者自己测试测试即可







以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号