第一步
引入所需的包
第二步
1,配置数据库属性
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<!-- 配置外置的property属性文件 必须引入 context 命名空间 -->
<context:property-placeholder location="/WEB-INF/jdbc.properties" />
<!--配置数据源-->
<bean id="dataSources" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass">
<value>${jdbcDriver}</value>
</property>
<property name="jdbcUrl">
<value>${jdbcUrl}</value>
</property>
<property name="user">
<value>${jdbcUserName}</value>
</property>
<property name="password">
<value>${jdbcPassWord}</value>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource"ref="dataSources" />
</bean>
<!—配置sessionFactory工厂-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"ref="dataSources" />
<property name="mappingResources">
<list>
<value>com/zhu/domain/ZZUser.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
<prop key="hibernate.autoReconnect">true</prop>
</props>
</property>
</bean>
</beans>
2,WEB-INF 下的property属性文件配置如下
jdbcDriver=com.mysql.jdbc.Driver
jdbcUrl=jdbc\:mysql\://localhost\:3306/zhulei
jdbcUserName=root
jdbcPassWord=root
链接池中最大的链接术
jdbcMax=10
最小
jdbcMin=3
初始化
jdbcInit=5
3,配置Dao包继承的父类
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTDBEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-lazy-init="true"default-autowire="byName">
<bean id="hibernateDaoSupport" class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"abstract="true"></bean>
</beans>
或者
<bean id="hibernateDaoSupport" class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"abstract="true">
<property name="sessionFactory"ref="sessionFactory" />
</bean>
这只是其中的一种方法
4,Dao包中引用
package com.zhu.dao;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.zhu.Hibernate.BaseHibernateDao;
import com.zhu.Hibernate.HibernateQuery;
import com.zhu.domain.ZZUser;
import com.zhu.util.Pagination;
import com.zhu.util.SqlUtil;
/**
* user操作对象具体实现类
*/
public class ZZUserDaoImpl extends BaseHibernateDao implements ZZUserDao {
private static final long serialVersionUID = 1L;
private static String[] properties={"id","username","userpass","available","addtimes"};
@Autowired
HibernateQuery hibernateQuery;
/**
* 插入数据
* */
public boolean insert(ZZUser user) {
boolean falg = true;
try {
this.getHibernateTemplate().save(user);
} catch (Exception e) {
System.out.println(e);
falg = false;
}
return falg;
}
/**
* 根据用户名查找数据
*/
@SuppressWarnings("unchecked")
public ZZUser findByUsername(String username) {
ZZUser user=null;
try {
List list=this.getHibernateTemplate().find("from ZZUser where username= ? ", username);
if(list.size()!=0){
user=(ZZUser)list.get(0);
}
} catch (Exception e) {
System.out.println(e);
}
return user;
}
/**
* 分页查找用户
* */
public List findList(ZZUser user,Pagination pagination){
List list=null;
StringBuffer sql=new StringBuffer();
String sqlConditions="";
try {
sqlConditions=SqlUtil.addConditions(user, properties);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
sql.append("select *From ZZUser where 1=1");
sql.append(sqlConditions);
list = hibernateQuery.getQueryPage(sql.toString(), pagination);
return list;
}
public HibernateQuery getHibernateQuery() {
return hibernateQuery;
}
public void setHibernateQuery(HibernateQuery hibernateQuery) {
this.hibernateQuery =hibernateQuery;
}
}