11. DAO使用进阶

来源:互联网 发布:js中hasclass 编辑:程序博客网 时间:2024/06/15 21:17


1. 不是基于SQLMap的DAO实现


1.1 Hibernate版本的DAO实现


1. 定义DAO上下文


Dao.xml中定义使用Hibernate的DAO上下文:

<context id="hibernate"><transactionManager type="HIBERNATE"><property name="hibernate.connection.driver_class" value="org.postgresql.Driver" /><property name="hibernate.connection.url" value="jdbc:postgresql:ibatisdemo" /><property name="hibernate.connection.username" value="ibatis" /><property name="hibernate.connection.password" value="ibatis" /><property name="hibernate.connection.pool_size" value="5" /><property name="hibernate.dialect" value="net.sf.hibernate.dialect.PostgreSQLDialect" /><property name="map.Account" value="${DaoHomeRes}/hibernate/Account.hbm.xml" /></transactionManager><dao interface="${DaoHome}.AccountDao" implementation="${DaoHome}.hibernate.AccountDaoImpl" /></context>

2. 映射Account表


hibernate映射文件:

<?xml version="1.0"?><!DOCTYPE hibernate-mappingPUBLIC "-//Hibernate/Hibernate Mapping DTD//EN""http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping><class name="org.apache.mapper2.examples.bean.Account" table="Account"><id name="accountId" type="int" column="accountid"><generator class="sequence"><param name="sequence">account_accountid_seq</param></generator></id><property name="username" /><property name="password" /><property name="firstName" /><property name="lastName" /><property name="address1" /><property name="address2" /><property name="city" /><property name="state" /><property name="postalCode" /><property name="country" /></class></hibernate-mapping>


3. 实际的DAO实现


<pre name="code" class="java">public class AccountDaoImpl extends HibernateDaoTemplate implements AccountDao {private static final Log log = LogFactory.getLog(AccountDaoImpl.class);public AccountDaoImpl(DaoManager daoManager) {super(daoManager);if (log.isDebugEnabled()) {log.debug("Creating instance of " + getClass());}}public Integer insert(Account account) {try {getSession().save(account);} catch (HibernateException e) {log.error(e);throw new DaoException(e);}return account.getAccountId();}public int update(Account account) {try {getSession().save(account);} catch (HibernateException e) {log.error(e);throw new DaoException(e);}return 1;}public int delete(Account account) {try {getSession().delete(account);} catch (HibernateException e) {log.error(e);throw new DaoException(e);}return 1;}public int delete(Integer accountId) {Account account = new Account();account.setAccountId(accountId);return delete(account);}public List<Account> getAccountListByExample(Account acct) {List accountList;Session session = this.getSession();Criteria criteria =session.createCriteria(Account.class);if (!nullOrEmpty(acct.getCity())) {criteria.add(Expression.like("city", acct.getCity()));}If (!nullOrEmpty(acct.getAccountId())) {criteria.add(Expression.eq("accountId", acct.getAccountId()));}try {accountList = criteria.list();} catch (HibernateException e) {log.error("Exception getting list: " +e.getLocalizedMessage(), e);throw new DaoException(e);}return (List<Account>)accountList;}public List<Map<String, Object>> getMapListByExample(Account account) {List<Account> accountList = getAccountListByExample(account);List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();for (Account acctToAdd : accountList) {Map<String, Object> map = new HashMap<String, Object>();map.put("accountId", acctToAdd.getAccountId());map.put("address1", acctToAdd.getAddress1());map.put("address2", acctToAdd.getAddress2());map.put("city", acctToAdd.getCity());map.put("country", acctToAdd.getCountry());map.put("firstName", acctToAdd.getFirstName());map.put("lastName", acctToAdd.getLastName());map.put("password", acctToAdd.getPassword());map.put("postalCode", acctToAdd.getPostalCode());map.put("state", acctToAdd.getState());map.put("username", acctToAdd.getUsername());mapList.add(map);}return mapList;}public List<IdDescription> getIdDescriptionListByExample(Account exAcct) {List<Account> acctList = getAccountListByExample(exAcct);List<IdDescription> idDescriptionList = new ArrayList<IdDescription>();for (Account acct : acctList) {idDescriptionList.add(new IdDescription(acct.getAccountId(), acct.getFirstName() + " " + acct.getLastName()));}return idDescriptionList;}public Account getById(Integer accountId) {Session session = this.getSession();try {return (Account) session.get(Account.class, accountId);} catch (HibernateException e) {log.error(e);throw new DaoException(e);}}public Account getById(Account account) {return getById(account.getAccountId());}}



1.2 JDBC版本的DAO实现


dao.xml配置文件

<context id="jdbc"><transactionManager type="JDBC"><property name="DataSource" value="SIMPLE" /><property name="JDBC.Driver" value="org.postgresql.Driver" /><property name="JDBC.ConnectionURL" value="jdbc:postgresql:ibatisdemo" /><property name="JDBC.Username" value="ibatis" /><property name="JDBC.Password" value="ibatis" /><property name="JDBC.DefaultAutoCommit" value="true" /></transactionManager><dao interface="${DaoHome}.AccountDao" implementation="${DaoHome}.jdbc.AccountDaoImpl" /></context>


2. 为其他数据源使用DAO模式


2.1  示例:为LDAP使用DAO


2.2 示例:为Web服务使用DAO


3. 使用Spring DAO


3.1  编写代码


Spring框架通过一个针对数据访问对象的模板模式来支持ibatis,即, DAO实现可以从扩展Spring框架中一个现成的SqlMapClientTemplate开始


SQLMap的Account DAO的Spring版本:

public class AccountDaoImplSpring extends SqlMapClientTemplate implements AccountDao {public Integer insert(Account account) {return (Integer) insert("Account.insert", account);}public int update(Account account) {return update("Account.update", account);}public int delete(Account account) {return delete(account.getAccountId());}public int delete(Integer accountId) {return delete("Account.delete", accountId);}public List<Account> getAccountListByExample(Account account) {return queryForList("Account.getAccountListByExample", account);}public List<Map<String, Object>> getMapListByExample(Account account) {return queryForList("Account.getMapListByExample", account);}public List<IdDescription> getIdDescriptionListByExample(Account account) {return queryForList("Account.getIdDescriptionListByExample", account);}public Account getById(Integer accountId) {return (Account) queryForObject("Account.getById", accountId);}public Account getById(Account account) {return (Account) queryForList("Account.getById", account);}}



























0 0