spring+ibatis在spring配置文件中注入sqlmapclient时,一直报错,详情代码见下面 50
Nouniquebeanoftype[com.ibatis.sqlmap.client.SqlMapClient]isdefined:expectedsinglematc...
No unique bean of type [com.ibatis.sqlmap.client.SqlMapClient] is defined: expected single matching bean but found 2: [sqlMapClient, sqlMapClient2]
我装载了两个sqlmapClient,sqlMaPclient2,
<bean id="sqlMapClient2"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource1" />
<property name="configLocation" value="classpath:ibatisxml/sqlmap-configYGZ.xml" />
</bean>
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:ibatisxml/sqlmap-config.xml" />
</bean> 展开
我装载了两个sqlmapClient,sqlMaPclient2,
<bean id="sqlMapClient2"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource1" />
<property name="configLocation" value="classpath:ibatisxml/sqlmap-configYGZ.xml" />
</bean>
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:ibatisxml/sqlmap-config.xml" />
</bean> 展开
1个回答
展开全部
spring3.0注入sqlMapClient的几种方式:
bean定义:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/cms" />
<property name="username" value="root" />
<property name="password" value="19860619" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:/context/ibatis/sqlMapConfig.xml
</value>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
a.在context中装载sqlMapClient并建立sqlMapClientfactory,每次执行sql操作都从factory中获取sqlMapClient.factory获取bean方法:
WebApplicationContext wac = ContextLoader
.getCurrentWebApplicationContext();
SqlMapClient sqlMapClient = wac.getBean("sqlMapClient",
SqlMapClient.class);
b.在每个DAO中使用spring注解注入sqlMapClient:
@Autowired
@Qualifier("sqlMapClient")
private SqlMapClient sqlMapClient;
c.创建BaseDao继承SqlMapClientDaoSupport,所有DAO都继承BaseDao.BaseDao中注入sqlMapClient:
@Autowired
@Qualifier("sqlMapClient")
public void setSqlMapClientForAutowired(SqlMapClient sqlMapClient) {
super.setSqlMapClient(sqlMapClient);
}
该方法实际上是使用了sqlMapclientTemplate并向template注入sqlMapClient。
bean定义:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/cms" />
<property name="username" value="root" />
<property name="password" value="19860619" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:/context/ibatis/sqlMapConfig.xml
</value>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
a.在context中装载sqlMapClient并建立sqlMapClientfactory,每次执行sql操作都从factory中获取sqlMapClient.factory获取bean方法:
WebApplicationContext wac = ContextLoader
.getCurrentWebApplicationContext();
SqlMapClient sqlMapClient = wac.getBean("sqlMapClient",
SqlMapClient.class);
b.在每个DAO中使用spring注解注入sqlMapClient:
@Autowired
@Qualifier("sqlMapClient")
private SqlMapClient sqlMapClient;
c.创建BaseDao继承SqlMapClientDaoSupport,所有DAO都继承BaseDao.BaseDao中注入sqlMapClient:
@Autowired
@Qualifier("sqlMapClient")
public void setSqlMapClientForAutowired(SqlMapClient sqlMapClient) {
super.setSqlMapClient(sqlMapClient);
}
该方法实际上是使用了sqlMapclientTemplate并向template注入sqlMapClient。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询