iBATIS Spring Struts2整合实例浅析
iBATIS + Spring整合例子,今天补充一个新Spring + iBATIS整合的方式 ,这种方式使通过Spring注入SqlMapClientTemplate完成的。具体模式如下:
- /*
- * UserDAOImpl3.java
- */
- package net.chinaideal.samples.ibatis.dao;
- import org.springframework.orm.ibatis.SqlMapClientTemplate;
- import net.chinaideal.samples.ibatis.model.User;
- /**
- * SpringiBatis - UserDAOImpl3.java
- * ----------------------------------------------
- * @version 0.1 2007-1-23
- * @author Panglei(bj02267)
- * ----------------------------------------------
- * 说明:
- * 整合可以将Spring的iBatis的SqlMapClientTemplate单独注入
- */
- public class UserDAOImpl3 implements UserDAO {
- private static final long serialVersionUID = -6615310540042830594L;
- //iBatis SqlMapClientTemplate
- protected SqlMapClientTemplate sqlMapClientTemplate;
- /* (non-Javadoc)
- * @see net.chinaideal.samples.ibatis.dao.UserDAO#getUserByUsername(java.lang.String)
- */
- public User getUserByUsername(String username) {
- return (User) sqlMapClientTemplate.queryForObject("getUserbyUsername", username);
- }
- public SqlMapClientTemplate getSqlMapClientTemplate() {
- return sqlMapClientTemplate;
- }
- public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
- this.sqlMapClientTemplate = sqlMapClientTemplate;
- }
- }
applicationContext.xml文件:
- ﹤!-- Spring iBatis SqlMapClient --﹥
- ﹤bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"﹥
- ﹤property name="configLocation" value="/WEB-INF/sqlmap-config.xml"/﹥
- ﹤property name="dataSource" ref="dataSource"/﹥
- ﹤/bean﹥
- ﹤!-- Spring iBatisTemplate --﹥
- ﹤bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate"﹥
- ﹤property name="sqlMapClient"﹥
- ﹤ref bean="sqlMapClient"/﹥
- ﹤/property﹥
- ﹤/bean﹥
- ﹤!-- UserDAO方案三 --﹥
- ﹤bean id="userDAO3" class="net.chinaideal.samples.ibatis.dao.UserDAOImpl3"﹥
- ﹤property name="sqlMapClientTemplate"﹥
- ﹤ref bean="sqlMapClientTemplate"/﹥
- ﹤/property﹥
- ﹤/bean﹥
再次基础上又增加Struts2作为整个应用的Web框架,由于Struts2和WebWork2没有什么大的区别.
那么iBATIS Spring Struts2整合的实例就向你介绍到这里,希望这些实例对你有所帮助。
【编辑推荐】