二 Hibernate HQL实现学生管理系统的CRUD

Posted  Island

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二 Hibernate HQL实现学生管理系统的CRUD相关的知识,希望对你有一定的参考价值。

package com.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.dao.StudentDao;
import com.domain.Student;
import com.util.HibernateUtils;
import com.util.JDBCUtil02;
import com.util.TextUtils;
/**
 * 这是StudentDao的实现,针对前面定义的规范做出的具体实现
 *
 */
public class StudentDaoImpl implements StudentDao {
   
    @Override
    /**
     * 查询所有学生
     * @throws SQLException 
     */
    public List<Student> findAll() throws SQLException {
       Session session = HibernateUtils.oppenSession();
       Transaction transaction = session.beginTransaction();
       //HQL查询,面向对象的查询语言,from Student
       Query query = session.createQuery("from Student");
       List<Student> list =  query.list();
       transaction.commit();     
       session.close();
       return list;
       /*    QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
        return runner.query("select * from students",new BeanListHandler<Student>(Student.class));    
    */}
    
    @Override
    /**
     * 需要添加到数据库的学生
     */
    public void insert(Student student) throws SQLException {
    /*    QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
         runner.update("insert into students values(null,?,?,?,?,?,?)",
                student.getSname(),
                student.getGender(),
                student.getPhone(),
                student.getBirthday(),
                student.getHobby(),
                student.getInfo()
                 ); */
           Session session = HibernateUtils.oppenSession();
           Transaction transaction = session.beginTransaction();
           //HQL查询,面向对象的查询语言
           session.save(student);
//           Query query = session.createQuery("insert into Student values(null,student.getSname(),student.getGender(),student.getPhone(),student.getBirthday(),student.getHobby(),student.getInfo())");
           transaction.commit();     
           session.close();
    }
    
    @Override
    /**
     * 根据id删除学生
     */
    public void delete(int sid) throws SQLException {
    /*    QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
        runner.update("delete from students where sid = ?" , sid);*/
           Session session = HibernateUtils.oppenSession();
           Transaction transaction = session.beginTransaction();
           //HQL查询,面向对象的查询语言
           Student student = session.get(Student.class, sid);
           session.delete(student);
//           Query query = session.createQuery("insert into Student values(null,student.getSname(),student.getGender(),student.getPhone(),student.getBirthday(),student.getHobby(),student.getInfo())");
           transaction.commit();     
           session.close();
    }
    /**
     * 根据id查询单个学生
     * @throws SQLException 
     */
    @Override
    public Student findStudentById(int sid) throws SQLException {
    /*    QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
        return runner.query("select * from students where sid = ?", new BeanHandler<Student>(Student.class) , sid);
    */
           Session session = HibernateUtils.oppenSession();
           Transaction transaction = session.beginTransaction();
           //HQL查询,面向对象的查询语言
           Student student = session.get(Student.class, sid);
           transaction.commit();     
           session.close();    
           return student;
    }
    @Override
    /**
     * 更新学生到数据库
     */
    public void Update(Student stu, int sid) throws SQLException {
/*        QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
        runner.update("update students set sname=?,gender=?,phone=?,birthday=?,hobby=?,info=? where sid = ? ", 
                stu.getSname(),
                stu.getGender(),
                stu.getPhone(),
                stu.getBirthday(),
                stu.getHobby(),
                stu.getInfo(),
                sid
                );*/
     Session session = HibernateUtils.oppenSession();
     Transaction transaction = session.beginTransaction();
     stu.setSid(sid);
     session.saveOrUpdate(stu);
     transaction.commit();
     session.close();
    }
    
    @Override
    /**
     * 模糊查询的DAO层实现
     */
    public List<Student> SearchStident(String sname, String sgender) throws SQLException {
        QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
        /*
         * select * from stu where sname like ? and gender = ?
         * 
         * 如果两个都没有就查询所有
         */
        List<String> list = new ArrayList<String>();
        String sql = "select * from students where 1 = 1 ";
        if(!TextUtils.isEmpty(sname)){
            sql = sql + " and sname like ? ";
            list.add("%"+sname+"%");
        }
        if(!TextUtils.isEmpty(sgender)){
            sql = sql + " and gender = ? ";
            list.add(sgender);
        }
         return runner.query(sql, new BeanListHandler<Student>(Student.class),list.toArray());        
    }

    @Override
    /**
     * 分页查询,查询当前页的学生数据并封装成集合
     */
    public List<Student> findStudentByPage(int currentPage) throws SQLException {
        QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
        //第一个?返回记录数量,第二个?偏移量
        //第n页 :  5,5*n-1
        return runner.query("select * from students limit ? offset ?",new BeanListHandler<Student>(Student.class),PAGE_SIZE,PAGE_SIZE*(currentPage-1));    
    }

    @Override
    /**
     * 查询学生记录数
     */
    public int findCount() throws SQLException {
        QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
        //用于处理平均值,总个数
        Long result = (Long)runner.query("select count(*) from students", new ScalarHandler());    
        return result.intValue();
    }
    
    
}

 

以上是关于二 Hibernate HQL实现学生管理系统的CRUD的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate学习笔记 — HQL查询

休眠错误-org.hibernate.hql.internal.ast.QuerySyntaxException

hibernate hql

hibernate 一对多update操作的hql语句怎么写?

Hibernate(十四):HQL查询

Hibernate:基于HQL实现数据查询