//HQL查询
// auto-import要设置true,如果是false,写HQL时要指定类的全名
//查询全部列
Query query = session.createQuery("from Dept");
System.out.println(query.list()); //查询指定列
Query query = session.createQuery("select d.depId,d.depName from Dept d");
System.out.println(query.list()); //查询指定列,并封装为对象,必须提供带参数的构造器
Query query = session.createQuery("select new Dept(d.depId,d.depName) from Dept d");
System.out.println(query.list()); //条件查询 一个条件、多个条件、and or between 模糊查询
Query query = session.createQuery("from Dept d where depName=?");
query.setString(0,"人事部");
query.setParameter(0,"人事部");
//条件查询,命名参数
Query query = session.createQuery("from Dept d where depId =:myid and depName=:name");
query.setParameter("myid",1);
query.setParameter("name","人事部");
//条件查询,between and查询
Query query = session.createQuery("from Dept d where depId between ? and ?");
query.setParameter(0,1);
query.setParameter(1,5);
//模糊查询
Query query = session.createQuery("from Dept d where depName like ?");
query.setParameter(0,"%部%");
System.out.println(query.list()); //集合函数统计,不支持count(1),uniqueResult()返回结果的第一行第一列中的值
Query query = session.createQuery("select count(*) from Dept");
System.out.println(query.uniqueResult()); //分组查询,查询employ表,统计每个部门的人数
Query query = session.createQuery("select e.dept, count(*) from Employee e group by e.dept");
//统计部门人数大于1的部门
Query query = session.createQuery("select e.dept, count(*) from Employee e group by e.dept having count(*) >1");
System.out.println(query.list()); //连接查询,内连接、左外连接、右外连接
//内连接,映射已经配置好了关系,关联的时候直接写对象的属性即可
//返回员工和部门组成的对象数组,写在前面的元素在结果集中也在前面
// SELECT e.empName, e.salary, d.deptName FROM t_employee e INNER JOIN t_dept d ON d.depId = e.dept_id
Query query = session.createQuery("select e.empName,e.salary,e.dept.depName from Employee e inner join e.dept");
List<Object[]> list = query.list();
for(int i=0;i<list.size();i++){
Object[] obj = list.get(i);
Employee employee = (Employee) obj[0];
Dept dept = (Dept) obj[1];
System.out.println(employee);
System.out.println(dept);
}
System.out.println(query.list());
//左外连接
// SELECT e.empName, e.salary, d.deptName FROM t_employee e LEFT JOIN t_dept d ON d.depId = e.dept_id
Query query = session.createQuery(" from Employee e left join e.dept");
List<Object[]> list = query.list();
for(int i=0;i<list.size();i++){
Object[] obj = list.get(i);
Employee employee = (Employee) obj[0];
Dept dept = (Dept) obj[1];
System.out.println(employee);
System.out.println(dept);
}//迫切内连接,将右表的对象填充到左表当中
Query query = session.createQuery("from Dept d inner join fetch d.emps");

将HQL查询语句放到映射文件中:

    <query name="getDept">
from Dept d where depName=?
</query>
<query name="getDept2">
<![CDATA[
from Dept d where depId < ?
]]>
</query>

使用查询语句

        Query query = session.getNamedQuery("getDept");
query.setParameter(0,"人事部");
Query query = session.getNamedQuery("getDept2");
query.setParameter(0, 4);
System.out.println(query.list());

使用c3p0连接池

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<!-- 通常,一个session-factory节点代表一个数据库 -->
<session-factory> <!-- 1. 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">juaner767</property>
<!-- 【连接池配置】 -->
<!--
数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 2. 其他相关配置 -->
<!-- 2.1 显示hibernate在运行时候执行的sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 2.2 格式化sql
<property name="hibernate.format_sql">true</property> -->
<!-- 2.3 自动建表 -->
<property name="hibernate.hbm2ddl.auto">update</property> <!-- 配置连接驱动管理类 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- 配置连接池参数信息 -->
<property name="hibernate.c3p0.min_size">2</property>
<property name="hibernate.c3p0.max_size">4</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.max_statements">10</property>
<property name="hibernate.c3p0.idle_test_period">30000</property>
<property name="hibernate.c3p0.acquire_increment">2</property> <!-- 3. 加载所有映射 -->
<mapping resource="com/juaner/hibernate/address/User.hbm.xml"/>
<!--<mapping resource="com/juaner/hibernate/department/Dept.hbm.xml"/>-->
<!--<mapping resource="com/juaner/hibernate/department/Employee.hbm.xml"/>-->
<!--<mapping resource="com/juaner/hibernate/project/Developer.hbm.xml"/>-->
<!--<mapping resource="com/juaner/hibernate/project/Project.hbm.xml"/>-->
<!--<mapping resource="com/juaner/hibernate/extendMap/Animal.hbm.xml"/>-->
</session-factory>
</hibernate-configuration>

HQL查询及Hibernate对c3p0连接池的支持的更多相关文章

  1. Hibernate配置C3P0连接池

    引入C3PO包 在hibernate.cfg.xml文件中配置 <!-- 数据库连接池的使用 --> <!-- 选择使用C3P0连接池 --> <property nam ...

  2. Hibernate -- 配置c3p0连接池, 事务隔离级别, 管理session

    知识点1:配置c3p0连接池(了解) * 引入c3p0-0.9.1.jar * 在hibernate.cfg.xml文件中增加如下配置 <!-- C3P0连接池设定--> <!-- ...

  3. (30)java web的hibernate使用-c3p0连接池配置

    hibernate支持c3p0连接池 需要导入c3p0的jar包 <!-- 配置连接驱动管理类 --> <property name="hibernate.connecti ...

  4. Hibernate 配置C3P0 连接池

    第一步在hibernate.cfg.xml配置 <!-- 连接池 --> <property name="hibernate.connection.provider_cla ...

  5. Hibernate的查询,二级缓存,连接池

    Hibernate的查询,二级缓存,连接池 1.Hibernate查询数据 Hibernate中的查询方法有5中: 1.1.Get/Load主键查询 使用get或者load方法来查询,两者之间的区别在 ...

  6. hibernate对连接池的支持和HQL查询

    hibernate对连接池的支持 连接池, 作用: 管理连接:提升连接的利用效率! 常用的连接池: C3P0连接池 Hibernate 自带的也有一个连接池,且对C3P0连接池也有支持! 只维护一个连 ...

  7. hibernate 查询、二级缓存、连接池

    hibernate 查询.二级缓存.连接池 查询: 1) 主键查询 Dept dept =  (Dept) session.get(Dept.class, 12); Dept dept =  (Dep ...

  8. C3P0连接池在hibernate和spring中的配置

    首先为什么要使用连接池及为什么要选择C3P0连接池,这里就不多说了,目前C3P0连接池还是比较方便.比较稳定的连接池,能与spring.hibernate等开源框架进行整合. 一.hibernate中 ...

  9. Hibernate的配置中,c3p0连接池相关配置

    一.配置c3p0 1.导入 hibernate-c3po连接池包,Maven地址是:http://mvnrepository.com/artifact/org.hibernate/hibernate- ...

随机推荐

  1. PHP 注册树模式

    /** * 注册树模式 * 将对象注册到一个类中 * 通过该类实现全局访问操作对象 */ class Tree { private static $treeList = []; private fun ...

  2. 如何在Mac OSX上安装xgboost

    听说xgboost效果很不错,于是准备学习下,但是发现大多数资料都是在讲如何在windows或linux下安装xgboost,而且照着官方文档也没有正确的安装好多线程的xgboost.最后还是从the ...

  3. Linux命令(1) - 查看内存使用情况: free -hm

    使用"free -hm"命令查看linux服务器的内存使用状况,其中-h表示人性化显示,-m表示将内存显示为M:

  4. phalcon count统计

    单表count: //How many robots are there? $number = Robots::count(); echo "There are ", $numbe ...

  5. stdcall与cdecl的区别

    1 区别 VC++的C/C++函数有两种基本的调用约定:__stdcall.__cdecl.它们有什么区别呢?请参考下表:     __stdcall __cdecl 函数代码 C int __std ...

  6. bzoj题解汇总(1032~1051)

    bzoj1034:贪心 bzoj1036:树剖 bzoj1037:一个比较巧妙,利用连续性维护的dp. http://www.cnblogs.com/Sdchr/p/6129496.html bzoj ...

  7. 5.3.2 Eclipse集成开发环境的使用技巧

    Eclipse具有强大的编辑.调试.编译和打包功能,本节仅讲解Eclipse中最常用的功能. 1.将程序代码和注释字体变大 (1)启动Eclipse,选择“Windows”->“Preferen ...

  8. 《javascript高级程序设计》第21章 Ajax和Comet

    21.1 XMLHttpRequest 对象  The XMLHttpRequest Object 21.1.1 XHR 的用法 XHR Usage 21.1.2 HTTP 头部信息 XHR Head ...

  9. LINUX&UNIX 安装vmware workstation10和centOS6

    大一下时,学习了linux&unix这门课程,全字符的操作,我对它并不是很感冒,不过,还是找学长安装过虚拟机和Linux系统,在考前利用它和putty进行复习.现在重装系统之后,各类软件,自然 ...

  10. HDU----(4519)郑厂长系列故事——体检

    郑厂长系列故事——体检 Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total S ...