hibernate HQL查询、条件查询、SQL查询、外置命名查询(四种查询)

本文章采用测试类学习查询方式


1、HQL查询

(1)用户名属性查询,查询(用户名为小化且id=3)的用户的用户名

在这里插入图片描述

	// 查询函数
	@Test
	public void getUser1() {
	Session session = HibernateUtils.getSession();// 获取session对象
		
		// 普通hql查询(用户名为小化且id=3)的用户的用户名(属性)信息
String hql="select username from User where username='小化' and id=3";

		@SuppressWarnings("unchecked")
		List<String> string=session.createQuery(hql).list();
		if (string.size() != 0)
			System.out.println("成功检索所有用户");
		else
			System.out.println("没有该用户哦");
		for (int i = 0; i < string.size(); i++) {
			String u = string.get(i);
			System.out.println(u);
		}
		HibernateUtils.closeSession(session);// 关闭session
	}

打印结果:
在这里插入图片描述

(2)模糊查询,查询(用户名为X化)的用户的用户名

	@Test
	public void getUser1() {
	Session session = HibernateUtils.getSession();// 获取session对象
		
		//普通hql模糊查询
	String hql="select username from User where username like '%化'";
	
		@SuppressWarnings("unchecked")
		List<String> string=session.createQuery(hql).list();
		if (string.size() != 0)
			System.out.println("成功检索所有用户");
		else
			System.out.println("没有该用户哦");
		for (int i = 0; i < string.size(); i++) {
			String u = string.get(i);
			System.out.println(u);
		}
		HibernateUtils.closeSession(session);// 关闭session
	}

打印结果:
在这里插入图片描述

(3)使用?占位符方式,查询(用户名为小化且id<4)的用户信息

	@Test
	public void getUser2() {
	Session session = HibernateUtils.getSession();// 获取session对象
		
	// 使用?占位符来传递参数(位置参数)查询(用户名为小化且id<4)的用户信息
		//?表示从哪里开始查询
		String hql="from User where username=?0 and id<?1";
		@SuppressWarnings("unchecked")
		List<User> string = session.createQuery(hql).
		setParameter(0, "小化").setParameter(1, 4).list();

		if (string.size() != 0)
			System.out.println("成功检索所有用户");
		else
			System.out.println("没有该用户哦");
		for (int i = 0; i < string.size(); i++) {
			User u = string.get(i);
			if(u.getMyClass()!=null)
				System.out.println(u.toString());
			else
				System.out.println(u.tooString());
		}
		HibernateUtils.closeSession(session);// 关闭session
	}

打印结果:
在这里插入图片描述

(4)使用?占位符方式,查询(用户名为X化)的用户信息(模糊查询)

	@Test
	public void getUser2() {
	Session session = HibernateUtils.getSession();// 获取session对象
//使用?占位符来传递参数(位置参数)查询(用户名为x化)的用户信息(模糊查询)
		String hql="from User where username like ?1";
		@SuppressWarnings("unchecked")
		List<User> string=session.createQuery(hql)
		.setParameter(1,"%化").list();
		
		if (string.size() != 0)
			System.out.println("成功检索所有用户");
		else
			System.out.println("没有该用户哦");
		for (int i = 0; i < string.size(); i++) {
			User u = string.get(i);
			if(u.getMyClass()!=null)
				System.out.println(u.toString());
			else
				System.out.println(u.tooString());
		}
		HibernateUtils.closeSession(session);// 关闭session
	}

打印结果:
在这里插入图片描述
(5)使用:命名方式,查询(用户名为小化且id<4)的用户信息

	@Test
	public void getUser3() {
		// 获取session对象
		Session session = HibernateUtils.getSession();
		//使用(:命名)方式来传递参数(命名参数)
		//查询(用户名为小化且id<4)的用户信息
		String hql="from User where username=:username and id<:id";
		@SuppressWarnings("unchecked")
		List<User> string=session.createQuery(hql)
		.setParameter("username","小化").
		setParameter("id", 4).list();

		if (string.size() != 0)
			System.out.println("成功检索所有用户");
		else
			System.out.println("没有该用户哦");
		for (int i = 0; i < string.size(); i++) {
			User u = string.get(i);
			if(u.getMyClass()!=null)
				System.out.println(u.toString());
			else
				System.out.println(u.tooString());
		}
		HibernateUtils.closeSession(session);// 关闭session
	}

打印结果:
在这里插入图片描述
(6)使用:命名方式,查询(用户名为X化)的用户信息(模糊查询)

	@Test
	public void getUser3() {
		// 获取session对象
		Session session = HibernateUtils.getSession();
		//使用(:命名)方式来传递参数(命名参数)查询
		//(用户名为x化)的用户信息(模糊查询)
		String hql="from User where username like :username";
		@SuppressWarnings("unchecked")
		List<User> string=session.createQuery(hql).setParameter
		("username","%化").list();
		
		if (string.size() != 0)
			System.out.println("成功检索所有用户");
		else
			System.out.println("没有该用户哦");
		for (int i = 0; i < string.size(); i++) {
			User u = string.get(i);
			if(u.getMyClass()!=null)
				System.out.println(u.toString());
			else
				System.out.println(u.tooString());
		}
		HibernateUtils.closeSession(session);// 关闭session
	}

打印结果:
在这里插入图片描述


2、条件查询

使用criteria方式,查询id<5的用户信息

	@Test
	public void getUser4() {
		// 获取session对象
		Session session = HibernateUtils.getSession();
		//使用(criteria)方式查询id小于5的用户信息(条件查询)
		@SuppressWarnings("deprecation")
		List<User> string =session.createCriteria(User.class).add
		(Restrictions.lt("id", 5)).list();

		if (string.size() != 0)
			System.out.println("成功检索所有用户");
		else
			System.out.println("没有该用户哦");
		for (int i = 0; i < string.size(); i++) {
			User u = string.get(i);
			System.out.println(u);
		}
		HibernateUtils.closeSession(session);// 关闭session
	}

打印结果:
在这里插入图片描述


3、SQL查询

使用传统SQL语言方式,查询所有用户信息(所有属性都有数据才能输出)

	@Test
	public void getUser5() {
		// 获取session对象
		Session session = HibernateUtils.getSession();
		// 使用sql查询
		String sql="select * from user_table";
		@SuppressWarnings("unchecked")
		List<User> string =session.createSQLQuery(sql).addEntity
		(User.class).list();
		if (string.size() != 0)
			System.out.println("成功检索所有用户");
		else
			System.out.println("没有该用户哦");
		for (int i = 0; i < string.size(); i++) {
			User u = string.get(i);
			System.out.println(u.toString());
		}
		HibernateUtils.closeSession(session);// 关闭session
	}

打印结果:
在这里插入图片描述


4、外置命名查询

使用该方式需在实体类的映射文件添加如下代码:

<hibernate-mapping package=".....">
	<query name="external_name1">
		<![CDATA[from User where id <?0]]>
	</query>
	
	<query name="external_name2">
		<![CDATA[from User where username like :username]]>
	</query>
</hibernate-mapping>

(1)使用外置命名方式,查询id<4的用户信息

	@Test
	public void getUser6() {
		// 获取session对象
		Session session = HibernateUtils.getSession();
		//查询id<4的用户信息
		@SuppressWarnings("unchecked")
		List<User> string = session.getNamedQuery
		("external_name1").setParameter(0, 4).list();
		
		if (string.size() != 0)
			System.out.println("成功检索所有用户");
		else
			System.out.println("没有该用户哦");
		for (int i = 0; i < string.size(); i++) {
			//String u = string.get(i);
			User u = string.get(i);
			System.out.println(u);
		}
		HibernateUtils.closeSession(session);// 关闭session
	}

打印结果:
在这里插入图片描述
(2)使用外置命名方式,查询(用户名为X明)的用户信息(模糊查询)

	public void getUser6() {
		// 获取session对象
		Session session = HibernateUtils.getSession();
		//查询(用户名为x明)的用户信息(模糊查询)
		@SuppressWarnings("unchecked")
		List<User> string = session.getNamedQuery
		("external_name2").setParameter("username","%明").list();
		
		if (string.size() != 0)
			System.out.println("成功检索所有用户");
		else
			System.out.println("没有该用户哦");
		for (int i = 0; i < string.size(); i++) {
			//String u = string.get(i);
			User u = string.get(i);
			System.out.println(u);
		}
		HibernateUtils.closeSession(session);// 关闭session
	}

打印结果:
在这里插入图片描述


虽然有点多,但技多不压身嘛。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值