手记

关于Hibernate和hql语句的非常实用的知识点

1.Hql基础查询
(1)查询Oracle数据库中某个表的信息总数(返回整型)

//首先获取Session对象
String hql="select count(*) from 表名";//定义hql语句
//接下来获取Query对象
Number num=(Number) query.uniqueResult();//强制转换,返回单个实例
int count=num.intValue();

(2) 使用Hql语句给参数赋值

//使用Query对象进行给参数赋值
query.setParameter(index,value);//index从0开始,表示给第一个?(参数)
//赋值,value表示要赋的值

关于HQL语句查询返回的结构类型

//(1)返回的是object类型
    public List<Object>  DoSelect(){
        Session session=this.getSession();
        Transaction ts=session.beginTransaction();
                String hql="select u.username from User u";//访问的是User对象
                Query query=session.createQuery(hql);
        List<Object>list=query.list();
        for(Object obj:list){

            System.out.println(obj);

        }
          ts.commit();
         session.close();
return list;
    }
//(2)返回的是数组类型
    public List<Object[]> dohql(){
        Session session=this.getSession();
        Transaction ts=session.beginTransaction();
        String hql="select u.username ,u.password from User u";//访问的是对象
        Query query=session.createQuery(hql);
        List<Object[]>list=query.list();
        for(Object[] obj:list){

            System.out.println("姓名为"+obj[0]+"密码为"+obj[1]);/         
        }
           ts.commit();
        session.close();
        return list;
}
//(3)返回的是list类型
    public  List<List> dohql1(){
        Session session=this.getSession();
        Transaction ts=session.beginTransaction();
      String hql="select new list(u.username ,u.password) from User u";//访问的是对象
       Query query=session.createQuery(hql);
        List<List> list=query.list();
        for(List obj:list){

            System.out.println("姓名为"+ obj.get(0)+"·密码为"+ obj.get(1));

        }
      ts.commit();
        session.close();
return  list;
    }
//(4)返回的是map类型
    public List<Map<String,Object>> dohql2(){
        Session session=this.getSession();
        Transaction ts=session.beginTransaction();
       String hql="select new map(u.username ,u.password) from User u";//访问的是对象
       Query query=session.createQuery(hql);
        List<Map<String,Object>> list=query.list();
       for(Map obj:list){

            System.out.println("姓名为"+ obj.get("0")+"密码为"+ obj.get("1"));

    }       ts.commit();
               session.close(); 
        return list;
    }
//(5)使用别名的Map 类型

    public List<Map<String,Object>> dohql3(){
        Session session=this.getSession();
        Transaction ts=session.beginTransaction();
        String hql="select new map(u.username as name,u.password  as pwd) from User u";
        Query query=session.createQuery(hql);
        List<Map<String,Object>> list=query.list();
         for(Map obj:list){

            System.out.println("姓名为"+ obj.get("name")+"密码为"+ obj.get("pwd"));

        }       ts.commit();        
                session.close();
        return list;
    }
    //(6)使用hibernate进行sql查询
        public List<Map<String,Object>> Querymid(BigDecimal rid){
        Session session=this.getSession();
        StringBuffer sql = new StringBuffer();
         sql.append(" SELECT mid");
         sql.append(" FROM menu_role mr");
        sql.append(" INNER JOIN ROLE r ON mr.rid = r.rid");
        sql.append(" WHERE r.RID = ?");//sql查询进行等值连接
        Query query = session.createSQLQuery(sql.toString());
         query.setBigDecimal(0, rid);//赋值
        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
         return query.list();
 }
//(7)当有多个参数传入时,需要进行字符串的连接
public List<Map<String,Object>> QueryLeftCmenu(BigDecimal rid) {
     List<Map<String,Object>> ll=Querymid(rid);
          String midStr = "";
         for(int i=0;i<ll.size();i++){
         if(i==ll.size()-1){//是最后一个值,不加逗号
             midStr = midStr + ll.get(i).get("MID").toString();
         }else{
             midStr = midStr + ll.get(i).get("MID").toString() + ",";
         }
     }
     Session session=this.getSession();
     String sql="select a.mid as zid,a.mname as zname,b.pid as cid,b.mname as cname,b.url from menu a,menu b where a.mid=b.pid and a.mid in ("+midStr+")";
     Query query=session.createSQLQuery(sql);
     query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
     return query.list();
  }         

Hibernate对数据库的一些操作

//1.向数据库插入数据
    public void DoInsert(){
        Session session=this.getSession();
        Transaction ts=session.beginTransaction();
         User user=new User();//实例化一个对象
         user.setUsername("lzk");//给属性赋值
         user.setPassword("123");
         session.save(user);//保存对象
         ts.commit();
         session.close();
        }
//(2)删除数据
public void DoDelete(){
        Session session=this.getSession();
        Transaction ts=session.beginTransaction();
        User user=new User();
        user.setId(3);//删除id为3的对象
        session.delete(user);//进行删除操作
         ts.commit();
          session.close();
    }
//(3)修改数据
    public void DoUpdate(){
        Session session=this.getSession();
        Transaction ts=session.beginTransaction();
        User user=(User)session.get(User.class, 2); //获取id为2的对象             user.setPassword("456");//进行修改
         ts.commit();
        session.close();

    }
//(4)使用hql语句进行Oracle的分页查询

public List<Object> QueryAllLaw(String everycount,
            String currentpage) {
         Session session=this.getSession();
         Transaction ts=session.beginTransaction();
         int a = Integer.parseInt(everycount);//  每页的显示的信息数
        int b = Integer.parseInt(currentpage);// 当前页
        int end = a * b;
        int begin = (b - 1) * a + 1;
       String hql="from  表名";
        Query query=session.createQuery(hql);
        query.setFirstResult(begin);
        query.setMaxResults(end);
        List<Object>list=query.list();
         ts.commit();
         session.close();
         return list;
         }
2人推荐
随时随地看视频
慕课网APP