spring配置ibatis直至dao层

本文详细介绍了如何在Spring中配置Ibatis,包括web.xml中的上下文配置、applicationContext.xml中Ibatis信息设置、定义抽象DAO父类、单例模式的SqlMapClient实现以及DAO层的实现,使得在DAO实现类中可以直接调用getSqlMapClient().queryFor...进行数据库操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、web.xml配置用spring托管

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/conf/spring/applicationContext.xml</param-value>
 </context-param>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

2、applicationContext.xml里面配置ibatis的信息

<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean" scope="prototype">
  <property name="configLocation" value="WEB-INF/conf/ibatis/sqlMapConfig.xml" />   ----->sqlMapConfig.xml里面包含连接池和ibatis配置
  <property name="dataSource" ref="dataSource"/>                                                                 ----->前面配置了数据库连接池
 </bean>

 <bean id="daoparent" abstract="true">
  <property name="sqlMapClient" ref="sqlMapClient" />
 </bean>
 
 <bean class="com.huawei.common.dao.SingleSqlMapClinet" parent="daoparent"></bean>

3、SingleSqlMapClinet extends SqlMapClientDaoSupport implements BeanPostProcessor  ---->单例模式的sqlMapClient

private static SqlMapClient sqlMapClient = null;

 public static SqlMapClient fetchSqlMapClient() {
  return sqlMapClient;
 }

 @Override
 public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
  synchronized (SingleSqlMapClinet.class) {
   if (sqlMapClient == null) {
    sqlMapClient = getSqlMapClient();
   }
  }
  return arg0;
 }

 @Override
 public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
  return arg0;
 }

4、BasicDao extends SqlMapClientDaoSupport 里面使用到了SingleSqlMapClinet 的创建sqlMapClient

public BasicDao() {
  SqlMapClientTemplate template = getSqlMapClientTemplate();
  if (template.getSqlMapClient() == null) {
   template.setSqlMapClient(SingleSqlMapClinet.fetchSqlMapClient());
  }
 }

5、DAO的实现类继承BasicDao,对数据库的增删改查操作就直接可以调用getSqlMapClient().queryFor...方法

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值