Spring+Hibernate整合
项目
项目代码来源how2java中的相关框架整合学习资料。
根据自己个人理解,以及相关资料,总结框架整合的体会和经验
pojo以及hbm
这一部分和直接的Hibernate的使用没有区别。
一个普通java类:
package com.how2java.pojo;
public class Category {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int id;
private String name;
}
hbm编写:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.how2java.pojo">
<class name="Category" table="category_">
<id name="id" column="id">
<generator class="native">
</generator>
</id>
<property name="name" />
</class>
</hibernate-mapping>
Dao层
我们实现CategoryDao层,令它继承于
HibernateTemplate
。
package com.how2java.dao;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class CategoryDAO extends HibernateTemplate{
}
Spring配置
其中,注入了:
- 一个Category实体类对应的对象
- 一个Category实体类对应的Dao对象
- 一个SessionFactory包含
- 数据源配置datasource。
- Hibernate的配置属性
- 映射关系mapping。
- 一个数据源ds:其中配置了数据库的连接。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="c" class="com.how2java.pojo.Category">
<property name="name" value="yyy" />
</bean>
<bean name="dao" class="com.how2java.dao.CategoryDAO">
<property name="sessionFactory" ref="sf" />
</bean>
<bean name="sf"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="ds" />
<property name="mappingResources">
<list>
<value>com/how2java/pojo/Category.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hbm2ddl.auto=update
</value>
</property>
</bean>
<bean name="ds"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="admin" />
</bean>
</beans>
测试(增删查改)
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
CategoryDAO dao = (CategoryDAO) context.getBean("dao");
Category c = new Category();
c.setName("category yyy");
//增加
dao.save(c);
//获取
Category c2 = dao.get(Category.class, 1);
//修改
c2.setName("category zzz");
dao.update(c2);
//删除
dao.delete(c2);
}
新特性
分页
start是起始的位置,count是每页显示的数量。
DetachedCriteria dc = DetachedCriteria.forClass(Category.class);
int start =5;//从多少开始查询
int count =10;//每页显示数量
List<Category> cs= dao.findByCriteria(dc,start,count);
查询总数:
List<Long> l =dao.find("select count(*) from Category c");
long total = l.get(0);
模糊查询的实现:
分别使用Hql和Criteria来实现。
List<Category> cs =dao.find("from Category c where c.name like ?", "%c%");
for (Category c : cs) {
System.out.println(c.getName());
}
DetachedCriteria dc = DetachedCriteria.forClass(Category.class);
dc.add(Restrictions.like("name", "%分类%"));
cs =dao.findByCriteria(dc);
for (Category c : cs) {
System.out.println(c.getName());
}
}
c3p0连接池
修改数据源配置:
<bean name="ds"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8" />
<property name="user" value="root" />
<property name="password" value="admin" />
<!--连接池中保留的最小连接数。-->
<property name="minPoolSize" value="10" />
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="100" />
<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800" />
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3" />
<!--最大的Statements条数 -->
<property name="maxStatements" value="1000" />
<!--初始化10条连接 -->
<property name="initialPoolSize" value="10" />
<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
<property name="acquireRetryAttempts" value="30" />
<!--每隔60秒发一次心跳信号到数据库,以保持连接的活性 -->
<property name="idleConnectionTestPeriod" value="60" />
</bean>
Orcale数据库
因为Oracle基本没用过,就暂不讨论。
参考
how2java:中相关框架整合