Hibernate----QBC语句

Hibernate----QBC语句

1、什么是QBC?

QBC---->Query By Criteria,根据Criteria接口进行查询。是检索对象的另一种方式。

2、QBC检索步骤

QBC检索步骤:
1.调用Session的createCriteria()方法创建一个Criteria对象。
2.设定查询条件。Restrictions类提供了一系列用于设定查询条件的 静态方法
这些 静态方法都返回Criterion实例,每个Criterion实例代表一个查询条件。
Criteria的add()方法用于加入查询条件。
3.调用Criteria的list()方法执行查询语句。该方法返回List类型的查询结果,在
List集合中存放了符合查询条件的持久化对象。
3、实例如下:

(1)新建一个Java工程,添加Hibernate3.3支持,连接test数据库

(2)新建User.java类:

package com.etc.dao;

public class User {
	private int id;
	private String username;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + "]";
	}
	
	
}

(3)新建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">
<hibernate-mapping>
	<class name="com.etc.dao.User" table="t_user" catalog="test">
		<id name="id" type="java.lang.Integer">
			<column name="user_id"/>
			<generator class="native"/>
		</id>
		<property name="username" type="java.lang.String">
			<column name="user_name"/>
		</property>
	</class>
</hibernate-mapping>

(4)修改hibernate.cfg.xml配置文件:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
    	<property name="show_sql">true</property>
    	<property name="format_sql">true</property>
    	<!-- <property name="hbm2ddl.auto">create</property> -->
    	<property name="current_session_context_class">thread</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="myeclipse.connection.profile">com.mysql.jdbc.Driver</property>
    	<mapping resource="com/etc/dao/User.hbm.xml"/>
    </session-factory>

</hibernate-configuration>

(5)新建test文件夹,新建UserDAOTest.java测试类:

package com.etc.dao;

import java.util.List;

import javax.security.auth.login.CredentialException;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;

public class UserDAOTest {
	/**
	 * Description:自动创建t_user表,并且向表中插入5条数据
	 * @author zoey
	 * @date 2017年7月28日
	 */
	@Test
	public void testCreate(){
		SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		for(int i=1;i<=10;i++){
			User user = new User();
			user.setUsername("zoey"+i);
			session.save(user);
		}
		session.getTransaction().commit();
	}
		
	/**
	 * Description:QBC查询
	 * @author zoey
	 * @date 2017年7月28日
	 */
	@Test
	public void testQbc(){
		SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		
		//创建一个Criteria接口对象,查询User
		Criteria c = session.createCriteria(User.class);
		//添加where条件,相当于 where username = zoey1
		c.add(Restrictions.eq("username","zoey1"));
		//执行查询,获取查询结果集
		List<User> list1 = c.list();
		System.out.println(list1.get(0));
		
		session.getTransaction().commit();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值