JavaWeb_(Hibernate框架)Hibernate中数据查询语句SQL基本用法

本文展示三种在Hibernate中使用SQL语句进行数据查询基本用法

  1、基本查询

  2、条件查询

  3、分页查询

package com.Gary.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.NativeQuery; import com.Gary.domain.User;
import com.Gary.utils.HibernateUtils; public class SQLDao { public void find()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //开始进行SQL查询
String sql = "select * from user"; //创建sql查询对象
NativeQuery query = session.createSQLQuery(sql);
query.addEntity(User.class); //接收list
List<User> list = query.list();
//接收单一返回值 query.uniqyeResult(); //输出第一个成员姓名
System.out.println(list.get(0).getUsername());
//结束SQL查询 beginTransaction.commit();
session.close(); } //SQL条件查询
public void find2()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //开始进行SQL查询
String sql = "select * from user where id = ?";
//创建SQL查询对象
NativeQuery query = session.createSQLQuery(sql);
//封装参数,给第一个?赋值
query.setParameter(1, "1"); query.addEntity(User.class);
//唯一返回值
User user = (User) query.uniqueResult();
System.out.println(user);
//结束SQL查询 beginTransaction.commit();
session.close(); } //SQL分页查询
public void find3()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //开始进行SQL查询
String sql = "select * from user limit ? , ?";
//创建SQL查询对象
NativeQuery query = session.createSQLQuery(sql);
//封装参数,给第一个?赋值
query.setParameter(1, 0);
//同理
query.setParameter(2, 2);
query.addEntity(User.class);
//唯一返回值
List<User> list = query.list();
System.out.println(list.get(0).getUsername());
//结束SQL查询 beginTransaction.commit();
session.close(); } }

SQLDao.java

  数据库user表

JavaWeb_(Hibernate框架)Hibernate中数据查询语句SQL基本用法

  向数据库中添加假数据

  JavaWeb_(Hibernate框架)Hibernate中数据查询语句SQL基本用法

1、SQL基本查询

  //开始进行SQL查询
  String sql = "select * from user";   //创建sql查询对象
  NativeQuery query = session.createSQLQuery(sql);
  query.addEntity(User.class);

  SQLDao.java向数据库发起查询请求

public void find()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //开始进行SQL查询
String sql = "select * from user"; //创建sql查询对象
NativeQuery query = session.createSQLQuery(sql);
query.addEntity(User.class); //接收list
List<User> list = query.list();
//接收单一返回值 query.uniqyeResult(); //输出第一个成员姓名
System.out.println(list.get(0).getUsername());
//结束SQL查询 beginTransaction.commit();
session.close(); }

  JavaWeb_(Hibernate框架)Hibernate中数据查询语句SQL基本用法

package com.Gary.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.NativeQuery; import com.Gary.domain.User;
import com.Gary.utils.HibernateUtils; public class SQLDao { public void find()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //开始进行SQL查询
String sql = "select * from user"; //创建sql查询对象
NativeQuery query = session.createSQLQuery(sql);
query.addEntity(User.class); //接收list
List<User> list = query.list();
//接收单一返回值 query.uniqyeResult(); //输出第一个成员姓名
System.out.println(list.get(0).getUsername());
//结束SQL查询 beginTransaction.commit();
session.close(); } }

SQLDao.java

2、SQL条件查询

        //开始进行SQL查询
String sql = "select * from user where id = ?";
//创建SQL查询对象
NativeQuery query = session.createSQLQuery(sql);
//封装参数,给第一个?赋值
query.setParameter(1, "1"); query.addEntity(User.class);

  SQLDao.java向数据库发起查询请求

public void find2()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //开始进行SQL查询
String sql = "select * from user where id = ?";
//创建SQL查询对象
NativeQuery query = session.createSQLQuery(sql);
//封装参数,给第一个?赋值
query.setParameter(1, "1"); query.addEntity(User.class);
//唯一返回值
User user = (User) query.uniqueResult();
System.out.println(user);
//结束SQL查询 beginTransaction.commit();
session.close(); }

JavaWeb_(Hibernate框架)Hibernate中数据查询语句SQL基本用法

package com.Gary.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.NativeQuery; import com.Gary.domain.User;
import com.Gary.utils.HibernateUtils; public class SQLDao { public void find()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //开始进行SQL查询
String sql = "select * from user"; //创建sql查询对象
NativeQuery query = session.createSQLQuery(sql);
query.addEntity(User.class); //接收list
List<User> list = query.list();
//接收单一返回值 query.uniqyeResult(); //输出第一个成员姓名
System.out.println(list.get(0).getUsername());
//结束SQL查询 beginTransaction.commit();
session.close(); } public void find2()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //开始进行SQL查询
String sql = "select * from user where id = ?";
//创建SQL查询对象
NativeQuery query = session.createSQLQuery(sql);
//封装参数,给第一个?赋值
query.setParameter(1, "1"); query.addEntity(User.class);
//唯一返回值
User user = (User) query.uniqueResult();
System.out.println(user);
//结束SQL查询 beginTransaction.commit();
session.close(); } }

SQLDao.java

3、SQL分页查询

  //开始进行SQL查询
  String sql = "select * from user limit ? , ?";
  //创建SQL查询对象
  NativeQuery query = session.createSQLQuery(sql);
  //封装参数,给第一个?赋值
  query.setParameter(1, 0);
  //同理
  query.setParameter(2, 2);
  query.addEntity(User.class);
  //唯一返回值
  List<User> list = query.list();

  SQLDao.java向数据库发起查询请求

//SQL分页查询
public void find3()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //开始进行SQL查询
String sql = "select * from user limit ? , ?";
//创建SQL查询对象
NativeQuery query = session.createSQLQuery(sql);
//封装参数,给第一个?赋值
query.setParameter(1, 0);
//同理
query.setParameter(2, 2);
query.addEntity(User.class);
//唯一返回值
List<User> list = query.list();
System.out.println(list.get(0).getUsername());
//结束SQL查询 beginTransaction.commit();
session.close(); }

JavaWeb_(Hibernate框架)Hibernate中数据查询语句SQL基本用法

package com.Gary.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.NativeQuery; import com.Gary.domain.User;
import com.Gary.utils.HibernateUtils; public class SQLDao { public void find()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //开始进行SQL查询
String sql = "select * from user"; //创建sql查询对象
NativeQuery query = session.createSQLQuery(sql);
query.addEntity(User.class); //接收list
List<User> list = query.list();
//接收单一返回值 query.uniqyeResult(); //输出第一个成员姓名
System.out.println(list.get(0).getUsername());
//结束SQL查询 beginTransaction.commit();
session.close(); } //SQL条件查询
public void find2()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //开始进行SQL查询
String sql = "select * from user where id = ?";
//创建SQL查询对象
NativeQuery query = session.createSQLQuery(sql);
//封装参数,给第一个?赋值
query.setParameter(1, "1"); query.addEntity(User.class);
//唯一返回值
User user = (User) query.uniqueResult();
System.out.println(user);
//结束SQL查询 beginTransaction.commit();
session.close(); } //SQL分页查询
public void find3()
{
Session session = HibernateUtils.getSession();
Transaction beginTransaction = session.beginTransaction(); //开始进行SQL查询
String sql = "select * from user limit ? , ?";
//创建SQL查询对象
NativeQuery query = session.createSQLQuery(sql);
//封装参数,给第一个?赋值
query.setParameter(1, 0);
//同理
query.setParameter(2, 2);
query.addEntity(User.class);
//唯一返回值
List<User> list = query.list();
System.out.println(list.get(0).getUsername());
//结束SQL查询 beginTransaction.commit();
session.close(); } }

SQLDao.java

上一篇:hibernate中带查询条件的分页


下一篇:Hibernate中Criteria的用法