HQL查询及Hibernate对c3p0连接池的支持
//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连接池的支持的更多相关文章
- Hibernate配置C3P0连接池
引入C3PO包 在hibernate.cfg.xml文件中配置 <!-- 数据库连接池的使用 --> <!-- 选择使用C3P0连接池 --> <property nam ...
- Hibernate -- 配置c3p0连接池, 事务隔离级别, 管理session
知识点1:配置c3p0连接池(了解) * 引入c3p0-0.9.1.jar * 在hibernate.cfg.xml文件中增加如下配置 <!-- C3P0连接池设定--> <!-- ...
- (30)java web的hibernate使用-c3p0连接池配置
hibernate支持c3p0连接池 需要导入c3p0的jar包 <!-- 配置连接驱动管理类 --> <property name="hibernate.connecti ...
- Hibernate 配置C3P0 连接池
第一步在hibernate.cfg.xml配置 <!-- 连接池 --> <property name="hibernate.connection.provider_cla ...
- Hibernate的查询,二级缓存,连接池
Hibernate的查询,二级缓存,连接池 1.Hibernate查询数据 Hibernate中的查询方法有5中: 1.1.Get/Load主键查询 使用get或者load方法来查询,两者之间的区别在 ...
- hibernate对连接池的支持和HQL查询
hibernate对连接池的支持 连接池, 作用: 管理连接:提升连接的利用效率! 常用的连接池: C3P0连接池 Hibernate 自带的也有一个连接池,且对C3P0连接池也有支持! 只维护一个连 ...
- hibernate 查询、二级缓存、连接池
hibernate 查询.二级缓存.连接池 查询: 1) 主键查询 Dept dept = (Dept) session.get(Dept.class, 12); Dept dept = (Dep ...
- C3P0连接池在hibernate和spring中的配置
首先为什么要使用连接池及为什么要选择C3P0连接池,这里就不多说了,目前C3P0连接池还是比较方便.比较稳定的连接池,能与spring.hibernate等开源框架进行整合. 一.hibernate中 ...
- Hibernate的配置中,c3p0连接池相关配置
一.配置c3p0 1.导入 hibernate-c3po连接池包,Maven地址是:http://mvnrepository.com/artifact/org.hibernate/hibernate- ...
随机推荐
- heaters
https://leetcode.com/problems/heaters/ 开始的时候,下面的代码对于两边数字完全一样的情况,测试不通过.原因是heater会有重复情况,这时候对于飘红部分就不会往前 ...
- linux 文件类型 文件权限
linux中常见的文件类型有: “—”表示普通文件 :-rw-r--r-- 1 root root 41727 07-13 02:56 install.log “d”表示目录 :drwxr-xr- ...
- linux登录mysql
mysql -u 用户名 -p密码 mysql -u root -psqj888
- 使用soapui调用webservice接口
soapui是专门模拟调用webservice接口的工具,下面介绍下怎么使用: 1.下载soapui并安装: 2.以免费天气获取接口为例:http://www.webservicex.net/glob ...
- VIm vi 使用 汇总
x 删除当前光标下的字符dw 删除光标之后的单词剩余部分.d$ 删除光标之后的该行剩余部分.dd 删除当前行. c 功能和d相同,区别在于完成删除操作后进入INSERT MODEcc 也是删除当前行, ...
- phalcon: 查找记录(Finding Records)可用的查询设置如下:
可用的查询设置如下: 参数 描述 举例 conditions Search conditions for the find operation. Is used to extract only tho ...
- 【转】APP的缓存文件到底应该存在哪?看完这篇文章你应该就自己清楚了
只要是需要进行联网获取数据的APP,那么不管是版本更新,还是图片缓存,都会在本地产生缓存文件.那么,这些缓存文件到底放在什地方合适呢?系统有没有给我们提供建议的缓存位置呢?不同的缓存位置有什么不同呢? ...
- sizeof和strlen()的区别
二者有本质上的区别 从定义可以知道sizeof只是一个operator,而strlen()则是定义一个定义在<string.h>中的函数;所以sizeof(string)是在计算strin ...
- VR应用里面的Photogrammetry技术是什么
http://www.manew.com/thread-49556-1-1.html 具体使用 http://www.didayin.com/archives/632 软件下载 http://labs ...
- js(引用类型和setTimeout scope)
题目是群中小伙伴出的. var a = [1,2,3]; c= a //todo 限制条件 c 不能出现在 = 左边 console.log(a) console.log(c) console.log ...