答案:
1.首先实现一个接口.
============================================================
package com.cqtele.tnbos;import java.util.*;public inte易做图ce IUserDao { public void insertUser(userInfo user); public List findUser(String sql);}
==============================================================
2. 实现这个接口.
这里使用 Spring的 AOP-driven HibernateTemplate 简化 Hibernate 会话的处理.
================================================================package com.cqtele.tnbos;import java.util.*;
import net.sf.hibernate.*;import net.sf.hibernate.cfg.*;import java.io.Serializable;import org.springframework.orm.hibernate.*;//.HibernateCallback;import org.springframework.orm.hibernate.support.HibernateDaoSupport;
public class userDAO extends HibernateDaoSupport implements IUserDao{ //public List name=null; //Session session; private List list=null; public void insertUser(userInfo user) { getHibernateTemplate().saveOrUpdate(user); } public List findUser(String sql) { return getHibernateTemplate().find(sql); }}
=================================================================
3.声明式管理事务.
bean.xml
==========================================================================
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://127.0.0.1:3306/opertselect?useUnicode=true&characterEncoding=GBK</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value></value> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="dataSource" /> </property> <property name="mappingResources"> <list> <value>userInfo.hbm.xml</value> <!--value>Fruits.hbm.xml</value--> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> net.sf.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <bean id="userDAO" class="com.cqtele.tnbos.userDAO"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <bean id="DAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager" /> </property> <property name="target"> <ref local="userDAO" /> </property> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean></beans>==============================================================================
4.使用.(这是关于一个购物车的例子.)==============================================================================
package com.cqtele.tnbos;
import java.util.*;import java.io.*;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.aopalliance.aop.Advice;
public class login{ private List list=null; private String sql="from userInfo"; public List getAllMessageList() { try{ InputStream is = new FileInputStream("bean.xml"); XmlBeanFactory factory = new XmlBeanFactory(is); IUserDao user = (IUserDao)factory.getBean("DAOProxy"); list = user.findUser(sql); }catch(IOException ioe) { System.out.println("getAllMessageList Ocurr Error!! Message: "+ioe.getMessage()); } return list; }}
======================================================================
5.总结
======================================================================
Hibernate 是一种先进的 OR 映射工具,而 Spring 是一个 AOP 框架和 IOC 容器。这两种技术的综合使用,使得开发人员可以编写媲美数据库厂商的代码,它可以在 J2EE 容器中运行,也可以单独运行.
上一个:继续acegi -> 0.8
下一个:swt tray demo:右键菜单,单击隐藏,所有事件的例子