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

hibernate对连接池的支持

连接池,

作用: 管理连接;提升连接的利用效率!

常用的连接池: C3P0连接池

Hibernate 自带的也有一个连接池,且对C3P0连接池也有支持!

只维护一个连接,比较简陋。

可以查看hibernate.properties文件查看连接池详细配置:

......
#################################
### Hibernate Connection Pool ###
#################################

# Hibernate自带的连接池:只有一个连接
hibernate.connection.pool_size 1

###########################
### C3P0 Connection Pool###
###########################
#Hibernate连接池的参数
#hibernate.c3p0.max_size 2  最大连接数
#hibernate.c3p0.min_size 2  最小连接数
#hibernate.c3p0.timeout 5000  超时连接
#hibernate.c3p0.max_statements 100  最大执行的命令个数
#hibernate.c3p0.idle_test_period 3000 空闲测试时间
#hibernate.c3p0.acquire_increment 2 连接不够用的时候,每次增加的连接数
#hibernate.c3p0.validate false

##############################
### Proxool Connection Pool###
##############################

## Properties for external configuration of Proxool

hibernate.proxool.pool_alias pool1

## Only need one of the following

#hibernate.proxool.existing_pool true
#hibernate.proxool.xml proxool.xml
#hibernate.proxool.properties proxool.properties

#################################
### Plugin ConnectionProvider ###
#################################

## use a custom ConnectionProvider (if not set, Hibernate will choose a built-in ConnectionProvider using hueristics)
#Hibernate对C3P0的连接池支持
#hibernate.connection.provider_class org.hibernate.connection.DriverManagerConnectionProvider
#hibernate.connection.provider_class org.hibernate.connection.DatasourceConnectionProvider
#hibernate.connection.provider_class org.hibernate.connection.C3P0ConnectionProvider
#hibernate.connection.provider_class org.hibernate.connection.ProxoolConnectionProvider

......

Hibernate.cfg.xml 中增加连接池相关配置:

          <!-- 连接池配置 -->
		<!-- 配置连接驱动管理类 -->
		<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>	<!-- 最大执行sql语句的条数 -->
		<property name="hibernate.c3p0.idle_test_period">3000</property>
		<property name="hibernate.c3p0.acquire_increment">2</property>

HQL查询

1、分页查询

@Test
	public void test() {
		Session session=sf.openSession();
		session.beginTransaction();

		Query query=session.createQuery("from Employee");

		//从记录数
		ScrollableResults scroll=query.scroll();	//得到滚动的结果集
		scroll.last();								//滚到最后一条
		int totalCount=scroll.getRowNumber()+1;		//得到滚动的记录数,即总记录数

		//设置分页参数
		query.setFirstResult(0);
		query.setMaxResults(4);
		List<Employee> list=query.list();

		for (Employee employee : list) {
			System.out.println(employee);
		}

		session.getTransaction().commit();
		session.close();

	}

2、HQL的各种查询

@Test
	public void testHql() {
		Session session=sf.openSession();
		session.beginTransaction();
		//查询指定的列
//		Query query=session.createQuery("select e.empId,e.empName from Employee e");

		//查询指定的列,同时自动封装【必须要提供带参数的构造器】
//		Query query=session.createQuery("select new Employee(e.empId,e.empName) from Employee e");

		//条件查询  一个条件/多个条件/ or /and /like模糊查询
		//占位符
//		Query query=session.createQuery("from Employee where empName=?");
//		query.setString(0, "王五");
		//条件查询,命名参数
//		Query query=session.createQuery("from Employee where empId=:myId and empName=:myName");
//		query.setParameter("myId", 3);
//		query.setParameter("myName", "王五");
		//模糊查询
//		Query query=session.createQuery("from Employee where empName like ?");
//		query.setString(0, "%王%");

		//聚合函数统计
//		Query query=session.createQuery("select count(*) from Employee");
//		Long num=(Long)query.uniqueResult();
//		System.out.println(num);

		//分组查询
		Query query=session.createQuery("select e.dept,count(*) from Employee e group by e.dept");

		System.out.println(query.list());
		session.getTransaction().commit();
		session.close();
	}

js:多级菜单

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>延迟菜单</title>
    <link rel="stylesheet" type="text/css" href="miaov_style.css">
</head>
<script type="text/javascript">
window.onload = function() {
	var aA=getByClass(document,'normal');
	var aSpan=getByClass(document,'float_layer');
	var timer=null;

	for (var i = aA.length - 1; i >= 0; i--) {
		aA[i].index=i;
		aA[i].onmouseover=function () {
			for (var i = aSpan.length - 1; i >= 0; i--) {
				aSpan[i].style.display='none';
			}

			aSpan[this.index].style.display='block';
			clearInterval(timer);
		}

		aA[i].onmouseout=function () {
			var This=this;

			timer=setTimeout(function () {
				aSpan[This.index].style.display='none';
			},1000);
		}

		aSpan[i].onmouseover=function () {
			clearInterval(timer);
		};

		aSpan[i].onmouseout=function () {
			var This=this;
			timer=setTimeout(function () {
				This.style.display='none';
			},1000);
		}

	}

    function getByClass(oParent, sClass) {
        var aElem = oParent.getElementsByTagName('*');
        var result = [];
        //正则表达式
        // '\' 表示转义
        // \b 在正则里表示单词的边界位置
        // 'i' 区分大小写
        var re=new RegExp('\\b'+sClass+'\\b','i');
        for (var i = aElem.length - 1; i >= 0; i--) {
        	if (re.test(aElem[i].className)) {
        		result.push(aElem[i]);
        	}
        }
        return result;
    }
}
</script>

<body>
    <div id="nav">
        <div class="adorn1"></div>
        <div class="adorn2"></div>
        <ul>
            <li>
                <a class="normal" href="#"><span>首页</span></a>
                <span class="float_layer w1 pos1">
                <span class="arrows"></span>
                <span class="decorate1"></span>
                <span class="decorate2"></span>
                <a href="#"><span>最近更新</span></a>
                <a href="#"><span>活动</span></a>
                <a href="#"><span>报名试听</span></a>
                <a href="#"><span>学员反馈</span></a>
                </span>
            </li>
            <li class="line">
                <a class="normal" href="#"><span>关于我们</span></a>
                <span class="float_layer w2 pos2">
                <span class="arrows"></span>
                <span class="decorate1"></span>
                <span class="decorate2"></span>
                <a href="#"><span>妙味讲师</span></a>
                <a href="#"><span>培训方式</span></a>
                <a href="#"><span>培训理念</span></a>
                <a href="#"><span>联系我们</span></a>
                </span>
            </li>
            <li class="line">
                <a class="normal" href="#"><span>作品</span></a>
                <span class="float_layer w3 pos3">
                <span class="arrows"></span>
                <span class="decorate1"></span>
                <span class="decorate2"></span>
                <a href="#"><span>般固</span></a>
                <a href="#"><span>MATRIX</span></a>
                <a href="#"><span>留学e网</span></a>
                <a href="#"><span>ECMall</span></a>
                </span>
            </li>
            <li class="line">
                <a class="normal" href="#"><span>博客</span></a>
                <span class="float_layer w4 pos4">
                <span class="arrows"></span>
                <span class="decorate1"></span>
                <span class="decorate2"></span>
                <a href="#"><span>JS教程</span></a>
                <a href="#"><span>弹出层效果</span></a>
                <a href="#"><span>3D球面标签云</span></a>
                <a href="#"><span>Window计算器</span></a>
                </span>
            </li>
        </ul>
        <p><a href="#">>>更多</a></p>
    </div>
</body>

</html>

效果连接:延迟菜单、

代码:链接

上一篇:IO多路复用


下一篇:java学习笔记5(方法)