• 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表

      向数据库中添加假数据

      

    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();
            
        }

      

    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();
            
        }

    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();
            
        }

    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
  • 相关阅读:
    SQL Server数据库中批量替换数据的方法
    js 鼠标移上去弹出层效果
    50个jQuery代码段帮你成为更好的JavaScript开发者
    MSN、腾讯QQ、SKYPE、阿里旺旺网页在线客服源代码
    正则表达式基础知识
    常用邮箱POP3和SMTP服务器汇总
    CS5序列号
    如何使用Oracle SQLDeveloper 中连接MS SQLServer和MySQL数据库
    Silverlight与后台数据库的三种技术实现基本的互操作(转)
    ADO 与ADO.NET
  • 原文地址:https://www.cnblogs.com/1138720556Gary/p/11858664.html
Copyright © 2020-2023  润新知