hibernate的QBC查询学习记录

只添加hibernate

package com.dao;

import java.util.Iterator;

import java.util.List;

import org.hibernate.Criteria;

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.criterion.Criterion;

import org.hibernate.criterion.Expression;

import org.hibernate.criterion.Order;

import com.pojo.User;

import com.util.HibernateSessionFactory;

public class UserDAO {

//QBC查询不支持SQL查询

private Session session;

public UserDAO()

{

session=HibernateSessionFactory.getSession();

}

public void closeSession()

{

if(session.isOpen())

{

session.close();

}

}

public void search()

{

//QBC的查询方法

//1,查询全部

Criteria cr=session.createCriteria(User.class);

/* .add(Expression.gt("id", 2))

.add(Expression.not(Expression.eq("id", 6)))

.add(Expression.isNull("name"))

.add(Expression.in("name", new String[]{"金春","金娟"}))

.add(Expression.between("id", 5, 9))

.add(Expression.not(Expression.between("id", 2, 4)))

.add(Expression.like("uname", "T__").ignoreCase())

.addOrder(Order.desc("id")).setFirstResult(1).setMaxResults(10);

*/

//指定要查询的表-以类的方式书写

//定义整体模板

//添加查询条件

Criterion c1=Expression.like("name","%");

Criterion c2=Expression.like("address","%");

//条件添加到模板中

cr.add(c1);

cr.add(c2);

List<User> list=cr.list();

for(Iterator<User> iter=list.iterator();iter.hasNext();)

{

User element=iter.next();

System.out.println(element.getName()+" "+element.getAddress());

}

}

/*当需要多次使用参数查询记录的时候使用此方法。在User.hbm.xml文件当中<!-- CDATA是书写格式xml文件里面的书写格式 -->

<![CDATA[

from User p where p.name like :name

]]>

</query>

</hibernate-mapping>*/

public void serachquery()

{

Query q=session.getNamedQuery("Jk");

q.setString("name", "%");

List l=q.list();

for(Iterator<User> iter=l.iterator();iter.hasNext();)

{

User element=iter.next();

System.out.println(element.getName()+" "+element.getAddress());

}

System.out.println("集合数据量的大小,既是数据记录的条数");

System.out.println(l.size());

}

}

类映射文件User.hbm.xml文件内容如下

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--

Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping>

<class name="com.pojo.User" table="user" catalog="study">

<id name="id" type="java.lang.Integer">

<column name="id" />

<generator class="assigned" />

</id>

<property name="name" type="java.lang.String">

<column name="name" length="22" not-null="true" />

</property>

<property name="address" type="java.lang.String">

<column name="address" length="32" not-null="true" />

</property>

</class>

<query name="Jk">

<!-- CDATA是书写格式xml文件里面的书写格式 -->

<![CDATA[

from User p where p.name like :name

]]>

</query>

</hibernate-mapping>

测试类test

package com.test;

import com.dao.UserDAO;

public class test {

public static void main(String[] args)

{

UserDAO dao=new UserDAO();

//dao.search();

dao.serachquery();

dao.closeSession();

}

}

你可能感兴趣的:(DAO,sql,Hibernate,xml,MyEclipse)