用法一:
先在Dao里注入template,然后再在Template 里注入sqlMapClient。
Spring配置文件如下:
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:ibatis/SqlMapConfig.xml</value>
</property>
<!-- 这里使用的数据源就是上面配置的数据源 -->
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<bean id="sqlMapClientTemplate" class=
"org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>
<!-- 定义DAO Bean-->
<bean id="memberDao" class=
"com.lxx.dao.impl.MemberDaoImpl">
<property name="sqlMapClientTemplate" ref="sqlMapClientTemplate"/>
</bean>
<!-- 配置一个业务逻辑组件 -->
<bean id="memberService" class=
"com.lxx.service.impl.MemberServiceImpl">
<!-- 注入持久化访问所需的DAO组件 -->
<property name="memberDao" ref="memberDao"/>
</bean>
Dao的实现类:
import org.springframework.orm.ibatis.SqlMapClientTemplate;
public class MemberDaoImpl implements MemberDao {
private SqlMapClientTemplate sqlMapClientTemplate;
public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
this.sqlMapClientTemplate = sqlMapClientTemplate;
}
/**
* 查找所有用户
*
* @param userId
* @return
*/
public List findAllMember(){
List list=sqlMapClientTemplate.queryForList("member.getMemberList");
return list;
}
}
用法二:
在Dao里直接注入sqlMapClient,然后Dao的实现类里继承SqlMapClientDaoSupport类。
Spring配置文件如下:
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:ibatis/SqlMapConfig.xml</value>
</property>
<!-- 这里使用的数据源就是上面配置的数据源 -->
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- 定义DAO Bean-->
<bean id="memberDao" class=
"com.lxx.dao.impl.MemberDaoImpl">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
<!-- 配置一个业务逻辑组件 -->
<bean id="memberService" class=
"com.lxx.service.impl.MemberServiceImpl">
<!-- 注入持久化访问所需的DAO组件 -->
<property name="memberDao" ref="memberDao"/>
</bean>
Dao的实现类:
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
public class MemberDaoImpl extends SqlMapClientDaoSupport implements MemberDao {
/**
* 查找所有用户
*
* @param userId
* @return
*/
public List findAllMember(){
List list=getSqlMapClientTemplate().queryForList("member.getMemberList");
return list;
}
}