码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2019-01-22 10:57:39      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:sele   imp   override   span   gets   upd   crud   create   pen   

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

标签:sele   imp   override   span   gets   upd   crud   create   pen   

原文地址:https://www.cnblogs.com/ltfxy/p/10301906.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!