(tng) 首先定要Proxy的目标,在Spring中默认采用JDK中的dynamic proxyQ它只能够实现接口的代理Q如果想对类q行代理的话Q需要采用CGLIB的proxy。显?dng)选择“编E到接口”是更明智的做法Q下面是要代理的接口:(x)
(tng) public interface FooInterface {
(tng) (tng) (tng) public void printFoo();
(tng) (tng) (tng) public void dummyFoo();
(tng) }
(tng)
(tng) 以及(qing)其一个简单的实现Q?br /> (tng)
(tng) public class FooImpl implements FooInterface {
(tng) (tng) (tng) public void printFoo() {
(tng) (tng) (tng) (tng) (tng) System.out.println("In FooImpl.printFoo");
(tng) (tng) (tng) }
(tng) (tng) (tng) public void dummyFoo() {
(tng) (tng) (tng) (tng) (tng) System.out.println("In FooImpl.dummyFoo");
(tng) (tng) (tng) }
(tng) }
(tng)
(tng) 接下来创Z个AdviceQ在Spring中支持Around,Before,After returning和Throws四种AdviceQ这里就以简单的Before Advice举例Q?br /> (tng)
(tng) public class PrintBeforeAdvice implements MethodBeforeAdvice {
(tng) (tng) (tng) public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
(tng) (tng) (tng) (tng) (tng) System.out.println("In PrintBeforeAdvice");
(tng) (tng) (tng) }
(tng) }
(tng)
(tng) 有了(jin)自己的business interface和adviceQ剩下的是如何去装配它们了(jin)Q首先利用ProxyFactory以编E方式实玎ͼ如下Q?br /> (tng)
(tng) public class AopTestMain {
(tng) (tng) (tng) public static void main(String[] args) {
(tng) (tng) (tng) (tng) (tng) FooImpl fooImpl = new FooImpl();
(tng) (tng) (tng) (tng) (tng) PrintBeforeAdvice myAdvice = new PrintBeforeAdvice();
(tng) (tng) (tng) (tng) (tng)
(tng) (tng) (tng) (tng) (tng) ProxyFactory factory = new ProxyFactory(fooImpl);
(tng) (tng) (tng) (tng) (tng) factory.addBeforeAdvice(myAdvice);
(tng) (tng) (tng) (tng) (tng) FooInterface myInterface = (FooInterface)factory.getProxy();
(tng) (tng) (tng) (tng) (tng) myInterface.printFoo();
(tng) (tng) (tng) (tng) (tng) myInterface.dummyFoo();
(tng) (tng) (tng) }
(tng) }
(tng)
(tng) 现在执行E序Q神奇的l果出C(jin)Q?br /> (tng)
(tng) In PrintBeforeAdvice
(tng) In FooImpl.printFoo
(tng) In PrintBeforeAdvice
(tng) In FooImpl.dummyFoo
(tng)
(tng) 虽然q样能体?x)到Spring中AOP的用法,但这决不是值得推荐的方法,既然使用?jin)SpringQ在ApplicationContext中装配所需?的bean才是最佳策略,实现上面的功能只需要写个简单的applicationContext可以了(jin)Q如下:(x)
(tng)
(tng) <?xml version="1.0" encoding="UTF-8"?>
(tng) <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
(tng) (tng) (tng) "http://www.springframework.org/dtd/spring-beans.dtd">
(tng) <beans>
(tng) (tng) (tng) <description>The aop application context</description>
(tng) (tng) (tng) <bean id="fooTarget" class="FooImpl"/>
(tng) (tng) (tng) <bean id="myAdvice" class="PrintBeforeAdvice"/>
(tng) (tng) (tng)<bean id="foo" class="org.springframework.aop.framework.ProxyFactoryBean">
(tng) (tng) (tng) (tng) <property name="proxyInterfaces">
(tng) (tng) (tng) (tng) (tng) (tng) <value>FooInterface</value>
(tng) (tng) (tng) (tng) </property>
(tng) (tng) (tng) (tng) (tng)<property name="target">
(tng) (tng) (tng) (tng) (tng) (tng) <ref local="fooTarget"/>
(tng) (tng) (tng) (tng) </property>
(tng) (tng) (tng) (tng) <property name="interceptorNames">
(tng) (tng) (tng) (tng) (tng) (tng) <list>
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) <value>myAdvice</value>
(tng) (tng) (tng) (tng) (tng) (tng) </list>
(tng) (tng) (tng) (tng) </property>
(tng) (tng) (tng) </bean>
(tng) </beans>
(tng) 当然Qmain中的代码也要q行相应的修改:(x)
(tng) (tng) (tng) (tng)
(tng) public static void main(String[] args) {
(tng) (tng) (tng) ClassPathXmlApplicationContext context = new (tng)
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)ClassPathXmlApplicationContext("applicationContext.xml");
(tng) (tng) (tng) FooInterface foo = (FooInterface)context.getBean("foo");
(tng) (tng) (tng) foo.printFoo();
(tng) (tng) (tng) foo.dummyFoo();
(tng) }
(tng)
(tng) 现在q行一下,l果和上面的运行结果完全一Pq样是不是更优雅Q当需要更改实现时Q只需要修攚w|文件就可以?jin),E序中的代码不需M改动?br /> (tng)
(tng) 但是Q这时候会(x)发现被proxy的object中的所有方法调用时都将q行advice中的beforeQ这昄不能满l大多数情况下的需要,此时Q只 需借用Advisor可以了(jin)Q当然要在Advisor中利用pattern讄好哪些方法需要adviceQ更改applicationContext 如下Q?br /> (tng)
(tng) <?xml version="1.0" encoding="UTF-8"?>
(tng) <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
(tng) (tng) (tng) "http://www.springframework.org/dtd/spring-beans.dtd">
(tng) <beans>
(tng) (tng) (tng) <description>The springeva application context</description>
(tng) (tng) (tng)<bean id="fooTarget" class="FooImpl"/>
(tng) (tng) (tng) <bean id="printBeforeAdvice" class="PrintBeforeAdvice"/>
(tng) (tng) (tng) <bean id="myAdvisor"
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
(tng) (tng) (tng) (tng) (tng) <property name="advice">
(tng) (tng) (tng) (tng) (tng) (tng) (tng) <ref local="printBeforeAdvice"/>
(tng) (tng) (tng) (tng) (tng) </property>
(tng) (tng) (tng) (tng) (tng) <property name="pattern">
(tng) (tng) (tng) (tng) (tng) (tng) (tng) <value>.*print.*</value>
(tng) (tng) (tng) (tng) (tng) </property>
(tng) (tng) (tng) </bean>
(tng) (tng) (tng) <bean id="foo" class="org.springframework.aop.framework.ProxyFactoryBean">
(tng) (tng) (tng) (tng) (tng) <property name="proxyInterfaces">
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)<value>FooInterface</value>
(tng) (tng) (tng) (tng) (tng) </property>
(tng) (tng) (tng) (tng) (tng) <property name="target">
(tng) (tng) (tng) (tng) (tng) (tng) (tng) <ref local="fooTarget"/>
(tng) (tng) (tng) (tng) (tng) </property>
(tng) (tng) (tng) (tng) (tng) <property name="interceptorNames">
(tng) (tng) (tng) (tng) (tng) (tng) (tng) <list>
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) <value>myAdvisor</value>
(tng) (tng) (tng) (tng) (tng) (tng) (tng) </list>
(tng) (tng) (tng) (tng) (tng) (tng)</property>
(tng) (tng) (tng) </bean>
(tng) </beans>
(tng) ȝ序不需q行M修改Q运行结果已l变样了(jin)Q?/p>
(tng) In PrintBeforeAdvice
(tng) In FooImpl.printFoo
(tng) In FooImpl.dummyFoo
(tng)
(tng) x(chng)Q应该已l理解了(jin)Spring中AOP的用方法,当然Spring中AOP最重要的应用是Transaction ManagerQD个这斚w的applicationContext例子看看Q?br /> (tng)
(tng) <?xml version="1.0" encoding="UTF-8"?>
(tng) <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd">
(tng) <beans>
(tng) (tng) (tng) <bean id="propertyConfigurer" (tng) (tng) (tng)
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
(tng) (tng) (tng) (tng) (tng) <property name="location">
(tng) (tng) (tng) (tng) (tng) (tng) (tng) <value>/WEB-INF/jdbc.properties</value>
(tng) (tng) (tng) (tng) (tng) (tng)</property>
(tng) (tng) (tng)</bean>
(tng) (tng) (tng) <bean id="dataSource"
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) class="org.springframework.jdbc.datasource.DriverManagerDataSource">
(tng) (tng) (tng) (tng) (tng) <property name="driverClassName">
(tng) (tng) (tng) (tng) (tng) (tng) (tng) <value>${jdbc.driverClassName}</value>
(tng) (tng) (tng) (tng) (tng) </property>
(tng) (tng) (tng) (tng) (tng) (tng)<property name="url">
(tng) (tng) (tng) (tng) (tng) (tng) (tng) <value>${jdbc.url}</value>
(tng) (tng) (tng) (tng) (tng) </property>
(tng) (tng) (tng) (tng) (tng) <property name="username">
(tng) (tng) (tng) (tng) (tng) (tng) (tng) <value>${jdbc.username}</value>
(tng) (tng) (tng) (tng) (tng) </property>
(tng) (tng) (tng) (tng) (tng) <property name="password">
(tng) (tng) (tng) (tng) (tng) (tng) (tng) <value>${jdbc.password}</value>
(tng) (tng) (tng) (tng) (tng) </property>
(tng) (tng) (tng) </bean>
(tng) (tng) (tng)<bean id="sessionFactory"
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
(tng) (tng) (tng) (tng) (tng) <property name="dataSource">
(tng) (tng) (tng) (tng) (tng) (tng) (tng) <ref local="dataSource"/>
(tng) (tng) (tng) (tng) (tng) </property>
(tng) (tng) (tng) (tng) (tng) <property name="mappingResources">
(tng) (tng) (tng) (tng) (tng) (tng) (tng) <value>smartmenu.hbm.xml</value>
(tng) (tng) (tng) (tng) (tng) </property>
(tng) (tng) (tng) (tng) (tng) <property name="hibernateProperties">
(tng) (tng) (tng) (tng) (tng) (tng) (tng) <props>
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) <prop key="hibernate.dialect">${hibernate.dialect}</prop>
(tng) (tng) (tng) (tng) (tng) (tng) (tng) </props>
(tng) (tng) (tng) (tng) (tng) </property>
(tng) (tng) (tng) </bean>
(tng)
(tng) (tng) (tng) <bean id="transactionManager" (tng) (tng) (tng) (tng) (tng) (tng) (tng)
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) class="org.springframework.orm.hibernate.HibernateTransactionManager">
(tng) (tng) (tng) (tng) (tng) <property name="sessionFactory">
(tng) (tng) (tng) (tng) (tng) (tng) (tng) <ref local="sessionFactory"/>
(tng) (tng) (tng) (tng) (tng) </property>
(tng) (tng) (tng) </bean>
(tng) (tng) (tng) <bean id="smartmenuTarget" class="SmartMenuHibernate">
(tng) (tng) (tng) (tng) (tng) <property name="sessionFactory">
(tng) (tng) (tng) (tng) (tng) (tng) (tng) <ref local="sessionFactory"/>
(tng) (tng) (tng) (tng) (tng) </property>
(tng) (tng) (tng) </bean>
(tng) (tng) (tng)<bean id="smartMenu"
(tng) (tng) (tng) (tng) (tng) (tng) (tng) class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
(tng) (tng) (tng) (tng) (tng) <property name="transactionManager">
(tng) (tng) (tng) (tng) (tng) (tng) (tng) <ref local="transactionManager"/>
(tng) (tng) (tng) (tng) (tng) </property>
(tng) (tng) (tng) (tng) (tng) <property name="target">
(tng) (tng) (tng) (tng) (tng) (tng) (tng) <ref local="smartmenuTarget"/>
(tng) (tng) (tng) (tng) (tng) </property>
(tng) (tng) (tng) (tng) (tng) <property name="transactionAttributes">
(tng) (tng) (tng) (tng) (tng) (tng) (tng) <props>
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
(tng) (tng) (tng) (tng) (tng) (tng) (tng) </props>
(tng) (tng) (tng) (tng) (tng) </property>
(tng) (tng) (tng) </bean>
(tng) </beans>
(tng)
(tng) 要想d理解Spring的AOPQ最好还是多看看源码Q开源就是好啊!
java代码: (tng) |
package infoweb.dao; import java.util.List; import java.util.Iterator; import infoweb.pojo.Info; import net.sf.hibernate.HibernateException; import net.sf.hibernate.Query; import net.sf.hibernate.Session; import org.springframework.orm.hibernate.HibernateCallback; import org.springframework.orm.hibernate.support.HibernateDaoSupport; /** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: </p> * @author D|?br />* @version 1.0 */ publicclass InfoDAOImpl extends HibernateDaoSupport implements IInfoDAO { (tng) /** (tng) (tng)* 构造函?br /> (tng) (tng)*/ (tng) public InfoDAOImpl(){ (tng) (tng) (tng) super(); (tng) } (tng) /** (tng) (tng)* 增加记录 (tng) (tng)* @param info Info (tng) (tng)*/ (tng) publicvoid setInfo(Info info)throwsException{ (tng) (tng) (tng) getHibernateTemplate().save(info); (tng) } (tng) /** (tng) (tng)* 通过ID取得记录 (tng) (tng)* @param id String (tng) (tng)* @return Info (tng) (tng)*/ (tng) public Info getInfoById(String id)throwsException{ (tng) (tng) (tng) Info info = (Info) getHibernateTemplate().load(Info.class, id); (tng) (tng) (tng) return info; (tng) } (tng) /** (tng) (tng)* 修改记录 (tng) (tng)* @param Info info (tng) (tng)*/ (tng) publicvoid modifyInfo(Info info)throwsException{ (tng) (tng) (tng) getHibernateTemplate().update(info); (tng) } (tng) /** (tng) (tng)* 删除记录 (tng) (tng)* @param Info info (tng) (tng)*/ (tng) publicvoid removeInfo(Info info)throwsException{ (tng) (tng) (tng) getHibernateTemplate().delete(info); (tng) } (tng) //////////////////////////////////////////////////////// (tng) ///// (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) /// (tng) /////以下部䆾不带审核功能 (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) /// (tng) ///// (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) /// (tng) //////////////////////////////////////////////////////// (tng) /** (tng) (tng)* 取记录L (tng) (tng)* @return int (tng) (tng)*/ (tng) publicint getInfosCount()throwsException{ (tng) (tng) (tng) int count = 0; (tng) (tng) (tng) String queryString = "select count(*) from Info"; (tng) (tng) (tng) count = ((Integer) getHibernateTemplate().iterate(queryString).next()). (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) intValue(); (tng) (tng) (tng) return count; (tng) } (tng) /** (tng) (tng)* 取所有记录集?br /> (tng) (tng)* @return Iterator (tng) (tng)*/ (tng) publicIterator getAllInfos()throwsException{ (tng) (tng) (tng) Iterator iterator = null; (tng) (tng) (tng) String queryString = " select info from Info as info order by info.id desc"; (tng) (tng) (tng) List list = getHibernateTemplate().find(queryString); (tng) (tng) (tng) iterator = list.iterator(); (tng) (tng) (tng) return iterator; (tng) } (tng) /** (tng) (tng)* 取记录集?br /> (tng) (tng)* @return Iterator (tng) (tng)* @param int position, int length (tng) (tng)*/ (tng) publicIterator getInfos(int position, int length)throwsException{ (tng) (tng) (tng) Iterator iterator = null; (tng) (tng) (tng) String queryString = " select info from Info as info order by info.id desc"; (tng) (tng) (tng) Query query = getHibernateTemplate().createQuery(getSession(), queryString); (tng) (tng) (tng) //讄游标的v始点 (tng) (tng) (tng) query.setFirstResult(position); (tng) (tng) (tng) //讄游标的长?/span> (tng) (tng) (tng) query.setMaxResults(length); (tng) (tng) (tng) //记录生成 (tng) (tng) (tng) List list = query.list(); (tng) (tng) (tng) //把查询到的结果放入P代器 (tng) (tng) (tng) iterator = list.iterator(); (tng) (tng) (tng) return iterator; (tng) } (tng) /** (tng) (tng)* 取第一条记?br /> (tng) (tng)* @throws Exception (tng) (tng)* @return Station (tng) (tng)*/ (tng) public Info getFirstInfo()throwsException{ (tng) (tng) (tng) Iterator iterator = null; (tng) (tng) (tng) Info info = null; (tng) (tng) (tng) String queryString = "select info from Info as info order by info.id desc"; (tng) (tng) (tng) Query query = getHibernateTemplate().createQuery(getSession(), queryString); (tng) (tng) (tng) //记录生成 (tng) (tng) (tng) List list = query.list(); (tng) (tng) (tng) //把查询到的结果放入P代器 (tng) (tng) (tng) iterator = list.iterator(); (tng) (tng) (tng) if(iterator.hasNext()){ (tng) (tng) (tng) (tng) (tng) info = (Info) iterator.next(); (tng) (tng) (tng) } (tng) (tng) (tng) return info; (tng) } (tng) /** (tng) (tng)* 取最后一条记?br /> (tng) (tng)* @throws Exception (tng) (tng)* @return Station (tng) (tng)*/ (tng) public Info getLastInfo()throwsException{ (tng) (tng) (tng) Iterator iterator = null; (tng) (tng) (tng) Info info = null; (tng) (tng) (tng) String queryString = "select info from Info as info order by info.id asc"; (tng) (tng) (tng) Query query = getHibernateTemplate().createQuery(getSession(), queryString); (tng) (tng) (tng) //记录生成 (tng) (tng) (tng) List list = query.list(); (tng) (tng) (tng) //把查询到的结果放入P代器 (tng) (tng) (tng) iterator = list.iterator(); (tng) (tng) (tng) if(iterator.hasNext()){ (tng) (tng) (tng) (tng) (tng) info = (Info) iterator.next(); (tng) (tng) (tng) } (tng) (tng) (tng) return info; (tng) } (tng) //////////////////////////////////////////////////////// (tng) ///// (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) /// (tng) ///// 以下部䆾表中要有特定字段才能Õ(li)吩诵袪 牳鋈撕推W禒 (tng) (tng)/// (tng) ///// (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) /// (tng) //////////////////////////////////////////////////////// (tng) /** (tng) (tng)* 取符合条件记录L, [表中要有 isperson 字段] (tng) (tng)* @return int (tng) (tng)* @param int isPerson (tng) (tng)*/ (tng) publicint getInfosCountByIsperson(int isPerson)throwsException{ (tng) (tng) (tng) int count = 0; (tng) (tng) (tng) String queryString = (tng) (tng) (tng) (tng) (tng) (tng) (tng) "select count(*) from Info as info where info.isperson =" + isPerson; (tng) (tng) (tng) count = ((Integer) getHibernateTemplate().iterate(queryString).next()). (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) intValue(); (tng) (tng) (tng) return count; (tng) } (tng) /** (tng) (tng)* 取所有符合条件记录集? 模糊查询条g.[表中要有 isperson 字段] (tng) (tng)* @return Iterator (tng) (tng)* @param int isPerson (tng) (tng)*/ (tng) publicIterator getAllInfosByIsperson(int isPerson)throwsException{ (tng) (tng) (tng) Iterator iterator = null; (tng) (tng) (tng) String queryString = " select info from Info as info where info.isperson =" + (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)isPerson + " order by info.id desc"; (tng) (tng) (tng) List list = getHibernateTemplate().find(queryString); (tng) (tng) (tng) //把查询到的结果放入P代器 (tng) (tng) (tng) iterator = list.iterator(); (tng) (tng) (tng) return iterator; (tng) } (tng) /** (tng) (tng)* 取符合条件记录集? 模糊查询条g.[表中要有 isperson 字段] (tng) (tng)* @return Iterator (tng) (tng)* @param int isPerson,int position, int length (tng) (tng)*/ (tng) publicIterator getInfosByIsperson(int isPerson, int position, int length)throws (tng) (tng) (tng) (tng) (tng) Exception{ (tng) (tng) (tng) Iterator iterator = null; (tng) (tng) (tng) String queryString = " select info from Info as info where info.isperson =" + (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)isPerson + " order by info.id desc"; (tng) (tng) (tng) //创徏查询 (tng) (tng) (tng) Query query = getHibernateTemplate().createQuery(getSession(), queryString); (tng) (tng) (tng) //讄游标的v始点 (tng) (tng) (tng) query.setFirstResult(position); (tng) (tng) (tng) //讄游标的长?/span> (tng) (tng) (tng) query.setMaxResults(length); (tng) (tng) (tng) //记录生成 (tng) (tng) (tng) List list = query.list(); (tng) (tng) (tng) //把查询到的结果放入P代器 (tng) (tng) (tng) iterator = list.iterator(); (tng) (tng) (tng) return iterator; (tng) } (tng) //////////////////////////////////////////////////////// (tng) ///// (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) /// (tng) ///// 以下部䆾表中要有特定字段才能Õ(li)吩诵袪 (tng)查询部䆾 (tng) (tng) (tng) (tng) (tng) /// (tng) ///// (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) /// (tng) /////////////////////////////////////////////////////// (tng) /** (tng) (tng)* 取符合条件记录L, 模糊查询条g.[表中要有 title 字段] (tng) (tng)* @return int (tng) (tng)* @param String text (tng) (tng)*/ (tng) publicint getInfosCount(String text)throwsException{ (tng) (tng) (tng) int count = 0; (tng) (tng) (tng) count = ((Integer) getHibernateTemplate().iterate( (tng) (tng) (tng) (tng) (tng) (tng) (tng) "select count(*) from Info as info where info.title like '%" + text + (tng) (tng) (tng) (tng) (tng) (tng) (tng) "%'").next()).intValue(); (tng) (tng) (tng) return count; (tng) } (tng) /** (tng) (tng)* 取所有符合条件记录集? 模糊查询条g.[表中要有 title 字段] (tng) (tng)* @return Iterator (tng) (tng)* @param String text (tng) (tng)*/ (tng) publicIterator getAllInfos(String text)throwsException{ (tng) (tng) (tng) Iterator iterator = null; (tng) (tng) (tng) String queryString = (tng) (tng) (tng) (tng) (tng) (tng) (tng) " select info from Info as info where info.title like '%" + text + (tng) (tng) (tng) (tng) (tng) (tng) (tng) "%' order by info.id desc"; (tng) (tng) (tng) //创徏查询 (tng) (tng) (tng) Query query = getHibernateTemplate().createQuery(getSession(), queryString); (tng) (tng) (tng) //记录生成 (tng) (tng) (tng) List list = query.list(); (tng) (tng) (tng) //把查询到的结果放入P代器 (tng) (tng) (tng) iterator = list.iterator(); (tng) (tng) (tng) return iterator; (tng) } (tng) /** (tng) (tng)* 取符合条件记录集? 模糊查询条g.[表中要有 title 字段] (tng) (tng)* @return Iterator (tng) (tng)* @param String text,int position, int length (tng) (tng)*/ (tng) publicIterator getInfos(String text, int position, int length)throws (tng) (tng) (tng) (tng) (tng) Exception{ (tng) (tng) (tng) Iterator iterator = null; (tng) (tng) (tng) String queryString = (tng) (tng) (tng) (tng) (tng) (tng) (tng) " select info from Info as info where info.title like '%" + text + (tng) (tng) (tng) (tng) (tng) (tng) (tng) "%' order by info.id desc"; (tng) (tng) (tng) //创徏查询 (tng) (tng) (tng) Query query = getHibernateTemplate().createQuery(getSession(), queryString); (tng) (tng) (tng) //讄游标的v始点 (tng) (tng) (tng) query.setFirstResult(position); (tng) (tng) (tng) //讄游标的长?/span> (tng) (tng) (tng) query.setMaxResults(length); (tng) (tng) (tng) //记录生成 (tng) (tng) (tng) List list = query.list(); (tng) (tng) (tng) //把查询到的结果放入P代器 (tng) (tng) (tng) iterator = list.iterator(); (tng) (tng) (tng) return iterator; (tng) } (tng) //////////////////////////////////////////////////////// (tng) ///// (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) /// (tng) ///// 以下部䆾表中要有特定字段才能Õ(li)吩诵袪 犠⒉嵯喙? (tng) (tng) (tng) /// (tng) ///// (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) /// (tng) //////////////////////////////////////////////////////// (tng) /** (tng) (tng)* 取符合条件记录L.[ 表中要有 registername 字段] (tng) (tng)* @return int (tng) (tng)* @param String text (tng) (tng)*/ (tng) publicint getInfosCountByRegisterName(String registerName)throwsException{ (tng) (tng) (tng) int count = 0; (tng) (tng) (tng) count = ((Integer) getHibernateTemplate().iterate( (tng) (tng) (tng) (tng) (tng) (tng) (tng) "select count(*) from Info as info where info.registername = '" + (tng) (tng) (tng) (tng) (tng) (tng) (tng) registerName + "'").next()).intValue(); (tng) (tng) (tng) return count; (tng) } (tng) /** (tng) (tng)* 通过注册名取得一条记?如有多条,只取W一?[表中要有 registername字段] (tng) (tng)* @param registername String (tng) (tng)* @return Info (tng) (tng)*/ (tng) public Info getInfoByRegisterName(String registerName)throwsException{ (tng) (tng) (tng) Iterator iterator = null; (tng) (tng) (tng) Info info = null; (tng) (tng) (tng) String queryString = (tng) (tng) (tng) (tng) (tng) (tng) (tng) " select info from Info as info where info.registername='" + (tng) (tng) (tng) (tng) (tng) (tng) (tng) registerName + "' order by info.id desc"; (tng) (tng) (tng) //创徏查询 (tng) (tng) (tng) Query query = getHibernateTemplate().createQuery(getSession(), queryString); (tng) (tng) (tng) //记录生成 (tng) (tng) (tng) List list = query.list(); (tng) (tng) (tng) //把查询到的结果放入P代器 (tng) (tng) (tng) iterator = list.iterator(); (tng) (tng) (tng) if(iterator.hasNext()){ (tng) (tng) (tng) (tng) (tng) info = (Info) iterator.next(); (tng) (tng) (tng) } (tng) (tng) (tng) return info; (tng) } (tng) /** (tng) (tng)* 通过注册名取得所有记录集?[表中要有 registername字段] (tng) (tng)* @param registername String (tng) (tng)* @return Iterator (tng) (tng)*/ (tng) publicIterator getAllInfosByRegisterName(String registerName)throws (tng) (tng) (tng) (tng) (tng) Exception{ (tng) (tng) (tng) Iterator iterator = null; (tng) (tng) (tng) String queryString = (tng) (tng) (tng) (tng) (tng) (tng) (tng) " select info from Info as info where info.registername='" + (tng) (tng) (tng) (tng) (tng) (tng) (tng) registerName + "' order by info.id desc"; (tng) (tng) (tng) //创徏查询 (tng) (tng) (tng) Query query = getHibernateTemplate().createQuery(getSession(), queryString); (tng) (tng) (tng) //记录生成 (tng) (tng) (tng) List list = query.list(); (tng) (tng) (tng) //把查询到的结果放入P代器 (tng) (tng) (tng) iterator = list.iterator(); (tng) (tng) (tng) return iterator; (tng) } (tng) /** (tng) (tng)* 通过注册名取得记录列?[表中要有 registername字段] (tng) (tng)* @param registername String (tng) (tng)* @return Iterator (tng) (tng)*/ (tng) publicIterator getInfosByRegisterName(String registerName, int position, (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)int length)throwsException{ (tng) (tng) (tng) Iterator iterator = null; (tng) (tng) (tng) String queryString = (tng) (tng) (tng) (tng) (tng) (tng) (tng) " select info from Info as info where info.registername='" + (tng) (tng) (tng) (tng) (tng) (tng) (tng) registerName + "' order by info.id desc"; (tng) (tng) (tng) //创徏查询 (tng) (tng) (tng) Query query = getHibernateTemplate().createQuery(getSession(), queryString); (tng) (tng) (tng) //讄游标的v始点 (tng) (tng) (tng) query.setFirstResult(position); (tng) (tng) (tng) //讄游标的长?/span> (tng) (tng) (tng) query.setMaxResults(length); (tng) (tng) (tng) //记录生成 (tng) (tng) (tng) List list = query.list(); (tng) (tng) (tng) //把查询到的结果放入P代器 (tng) (tng) (tng) iterator = list.iterator(); (tng) (tng) (tng) return iterator; (tng) } (tng) //////////////////////////////////////////////////////// (tng) ///// (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) /// (tng) ///// 以下部䆾表中要有特定字段才能Õ(li)吩诵袪 (tng) 犑餍桶婵? (tng) (tng)/// (tng) ///// (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) /// (tng) //////////////////////////////////////////////////////// (tng) /** (tng) (tng)* 取记录L.[ 表中要有 board_id 字段] (tng) (tng)* @return int (tng) (tng)* @param String boardId (tng) (tng)*/ (tng) publicint getInfosCountByBoard(String boardId)throwsException{ (tng) (tng) (tng) int count = 0; (tng) (tng) (tng) count = ((Integer) getHibernateTemplate().iterate( (tng) (tng) (tng) (tng) (tng) (tng) (tng) "select count(*) from Info as info where info.boardId = '" + boardId + (tng) (tng) (tng) (tng) (tng) (tng) (tng) "'").next()).intValue(); (tng) (tng) (tng) return count; (tng) } (tng) /** (tng) (tng)* 通过版块名取得所有记录集?[表中要有 board_id字段] (tng) (tng)* @param BoardId String (tng) (tng)* @return Iterator (tng) (tng)*/ (tng) publicIterator getAllInfosByBoard(String boardId)throwsException{ (tng) (tng) (tng) Iterator iterator = null; (tng) (tng) (tng) String queryString = " select info from Info as info where info.boardId='" + (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)boardId + "' order by info.id desc"; (tng) (tng) (tng) //创徏查询 (tng) (tng) (tng) Query query = getHibernateTemplate().createQuery(getSession(), queryString); (tng) (tng) (tng) //记录生成 (tng) (tng) (tng) List list = query.list(); (tng) (tng) (tng) //把查询到的结果放入P代器 (tng) (tng) (tng) iterator = list.iterator(); (tng) (tng) (tng) return iterator; (tng) } (tng) /** (tng) (tng)* 通过版块名取得记录列?[表中要有 board_id字段] (tng) (tng)* @param BoardId String (tng) (tng)* @return Iterator (tng) (tng)*/ (tng) publicIterator getInfosByBoard(String boardId, int position, int length)throws (tng) (tng) (tng) (tng) (tng) Exception{ (tng) (tng) (tng) Iterator iterator = null; (tng) (tng) (tng) String queryString = " select info from Info as info where info.boardId='" + (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)boardId + "' order by info.id desc"; (tng) (tng) (tng) //创徏查询 (tng) (tng) (tng) Query query = getHibernateTemplate().createQuery(getSession(), queryString); (tng) (tng) (tng) //讄游标的v始点 (tng) (tng) (tng) query.setFirstResult(position); (tng) (tng) (tng) //讄游标的长?/span> (tng) (tng) (tng) query.setMaxResults(length); (tng) (tng) (tng) //记录生成 (tng) (tng) (tng) List list = query.list(); (tng) (tng) (tng) //把查询到的结果放入P代器 (tng) (tng) (tng) iterator = list.iterator(); (tng) (tng) (tng) return iterator; (tng) } (tng) /** (tng) (tng)* 取符合条件记录L.[ 表中要有 board_id 字段,title] (tng) 模糊查询title (tng) (tng)* @return int (tng) (tng)* @param String boardId ,String text (tng) (tng)*/ (tng) publicint getInfosCountByBoard(String boardId, String text)throwsException{ (tng) (tng) (tng) int count = 0; (tng) (tng) (tng) count = ((Integer) getHibernateTemplate().iterate( (tng) (tng) (tng) (tng) (tng) (tng) (tng) "select count(*) from Info as info where info.boardId='" + boardId + (tng) (tng) (tng) (tng) (tng) (tng) (tng) "' and info.title like '%" + text + "%'").next()).intValue(); (tng) (tng) (tng) return count; (tng) } (tng) /** (tng) (tng)* 通过版块名取得记录列?[表中要有 board_id字段] (tng) 模糊查询title (tng) (tng)* @param String boardID,int position, int length (tng) (tng)* @return Iterator (tng) (tng)*/ (tng) publicIterator getInfosByBoard(String boardId, int position, int length, (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) String text)throwsException{ (tng) (tng) (tng) Iterator iterator = null; (tng) (tng) (tng) String queryString = " select info from Info as info where info.boardId='" + (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)boardId + "' and info.title like '%" + text + (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)"%' order by info.id desc"; (tng) (tng) (tng) //创徏查询 (tng) (tng) (tng) Query query = getHibernateTemplate().createQuery(getSession(), queryString); (tng) (tng) (tng) //讄游标的v始点 (tng) (tng) (tng) query.setFirstResult(position); (tng) (tng) (tng) //讄游标的长?/span> (tng) (tng) (tng) query.setMaxResults(length); (tng) (tng) (tng) //记录生成 (tng) (tng) (tng) List list = query.list(); (tng) (tng) (tng) //把查询到的结果放入P代器 (tng) (tng) (tng) iterator = list.iterator(); (tng) (tng) (tng) return iterator; (tng) } (tng) //////////////////////////////////////////////////////// (tng) ///// (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) /// (tng) /////以下部䆾带有审核功能 (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) /// (tng) ///// (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) /// (tng) //////////////////////////////////////////////////////// (tng) /** (tng) (tng)* 取记录L (tng) (tng)* @return int (tng) (tng)* @param int isAuditing (tng) (tng)*/ (tng) publicint getInfosCount(int isAuditing)throwsException{ (tng) (tng) (tng) |
Spring Framework最得以出名的是与Hibernate的无~链接,基本上用SpringQ就?x)用Hibernate。可惜的是Spring提供的HibernateTemplate功能昑־不够Q用v来也不是很方ѝ我们编E序Ӟ一般先写BusinessServiceQ由BusinessService调DAO来执行存储,在这斚wSpring没有很好的例子,造成真正想用好它Qƈ不容易?/font> 我们的思\是先写一个BaseDaoQ仿照HibernateTemplateQ将基本功能全部实现Q?/font> public class BaseDao extends HibernateDaoSupport{ (tng) (tng) (tng) private Log log = LogFactory.getLog(getClass());
(tng) (tng) (tng) public Session openSession() {
(tng) (tng) (tng) public Object get(Class entityClass, Serializable id) throws DataAccessException {
(tng) (tng) (tng) public Serializable create(Object entity) throws DataAccessException { ... 其它的DAOQ从BaseDaol承出来Q这样写其他的DAOQ代码就?x)很?/font> 从BaseDaol承出来EntityDaoQ专门负责一般实体的基本操作Q会(x)更方ѝ?/font> public interface EntityDao { (tng) (tng) (tng) public Object get(Class entityClass, Serializable id) throws DataAccessException; (tng) (tng) (tng) public Object load(Class entityClass, Serializable id) throws DataAccessException;
(tng) (tng) (tng) public Serializable create(Object entity) throws DataAccessException;
/** } Z(jin)Transaction的控Ӟ采用AOP的方式:(x) public interface EntityManager { (tng) (tng) (tng) public Object get(Class entityClass, Serializable id); (tng) (tng) (tng) public Object load(Class entityClass, Serializable id);
(tng) (tng) (tng) public Serializable create(Object entity); }
/** (tng) (tng) (tng) private EntityDao entityDao;
(tng) (tng) (tng) public void setEntityDao(EntityDao entityDao) {
(tng) (tng) (tng) public Object get(Class entityClass, Serializable id) {
(tng) (tng) (tng) public Object load(Class entityClass, Serializable id) { } q样我们有?jin)一个通用的Hibernate实体引擎Q可以对MHibernate实体实现基本的增加、修攏V删除、查询等?/font> 其它的BusinessService可以(h)承EntityManagerQ快速实C务逻辑?/font> 具体XML配置如下Q?/font>
(tng)<!-- Oracle JNDI DataSource for J2EE environments -->
(tng)<!-- Hibernate SessionFactory for Oracle -->
(tng)<!-- AOP DAO Intecepter -->
(tng) (tng) (tng) (tng) (tng) (tng) (tng) <bean id="entityDaoTarget" class="com.gpower.services.entity.dao.EntityDaoImpl">
(tng) (tng) (tng) (tng) (tng) (tng) (tng) <bean id="entityDao" class="org.springframework.aop.framework.ProxyFactoryBean">
(tng)<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
(tng)<!-- Transaction manager that delegates to JTA (for a transactional JNDI DataSource) -->
(tng)<!-- Transactional proxy for the Application primary business object -->
(tng) (tng) (tng) (tng) (tng) (tng) (tng) <bean id="entityManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> |
![]() |