zoukankan      html  css  js  c++  java
  • Hibernate-Criteria(查询)

    使用Criteria查询包括以下步骤:

    (1).使用Session接口的createCriteria()方法创建Criteria对象。

    (2).使用Restrictions类提供的静态方法设置查询条件,这些静态方法放回Criterion对象,一个Criterion对象代表一个查询条件。Criteria接口的add()方法用来添加查询条件。

    (3).使用Criteria接口的list()方法执行查询语句,list()方法返回java.util.List类型的结果,List集合中的每个元素都是持久化对象。

     1 package com.accp.test;
     2 
     3 import java.util.List;
     4 import org.hibernate.Criteria;
     5 import org.hibernate.Session;
     6 import org.hibernate.cfg.Configuration;
     7 import com.accp.entity.Emp;
     8 
     9 /**
    10  * Hibernate-Criteria查询
    11  * 
    12  * @author 孙洪雨
    13  */
    14 public class test {
    15     Configuration conf = null;
    16     Session session = null;
    17 
    18     /**
    19      * Criteria查询
    20      */
    21     public void show() {
    22         conf = new Configuration().configure();// 读取Hibernate配置文件
    23         session = conf.buildSessionFactory().openSession();// 打开Session
    24         Criteria criteria = session.createCriteria(Emp.class);// 查询Emp表
    25         List<Emp> list = criteria.list();
    26         for (Emp item : list) {
    27             // 使用for循环遍历所有用户信息
    28             System.out.println("姓名:" + item.getEname() + "	工资:" + item.getSal());
    29         }
    30     }
    31 
    32     public static void main(String[] args) {
    33         test t = new test();
    34         t.show();
    35     }
    36 
    37 }
    Criteria查询(无条件)

    使用Criteria接口的add()方法用来添加查询条件。

    --Criteria支持的比较运算.eq()等于 .ne()不等于 .gt()大于 .ge()大于等于 .lt()小于 .le()小于等于 .isNull()等于空值 isNotNull() 非空值

     1 package com.accp.test;
     2 
     3 import java.util.List;
     4 
     5 import org.hibernate.Criteria;
     6 import org.hibernate.Session;
     7 import org.hibernate.cfg.Configuration;
     8 import org.hibernate.criterion.Restrictions;
     9 
    10 import com.accp.entity.Emp;
    11 
    12 /**
    13  * Hibernate-Criteria查询
    14  * 
    15  * @author 孙洪雨
    16  */
    17 public class test {
    18     Configuration conf = null;
    19     Session session = null;
    20 
    21     /**
    22      * Criteria查询
    23      */
    24     public void show() {
    25         conf = new Configuration().configure();// 读取Hibernate配置文件
    26         session = conf.buildSessionFactory().openSession();// 打开Session
    27         Criteria criteria = session.createCriteria(Emp.class);// 查询Emp表
    28         criteria.add(Restrictions.eq("job", "SALESMAN"));//添加查询条件为(job等于SALESMAN)的员工
    29         List<Emp> list = criteria.list();
    30         for (Emp item : list) {
    31             // 使用for循环遍历(job等于SALESMAN)的员工用户信息
    32             System.out.println("姓名:" + item.getEname() + "	工资:" + item.getSal()+"	部门:"+item.getJob());
    33         }
    34     }
    35 
    36     public static void main(String[] args) {
    37         test t = new test();
    38         t.show();
    39     }
    40 
    41 }
    Criteria查询(带条件)

    --使用ignoreCase()方法忽略大小写。类型必须是字符型,其他会报错!

     1 package com.accp.test;
     2 
     3 import java.util.List;
     4 
     5 import org.hibernate.Criteria;
     6 import org.hibernate.Session;
     7 import org.hibernate.cfg.Configuration;
     8 import org.hibernate.criterion.Restrictions;
     9 
    10 import com.accp.entity.Emp;
    11 
    12 /**
    13  * Hibernate-Criteria查询
    14  * 
    15  * @author 孙洪雨
    16  */
    17 public class test {
    18     Configuration conf = null;
    19     Session session = null;
    20 
    21     /**
    22      * Criteria查询
    23      */
    24     public void show() {
    25         conf = new Configuration().configure();// 读取Hibernate配置文件
    26         session = conf.buildSessionFactory().openSession();// 打开Session
    27         Criteria criteria = session.createCriteria(Emp.class);// 查询Emp表
    28         criteria.add(Restrictions.eq("job", "SALESMAN").ignoreCase());//添加查询条件为(job等于SALESMAN(忽略大小写))的员工
    29         List<Emp> list = criteria.list();
    30         for (Emp item : list) {
    31             // 使用for循环遍历(job等于SALESMAN)的员工用户信息
    32             System.out.println("姓名:" + item.getEname() + "	工资:" + item.getSal()+"	部门:"+item.getJob());
    33         }
    34     }
    35 
    36     public static void main(String[] args) {
    37         test t = new test();
    38         t.show();
    39     }
    40 
    41 }
    Criteria查询(ignoreCase()方法使用)

    --Criteria支持的范围运算(in列表)、(not in列表)、between 值1 and 值2、 not between 值1 and 值2

     1 package com.accp.test;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 import org.hibernate.Criteria;
     7 import org.hibernate.Session;
     8 import org.hibernate.cfg.Configuration;
     9 import org.hibernate.criterion.Restrictions;
    10 
    11 import com.accp.entity.Emp;
    12 
    13 /**
    14  * Hibernate-Criteria查询
    15  * 
    16  * @author 孙洪雨
    17  */
    18 public class test {
    19     Configuration conf = null;
    20     Session session = null;
    21     /**
    22      * Criteria查询
    23      */
    24     public void show() {
    25         conf = new Configuration().configure();// 读取Hibernate配置文件
    26         session = conf.buildSessionFactory().openSession();// 打开Session
    27         Criteria criteria = session.createCriteria(Emp.class);// 查询Emp表
    28         List<Short>empnos=new ArrayList<Short>();
    29         empnos.add(new Short("7369"));
    30         empnos.add(new Short("7499"));
    31         empnos.add(new Short("7521"));
    32         criteria.add(Restrictions.in("empno", empnos));//查询empno在empnos列表中的员工信息
    33         List<Emp> list = criteria.list();
    34         for (Emp item : list) {
    35             // 使用for循环遍历(符合empnps列表)的员工用户信息
    36             System.out.println("姓名:" + item.getEname() + "	工资:" + item.getSal()+"	部门:"+item.getJob());
    37         }
    38     }
    39 
    40     public static void main(String[] args) {
    41         test t = new test();
    42         t.show();
    43     }
    44 
    45 }
    Criteria查询(in)
     1 package com.accp.test;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 import org.hibernate.Criteria;
     7 import org.hibernate.Session;
     8 import org.hibernate.cfg.Configuration;
     9 import org.hibernate.criterion.Restrictions;
    10 
    11 import com.accp.entity.Emp;
    12 
    13 /**
    14  * Hibernate-Criteria查询
    15  * 
    16  * @author 孙洪雨
    17  */
    18 public class test {
    19     Configuration conf = null;
    20     Session session = null;
    21     /**
    22      * Criteria查询
    23      */
    24     public void show() {
    25         conf = new Configuration().configure();// 读取Hibernate配置文件
    26         session = conf.buildSessionFactory().openSession();// 打开Session
    27         Criteria criteria = session.createCriteria(Emp.class);// 查询Emp表
    28         List<Short>empnos=new ArrayList<Short>();
    29         empnos.add(new Short("7369"));
    30         empnos.add(new Short("7499"));
    31         empnos.add(new Short("7521"));
    32         criteria.add(Restrictions.not(Restrictions.in("empno", empnos)) );//查询empno不在empnos列表中的员工信息
    33         List<Emp> list = criteria.list();
    34         for (Emp item : list) {
    35             // 使用for循环遍历(不符合empnps列表)的员工用户信息
    36             System.out.println("姓名:" + item.getEname() + "	工资:" + item.getSal()+"	部门:"+item.getJob());
    37         }
    38     }
    39 
    40     public static void main(String[] args) {
    41         test t = new test();
    42         t.show();
    43     }
    44 
    45 }
    Criteria查询(not in)
     1 package com.accp.test;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 import org.hibernate.Criteria;
     7 import org.hibernate.Session;
     8 import org.hibernate.cfg.Configuration;
     9 import org.hibernate.criterion.Restrictions;
    10 
    11 import com.accp.entity.Emp;
    12 
    13 /**
    14  * Hibernate-Criteria查询
    15  * 
    16  * @author 孙洪雨
    17  */
    18 public class test {
    19     Configuration conf = null;
    20     Session session = null;
    21     /**
    22      * Criteria查询
    23      */
    24     public void show() {
    25         conf = new Configuration().configure();// 读取Hibernate配置文件
    26         session = conf.buildSessionFactory().openSession();// 打开Session
    27         Criteria criteria = session.createCriteria(Emp.class);// 查询Emp表
    28         criteria.add(Restrictions.between("empno", new Short("7369"), new Short("7869")));//查询empno在7369-7869之间的员工信息
    29         List<Emp> list = criteria.list();
    30         for (Emp item : list) {
    31             // 使用for循环遍历(empno在7369-7869之间)的员工用户信息
    32             System.out.println("姓名:" + item.getEname() + "	工资:" + item.getSal()+"	部门:"+item.getJob()+"	编号:"+item.getEmpno());
    33         }
    34     }
    35 
    36     public static void main(String[] args) {
    37         test t = new test();
    38         t.show();
    39     }
    40 
    41 }
    Criteria查询(between 值1 and 值2)
     1 package com.accp.test;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 import org.hibernate.Criteria;
     7 import org.hibernate.Session;
     8 import org.hibernate.cfg.Configuration;
     9 import org.hibernate.criterion.Restrictions;
    10 
    11 import com.accp.entity.Emp;
    12 
    13 /**
    14  * Hibernate-Criteria查询
    15  * 
    16  * @author 孙洪雨
    17  */
    18 public class test {
    19     Configuration conf = null;
    20     Session session = null;
    21     /**
    22      * Criteria查询
    23      */
    24     public void show() {
    25         conf = new Configuration().configure();// 读取Hibernate配置文件
    26         session = conf.buildSessionFactory().openSession();// 打开Session
    27         Criteria criteria = session.createCriteria(Emp.class);// 查询Emp表
    28         criteria.add(Restrictions.not(Restrictions.between("empno", new Short("7369"), new Short("7869"))));//查询empno不在7369-7869之间的员工信息
    29         List<Emp> list = criteria.list();
    30         for (Emp item : list) {
    31             // 使用for循环遍历(不符合empno在7369-7869之间)的员工用户信息
    32             System.out.println("姓名:" + item.getEname() + "	工资:" + item.getSal()+"	部门:"+item.getJob()+"	编号:"+item.getEmpno());
    33         }
    34     }
    35 
    36     public static void main(String[] args) {
    37         test t = new test();
    38         t.show();
    39     }
    40 
    41 }
    Criteria查询(not between 值1 and 值2)

    --Criteria支持的字符串模式匹配 like、ilike(忽略大小写)

     1 package com.accp.test;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 import org.hibernate.Criteria;
     7 import org.hibernate.Session;
     8 import org.hibernate.cfg.Configuration;
     9 import org.hibernate.criterion.Restrictions;
    10 
    11 import com.accp.entity.Emp;
    12 
    13 /**
    14  * Hibernate-Criteria查询
    15  * 
    16  * @author 孙洪雨
    17  */
    18 public class test {
    19     Configuration conf = null;
    20     Session session = null;
    21     /**
    22      * Criteria查询
    23      */
    24     public void show() {
    25         conf = new Configuration().configure();// 读取Hibernate配置文件
    26         session = conf.buildSessionFactory().openSession();// 打开Session
    27         Criteria criteria = session.createCriteria(Emp.class);// 查询Emp表
    28         criteria.add(Restrictions.like("ename", "S%"));//查询ename以S开头的员工信息
    29         List<Emp> list = criteria.list();
    30         for (Emp item : list) {
    31             // 使用for循环遍历(ename以S开头的员工信息)的员工用户信息
    32             System.out.println("姓名:" + item.getEname() + "	工资:" + item.getSal()+"	部门:"+item.getJob()+"	编号:"+item.getEmpno());
    33         }
    34     }
    35 
    36     public static void main(String[] args) {
    37         test t = new test();
    38         t.show();
    39     }
    40 
    41 }
    Criteria查询(like)

    --Criteria支持的逻辑运算 and、or、not、disjunction

     1 package com.accp.test;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 import org.hibernate.Criteria;
     7 import org.hibernate.Session;
     8 import org.hibernate.cfg.Configuration;
     9 import org.hibernate.criterion.Restrictions;
    10 
    11 import com.accp.entity.Emp;
    12 
    13 /**
    14  * Hibernate-Criteria查询
    15  * 
    16  * @author 孙洪雨
    17  */
    18 public class test {
    19     Configuration conf = null;
    20     Session session = null;
    21     /**
    22      * Criteria查询
    23      */
    24     public void show() {
    25         conf = new Configuration().configure();// 读取Hibernate配置文件
    26         session = conf.buildSessionFactory().openSession();// 打开Session
    27         Criteria criteria = session.createCriteria(Emp.class);// 查询Emp表
    28         criteria.add(Restrictions.disjunction().add(Restrictions.like("ename", "S%")).add(Restrictions.ge("sal", 3000D)).add(Restrictions.eq("job", "MANAGER")));//查询ename以S开头或者工资高于等于3000或部门为MANAGER的员工信息
    29         List<Emp> list = criteria.list();
    30     
    31         for (Emp item : list) {
    32             // 使用for循环遍历(ename以S开头或者工资高于等于3000或部门为MANAGER)的员工用户信息
    33             System.out.println("姓名:" + item.getEname() + "	工资:" + item.getSal()+"	部门:"+item.getJob()+"	编号:"+item.getEmpno());
    34         }
    35     }
    36 
    37     public static void main(String[] args) {
    38         test t = new test();
    39         t.show();
    40     }
    41 
    42 }
    Criteria查询(disjunction)

    --Criteria支持的集合运算 isEmpty()集合为空、isNotEmpty()集合不为空

     1 package com.accp.test;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 import org.hibernate.Criteria;
     7 import org.hibernate.Session;
     8 import org.hibernate.cfg.Configuration;
     9 import org.hibernate.criterion.Restrictions;
    10 
    11 import com.accp.entity.Dept;
    12 import com.accp.entity.Emp;
    13 
    14 /**
    15  * Hibernate-Criteria查询
    16  * 
    17  * @author 孙洪雨
    18  */
    19 public class test {
    20     Configuration conf = null;
    21     Session session = null;
    22     /**
    23      * Criteria查询
    24      */
    25     public void show() {
    26         conf = new Configuration().configure();// 读取Hibernate配置文件
    27         session = conf.buildSessionFactory().openSession();// 打开Session
    28         Criteria criteria = session.createCriteria(Dept.class);// 查询Dept表
    29         criteria.add(Restrictions.isEmpty("emps"));//查询没有员工的部门信息
    30         List<Dept> list = criteria.list();
    31     
    32         for (Dept item : list) {
    33             // 使用for循环遍历(没有员工的部门)的信息
    34             System.out.println("部门:" + item.getDname() +"	部门编号:"+item.getDeptno());
    35         }
    36     }
    37 
    38     public static void main(String[] args) {
    39         test t = new test();
    40         t.show();
    41     }
    42 
    43 }
    Criteria查询(isEmpty)

    Criteria 动态查询

     1 package com.accp.test;
     2 
     3 import java.text.ParseException;
     4 import java.text.SimpleDateFormat;
     5 import java.util.ArrayList;
     6 import java.util.Date;
     7 import java.util.List;
     8 
     9 import org.hibernate.Criteria;
    10 import org.hibernate.HibernateException;
    11 import org.hibernate.Session;
    12 import org.hibernate.cfg.Configuration;
    13 import org.hibernate.criterion.Restrictions;
    14 
    15 import com.accp.entity.Dept;
    16 import com.accp.entity.Emp;
    17 import com.accp.entity.EmpCondition;
    18 
    19 /**
    20  * Hibernate-Criteria查询
    21  * 
    22  * @author 孙洪雨
    23  */
    24 public class test {
    25     Configuration conf = null;
    26     Session session = null;
    27 
    28     /**
    29      * Criteria查询
    30      */
    31     public void show() {
    32         try {
    33             conf = new Configuration().configure();// 读取Hibernate配置文件
    34             session = conf.buildSessionFactory().openSession();// 打开Session
    35             // 准备查询条件,empCondition对象封装条件
    36             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    37             Date dateStart = sdf.parse("1976-12-31");
    38             Date dateEnd = sdf.parse("2008-12-31");
    39             EmpCondition empCondition = new EmpCondition();
    40             empCondition.setJob("SALESMAN");// 设置查询职位条件
    41             empCondition.setSal(1000D);// 设置查询工资条件
    42             empCondition.setHireDateStart(dateStart);// 设置开始时间1976年12月31日
    43             empCondition.setHireDateEnd(dateEnd);// 设置结束时间2008年12月31日
    44             Criteria criteria = session.createCriteria(Emp.class);// 查询Emp表
    45             if (empCondition.getJob() != null) {
    46                 criteria.add(Restrictions.eq("job", empCondition.getJob()));
    47             }
    48             if (empCondition.getSal() != null) {
    49                 criteria.add(Restrictions.ge("sal", empCondition.getSal()));
    50             }
    51             if (empCondition.getHireDateStart() != null) {
    52                 criteria.add(Restrictions.ge("hiredate",
    53                         empCondition.getHireDateStart()));
    54             }
    55             if (empCondition.getHireDateEnd() != null) {
    56                 criteria.add(Restrictions.le("hiredate",
    57                         empCondition.getHireDateEnd()));
    58             }
    59             List<Emp> list = criteria.list();
    60 
    61             for (Emp item : list) {
    62                 // 使用for循环遍历(满足条件)的员工信息
    63                 System.out.println("姓名:" + item.getEname()+"	部门:"+item.getJob()+"	工资:"+item.getSal());
    64             }
    65         } catch (HibernateException e) {
    66             // TODO Auto-generated catch block
    67             e.printStackTrace();
    68         } catch (ParseException e) {
    69             // TODO Auto-generated catch block
    70             e.printStackTrace();
    71         }
    72     }
    73 
    74     public static void main(String[] args) {
    75         test t = new test();
    76         t.show();
    77     }
    78 
    79 }
    Criteria 动态查询

    Criteria 分页查询  

     1 package com.accp.test;
     2 
     3 import java.text.ParseException;
     4 import java.text.SimpleDateFormat;
     5 import java.util.ArrayList;
     6 import java.util.Date;
     7 import java.util.List;
     8 
     9 import org.hibernate.Criteria;
    10 import org.hibernate.HibernateException;
    11 import org.hibernate.Session;
    12 import org.hibernate.cfg.Configuration;
    13 import org.hibernate.criterion.Order;
    14 import org.hibernate.criterion.Restrictions;
    15 
    16 import com.accp.entity.Dept;
    17 import com.accp.entity.Emp;
    18 import com.accp.entity.EmpCondition;
    19 
    20 /**
    21  * Hibernate-Criteria查询
    22  * 
    23  * @author 孙洪雨
    24  */
    25 public class test {
    26     Configuration conf = null;
    27     Session session = null;
    28 
    29     /**
    30      * Criteria查询
    31      */
    32     public void show() {
    33             conf = new Configuration().configure();// 读取Hibernate配置文件
    34             session = conf.buildSessionFactory().openSession();// 打开Session
    35             Criteria criteria = session.createCriteria(Emp.class);// 查询Emp表
    36             criteria.setFirstResult(0);//按照数据库中的数据从第一个开始
    37             criteria.setMaxResults(5);//最大显示5条信息
    38             List<Emp> list = criteria.list();
    39             for (Emp item : list) {
    40                 // 使用for循环遍历(满足条件)的员工信息
    41                 System.out.println("姓名:" + item.getEname()+"	部门:"+item.getJob()+"	工资:"+item.getSal());
    42             }
    43     }
    44 
    45     public static void main(String[] args) {
    46         test t = new test();
    47         t.show();
    48     }
    49 
    50 }
    Criteria 分页

    Criteria 查询唯一对象

     1 package com.accp.test;
     2 
     3 import org.hibernate.Criteria;
     4 import org.hibernate.Session;
     5 import org.hibernate.cfg.Configuration;
     6 import org.hibernate.criterion.Projections;
     7 import com.accp.entity.Emp;
     8 
     9 /**
    10  * Hibernate-Criteria查询
    11  * 
    12  * @author 孙洪雨
    13  */
    14 public class test {
    15     Configuration conf = null;
    16     Session session = null;
    17 
    18     /**
    19      * Criteria查询
    20      */
    21     public void show() {
    22         conf = new Configuration().configure();// 读取Hibernate配置文件
    23         session = conf.buildSessionFactory().openSession();// 打开Session
    24         Criteria criteria = session.createCriteria(Emp.class);// 查询Emp表
    25         criteria.setProjection(Projections.count("id"));
    26         int count = (int) criteria.uniqueResult();
    27         System.out.println("总人数:" + count + "人");
    28     }
    29 
    30     public static void main(String[] args) {
    31         test t = new test();
    32         t.show();
    33     }
    34 
    35 } 
    Criteria(返回唯一值)

    Criteria  连接查询

    --Criteria 只支持内连接和迫切左外连接 Criteria 接口提供提供了createCriteria()和createAlias()方法建立内连接

     1 package com.accp.test;
     2 
     3 import java.util.List;
     4 
     5 import org.hibernate.Criteria;
     6 import org.hibernate.Session;
     7 import org.hibernate.cfg.Configuration;
     8 import org.hibernate.criterion.DetachedCriteria;
     9 import org.hibernate.criterion.MatchMode;
    10 import org.hibernate.criterion.Restrictions;
    11 
    12 import com.accp.entity.Emp;
    13 
    14 /**
    15  * Hibernate-Criteria查询
    16  * 
    17  * @author 孙洪雨
    18  */
    19 public class test {
    20     Configuration conf = null;
    21     Session session = null;
    22 
    23     /**
    24      * Criteria 链接查询 
    25      */
    26     public void show() {
    27         conf = new Configuration().configure();// 读取Hibernate配置文件
    28         session = conf.buildSessionFactory().openSession();// 打开Session
    29         Criteria critera=session.createCriteria(Emp.class);
    30         critera.createCriteria("dept");
    31         List<Emp>list=critera.list();
    32         for(Emp item:list){
    33             System.out.println("姓名:"+item.getEname()+"	部门名称:"+item.getDept().getDname());
    34         }
    35     }
    36 
    37     public static void main(String[] args) {
    38         test t = new test();
    39         t.show();
    40     }
    41 
    42 } 
    Criteria(连接查询)
     1 package com.accp.test;
     2 
     3 import java.util.List; import org.hibernate.Criteria;
     4 import org.hibernate.Session;
     5 import org.hibernate.cfg.Configuration;
     6 import org.hibernate.criterion.DetachedCriteria;
     7 import org.hibernate.criterion.MatchMode;
     8 import org.hibernate.criterion.Restrictions;
     9 
    10 import com.accp.entity.Emp;
    11 
    12 /**
    13 * Hibernate-Criteria查询
    14 * 
    15 * @author 孙洪雨
    16 */
    17 public class test {
    18     Configuration conf = null;
    19     Session session = null;
    20 
    21     /**
    22      * Criteria 链接查询 
    23      */
    24     public void show() {
    25         conf = new Configuration().configure();// 读取Hibernate配置文件
    26         session = conf.buildSessionFactory().openSession();// 打开Session
    27         Criteria critera=session.createCriteria(Emp.class,"e");
    28         critera.createAlias("e.dept", "d").add(Restrictions.eq("d.loc", "西一区"));//使用连接查询查找部门在西一区的员工和部门信息
    29         List<Emp>list=critera.list();
    30         for(Emp item:list){
    31         System.out.println("姓名:"+item.getEname()+"	部门名称:"+item.getDept().getDname());
    32         }
    33     }
    34 
    35     public static void main(String[] args) {
    36         test t = new test();
    37         t.show();
    38     }
    39 
    40 } 
    Criteria(createAlias连接查询)

    --Criteria 迫切左外连接

     1 package com.accp.test;
     2 
     3 import java.util.List;
     4 
     5 import org.hibernate.Criteria;
     6 import org.hibernate.FetchMode;
     7 import org.hibernate.Session;
     8 import org.hibernate.cfg.Configuration;
     9 import org.hibernate.criterion.DetachedCriteria;
    10 import org.hibernate.criterion.MatchMode;
    11 import org.hibernate.criterion.Restrictions;
    12 
    13 import com.accp.entity.Dept;
    14 import com.accp.entity.Emp;
    15 
    16 /**
    17  * Hibernate-Criteria查询
    18  * 
    19  * @author 孙洪雨
    20  */
    21 public class test {
    22     Configuration conf = null;
    23     Session session = null;
    24 
    25     /**
    26      * Criteria 链接查询 
    27      */
    28     public void show() {
    29         conf = new Configuration().configure();// 读取Hibernate配置文件
    30         session = conf.buildSessionFactory().openSession();// 打开Session
    31         Criteria critera=session.createCriteria(Dept.class,"d");
    32         critera.setFetchMode("emps", FetchMode.JOIN).add(Restrictions.eq("d.loc", "西一区"));
    33         List<Dept>list=critera.list();
    34         for(Dept item:list){
    35             System.out.println("部门名称:"+item.getDname()+"	地址:"+item.getLoc()+"	员工人数"+item.getEmps().size());
    36         }
    37     }
    38 
    39     public static void main(String[] args) {
    40         test t = new test();
    41         t.show();
    42     }
    43 
    44 } 
    Criteria(有重复)
     1 package com.accp.test;
     2 
     3 import java.util.HashSet;
     4 import java.util.List;
     5 
     6 import org.hibernate.Criteria;
     7 import org.hibernate.FetchMode;
     8 import org.hibernate.Session;
     9 import org.hibernate.cfg.Configuration;
    10 import org.hibernate.criterion.DetachedCriteria;
    11 import org.hibernate.criterion.MatchMode;
    12 import org.hibernate.criterion.Restrictions;
    13 
    14 import com.accp.entity.Dept;
    15 import com.accp.entity.Emp;
    16 
    17 /**
    18  * Hibernate-Criteria查询
    19  * 
    20  * @author 孙洪雨
    21  */
    22 public class test {
    23     Configuration conf = null;
    24     Session session = null;
    25 
    26     /**
    27      * Criteria 链接查询 
    28      */
    29     public void show() {
    30         conf = new Configuration().configure();// 读取Hibernate配置文件
    31         session = conf.buildSessionFactory().openSession();// 打开Session
    32         Criteria critera=session.createCriteria(Dept.class,"d");
    33         critera.setFetchMode("emps", FetchMode.JOIN).add(Restrictions.eq("d.loc", "西一区"));
    34         List<Dept>list=critera.list();
    35         HashSet<Dept>set=new HashSet<Dept>(list);
    36         for(Dept item:set){
    37             System.out.println("部门名称:"+item.getDname()+"	地址:"+item.getLoc()+"	员工人数"+item.getEmps().size());
    38         }
    39     }
    40 
    41     public static void main(String[] args) {
    42         test t = new test();
    43         t.show();
    44     }
    45 
    46 } 
    Criteria(去重复)

    Criteria 投影、分组

    --Hibernate提供了org.hibernate.criterion.Projection接口和org.hibernate.criterion.Projections类支持Criteria投影。

     1 package com.accp.test;
     2 
     3 import java.util.HashSet;
     4 import java.util.List;
     5 
     6 import org.hibernate.Criteria;
     7 import org.hibernate.FetchMode;
     8 import org.hibernate.Session;
     9 import org.hibernate.cfg.Configuration;
    10 import org.hibernate.criterion.Projection;
    11 import org.hibernate.criterion.Property;
    12 import org.hibernate.criterion.Restrictions;
    13 
    14 import com.accp.entity.Dept;
    15 import com.accp.entity.Emp;
    16 
    17 /**
    18  * Hibernate-Criteria查询
    19  * 
    20  * @author 孙洪雨
    21  */
    22 public class test {
    23     Configuration conf = null;
    24     Session session = null;
    25 
    26     /**
    27      * Criteria查询 投影
    28      */
    29     public void show() {
    30         conf = new Configuration().configure();// 读取Hibernate配置文件
    31         session = conf.buildSessionFactory().openSession();// 打开Session
    32         Criteria criteria = session.createCriteria(Dept.class);
    33         criteria.setProjection(Property.forName("dname"));
    34         List<String>list=criteria.list();
    35         for(String item:list){
    36             System.out.println("部门名称:"+item);
    37         }
    38     }
    39 
    40     public static void main(String[] args) {
    41         test t = new test();
    42         t.show();
    43     }
    44 
    45 } 
    Criteria(投影)
     1 package com.accp.test;
     2 
     3 import java.util.List;
     4 
     5 import org.hibernate.Criteria;
     6 import org.hibernate.Session;
     7 import org.hibernate.cfg.Configuration;
     8 import org.hibernate.criterion.Projection;
     9 import org.hibernate.criterion.ProjectionList;
    10 import org.hibernate.criterion.Projections;
    11 import org.hibernate.criterion.Property;
    12 
    13 import com.accp.entity.Dept;
    14 import com.accp.entity.Emp;
    15 
    16 /**
    17  * Hibernate-Criteria查询
    18  * 
    19  * @author 孙洪雨
    20  */
    21 public class test {
    22     Configuration conf = null;
    23     Session session = null;
    24 
    25     /**
    26      * Criteria查询 投影
    27      */
    28     public void show() {
    29         conf = new Configuration().configure();// 读取Hibernate配置文件
    30         session = conf.buildSessionFactory().openSession();// 打开Session
    31         Criteria criteria = session.createCriteria(Emp.class);
    32         criteria.setProjection(Projections.projectionList().add(Property.forName("ename")).add(Property.forName("hiredate")));
    33         List<Object[]>list=criteria.list();
    34         for(Object[] item:list){
    35             System.out.println("部门名称:"+item[0]+"	入职时间:"+item[1]);
    36         }
    37     }
    38 
    39     public static void main(String[] args) {
    40         test t = new test();
    41         t.show();
    42     }
    43 
    44 } 
    Criteria(分组)

    使用DetachedCriteria

     1 package com.accp.test;
     2 
     3 import java.util.List;
     4 
     5 import org.hibernate.Criteria;
     6 import org.hibernate.Session;
     7 import org.hibernate.cfg.Configuration;
     8 import org.hibernate.criterion.DetachedCriteria;
     9 import org.hibernate.criterion.MatchMode;
    10 import org.hibernate.criterion.Projection;
    11 import org.hibernate.criterion.ProjectionList;
    12 import org.hibernate.criterion.Projections;
    13 import org.hibernate.criterion.Property;
    14 import org.hibernate.criterion.Restrictions;
    15 
    16 import com.accp.entity.Dept;
    17 import com.accp.entity.Emp;
    18 
    19 /**
    20  * Hibernate-Criteria查询
    21  * 
    22  * @author 孙洪雨
    23  */
    24 public class test {
    25     Configuration conf = null;
    26     Session session = null;
    27 
    28     /**
    29      * Criteria查询 投影
    30      */
    31     public void show() {
    32         conf = new Configuration().configure();// 读取Hibernate配置文件
    33         session = conf.buildSessionFactory().openSession();// 打开Session
    34         DetachedCriteria dCriteria=DetachedCriteria.forClass(Emp.class,"e");
    35         dCriteria.createAlias("e.dept","d" ).add(Restrictions.eq("d.dname","shy")).add(Restrictions.ilike("e.ename","a",MatchMode.ANYWHERE));
    36         List<Emp>list=dCriteria.getExecutableCriteria(session).list();
    37         for(Emp item:list){
    38             System.out.println("姓名:"+item.getEname()+"	部门名称:"+item.getDept().getDname());
    39         }
    40     }
    41 
    42     public static void main(String[] args) {
    43         test t = new test();
    44         t.show();
    45     }
    46 
    47 } 
    DetachedCriteria(连接查询)
  • 相关阅读:
    Spring Batch 之 Sample(XML文件操作)(五)
    Spring Batch 之 Spring Batch 简介(一)
    Spring Batch 之 Sample(固定长格式文件读写)(六)
    Spring Batch 之 Sample(复合格式文件的读、多文件的写)(七)
    bat调用jar包的两个典型问题
    Spring Batch 之 Sample(Hello World)(三)
    开园大吉
    js中createElement方法的兼容性
    Struts2中关于"There is no Action mapped for namespace / and action name"的总结
    Spring Batch 之 框架流程简单介绍(二)
  • 原文地址:https://www.cnblogs.com/sunhongyu/p/3654397.html
Copyright © 2011-2022 走看看