2.0
Spring applicationContext.xml配置(例子)
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
">
<!-- 建立连接池 -->
<bean name="datasouce" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"></property>
<property name="username" value="sa"></property>
<property name="password" value="sa123456"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="100000"></property>
<property name="defaultAutoCommit" value="false"></property>
</bean>
<bean name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="datasouce"></property>
<property name="hibernateProperties">
<props>
<!-- 加载hibernate配置属性dialect(方言)show_sql(显示在数据库执行的sql语句) -->
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<!-- 加载hibernate的类关于数据库的映射文件 -->
<property name="mappingResources">
<list>
<value>com/skeyedu/crm/entity/CrmUserandcustomer.hbm.xml</value>
<value>com/skeyedu/crm/entity/User.hbm.xml</value>
<value>com/skeyedu/crm/entity/Customer.hbm.xml</value>
<value>com/skeyedu/crm/entity/CrmCustomerandlinkman.hbm.xml</value>
<value>com/skeyedu/crm/entity/LinkMan.hbm.xml</value>
<value>com/skeyedu/crm/entity/Crmfollow.hbm.xml</value>
</list>
</property>
</bean>
<!--所有dao实现类的继承了HibernateDaoSupport必实现里面的sessionFactory属性-->
<bean name="userdao" class="com.skeyedu.crm.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean name="customerdao" class="com.skeyedu.crm.dao.impl.CustomerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean name="linkmandao" class="com.skeyedu.crm.dao.impl.LinkManDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- service层调用dao方法,配置dao接口属性 -->
<bean name="userservice" class="com.skeyedu.crm.service.impl.UserServiceImpl">
<property name="UDao" ref="userdao"></property>
</bean>
<bean name="customerservice" class="com.skeyedu.crm.service.impl.CustomerServiceImpl">
<property name="CDao" ref="customerdao"></property>
</bean>
<bean name="linkmanservice" class="com.skeyedu.crm.service.impl.linkManServiceImpl">
<property name="CDao" ref="customerdao"></property>
<property name="LDao" ref="linkmandao"></property>
</bean>
<!--scope="prototype",表示每调用action时从新new个对象,这样排除出现错误在input的时值依然存在,导致actin报错,其他操作都无法使用,因使用的均为同一个action(struts2底层机制是使用时就new个对象)或者这样说scope="prototype" 会在该类型的对象被请求时创建一个新的action对象。如果没有配置scope=prototype则添加的时候不会新建一个action,他任然会保留上次访问的过记录的信息。 -->
<bean name="douser" class="com.skeyedu.crm.struts.DoUserAction" scope="prototype">
<property name="UService" ref="userservice"></property>
</bean>
<bean name="docustomer" class="com.skeyedu.crm.struts.DoCustmoerAction" scope="prototype">
<property name="CService" ref="customerservice"></property>
<property name="UService" ref="userservice"></property>
</bean>
<bean name="dolinkman" class="com.skeyedu.crm.struts.DoLinkmanAction" scope="prototype">
<property name="CService" ref="customerservice"></property>
<property name="LService" ref="linkmanservice"></property>
</bean>
<!-- 声明事务,仅管理service层,在调用dao层方法时异常,捕获并回滚(例:Add*表示以事务环境管理Add开头的方法),对不合以下规则的其他方法不做事务提交或回滚处理,是只读的,一般是指不涉及到数据库更新的方法-->
<bean name="trasactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="myadvice" transaction-manager="trasactionManager">
<tx:attributes>
<tx:method name="Add*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="Edit*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="Del*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="Drop*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="Regst*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="shareuser*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 以aop(面向方面)声明对的哪个包中的接口进行事务管理(列:"execution(* com.skeyedu.crm.service.*.*(..))"表示:第一个星:返回值,第二星:类,第三星:方法,(..):参数)
切点:对哪些包下面的接口进行事务管理
第一个星号代表方法返回值类型任意。
第二个星号代表包下面的接口或类名任意。
第三个星号代表包下面的接口或类的方法名任意。
小括号里的两个点代表方法参数任意-->
<aop:config>
<aop:pointcut expression="execution(* com.skeyedu.crm.service.*.*(..))" id="mycutpoint"/>
<!-- 合并事务与切面 -->
<aop:advisor advice-ref="myadvice" pointcut-ref="mycutpoint"/>
</aop:config>
</beans>
Spring basedao(通用)
package com.test.mydb;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class SpringBasedao extends HibernateDaoSupport {
//添加单对象
public int save(Object o){
int row=0;
try {
super.getHibernateTemplate().save(o);
row=1;
} catch (Exception e) {
e.printStackTrace();
}
return row;
}
//修改单对象
public int update(Object o){
int row=0;
try {
super.getHibernateTemplate().clear();
super.getHibernateTemplate().update(o);
row=1;
} catch (Exception e) {
e.printStackTrace();
}
return row;
}
//删除单对象
public int delete(Class cls,Serializable id){
int row=0;
Object o=get(cls, id);
try {
super.getHibernateTemplate().delete(o);
row=1;
} catch (Exception e) {
e.printStackTrace();
}
return row;
}
//根据ID(主键)获取单个对象
public Object get(Class cls,Serializable id){
return getHibernateTemplate().get(cls, id);
}
//查询获取对象集合,hql查询语句
public List getlistbyhql(String hql){
List list=new ArrayList();
list=getHibernateTemplate().find(hql);
return list;
}
public List getlistbyhql(final String hql,final String[] parms){
List list=new ArrayList();
list=getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query=session.createQuery(hql);
if(parms!=null){
for (int i = 0; i < parms.length; i++) {
query.setString(i, parms[i]);
}
}
return query.list();
}
});
return list;
}
//查询数据量
public Object getuniqbyhql(final String hql){
Object o=getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query=session.createQuery(hql);
return query.uniqueResult();
}
});
return o;
}
public Object getuniqbyhql(final String hql,final String[] parms){
Object o=getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query=session.createQuery(hql);
if(parms!=null){
for (int i = 0; i < parms.length; i++) {
query.setString(i, parms[i]);
}
}
return query.uniqueResult();
}
});
return o;
}
//执行批量更新(修改删除)
public int exeupdatehql(final String hql){
int row=0;
row=(Integer)getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query=session.createQuery(hql);
int row=query.executeUpdate();
return row;
}
});
return row;
}
public int exeupdatehql(final String hql,final String[] prams){
int row=0;
row=(Integer)getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query=session.createQuery(hql);
if (prams!=null) {
for (int i = 0; i < prams.length; i++) {
query.setString(i, prams[i]);
}
}
int row=query.executeUpdate();
return row;
}
});
return row;
}
/*分页方法,参数pagesize表示每页显示数据量,startpage表示数据库从指定开始数据读取到指定位置,
* 例:int startpage=(当前页数-1) * pagesize;
* 当前页数为1,pagesize为3,读取数据库0到2数据
* 当前页数为2,pagesize为3,读取数据库3到5数据
* */
public List pageList(final String hql,final int pagesize,final int startpage){
return getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query=session.createQuery(hql);
query.setFirstResult(startpage);
query.setMaxResults(pagesize);
List list=query.list();
return list;
}
});
}
public List pageList(final String hql,final int pagesize,final int startpage,final String[] parms){
return getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query=session.createQuery(hql);
if(parms!=null){
for (int i = 0; i < parms.length; i++) {
query.setString(i, parms[i]);
}
}
query.setFirstResult(startpage);
query.setMaxResults(pagesize);
List list=query.list();
return list;
}
});
}
}
分页代码例子:
//page当前页数,pagesize显示的数据量
public List<LinkMan> pagenumber(int page, int pagesize, LinkMan linkman) {
System.out.println(page+"_"+pagesize);
List<LinkMan> list =null;
String where=" where 1=1 ";
if(linkman!=null){
if(!"".equals(linkman.getName())){
where = where+" and l.surName||l.name like '%"+linkman.getSurName()+"%' ";
}
if(!"".equals(linkman.getCustomerName())){
where = where+" and l.CustomerName like '%"+linkman.getCustomerName()+"%' ";
}
if(!"".equals(linkman.getEmail())){
where = where+" and l.Email like '%"+linkman.getEmail()+"%' ";
}
}
String hql="from LinkMan l "+where+" order by l.linkManID desc";
int startpage=(page-1) * pagesize;
list=super.pageList(hql,pagesize,startpage);
return list;
}
//根据查询条件计数查询出来的数据条数
public int getsearchcount(LinkMan linkman) {
int count=0;
String where=" where 1=1 ";
if(linkman!=null){
if(!"".equals(linkman.getName())){
where = where+" and l.surName||l.name like '%"+linkman.getSurName()+"%' ";
}
if(!"".equals(linkman.getCustomerName())){
where = where+" and l.CustomerName like '%"+linkman.getCustomerName()+"%' ";
}
if(!"".equals(linkman.getEmail())){
where = where+" and l.Email like '%"+linkman.getEmail()+"%' ";
}
}
String hql=" select count(*) from LinkMan l "+where;
count=Integer.valueOf(super.getuniqbyhql(hql).toString());
return count;
}
分页action传的页码值
public String dolist(){
List<LinkMan> list = lService.getAllLinkMan();
int listcount = list.size();
Page pagenum=new Page();
pagenum.setRspagecount(listcount);//总数据条数
pagenum.setUppage(1);//上一页
pagenum.setNextpage(1);//下一页
pagenum.setPagesize(3);//页面显示条数
pagenum.setPagecount((pagenum.getRspagecount()+pagenum.getPagesize()-1)/pagenum.getPagesize());//页面总数
pagenum.setPage(1);//当前页码
String pagestr=ServletActionContext.getRequest().getParameter("page");
if(null==pagestr){
pagenum.setPage(1);
}
try {
pagenum.setPage(Integer.valueOf(pagestr));
} catch (Exception e) {
pagenum.setPage(1);
}
if (pagenum.getPage() <= 0) {
pagenum.setPage(1);
}
if (pagenum.getPage() > pagenum.getPagecount()) {
pagenum.setPage(pagenum.getPagecount());
}
if (pagenum.getPage() - 1 > 0) {
pagenum.setUppage(pagenum.getPage() - 1);
} else {
pagenum.setUppage(0);
}
if (pagenum.getPage() + 1 > pagenum.getPagecount()) {
pagenum.setNextpage(0);
} else {
pagenum.setNextpage(pagenum.getPage() + 1);
}
List<LinkMan> listpage=lService.getpagenumber(pagenum.getPage(), pagenum.getPagesize(), null);
request.put("list", listpage);
request.put("pagenum", pagenum);
return SUCCESS;
}
本文出自 “青春行囊” 博客,转载请与作者联系!