1. 在做spring + ibatis整合时,报异常: No SqlMapClient specified
延伸:之前做spring+hibernate整合是也会报类似的错误
异常:java.lang.IllegalArgumentException: No SqlMapClient specified
原因:创建DAO或service对象时使用了 newxxxDaoImpl()或new xxxServiceImpl()的方法;主要原因是使用spring容器的时候不可以使用new对象。
解决:
(1).应该使用依赖注入的方法,即set注入;注入的时候一定要注意:配置文件中的对象名称要与set注入的名称相同;例:
private static WikiSentMsgDAO wmd;
public WikiSentMsgDAOgetWmd() {
return wmd;
}
public voidsetWmd(WikiSentMsgDAO wmd) {
this.wmd = wmd;
}
Xml配置文件中:
(2).应该用使用上下文对象获取到dao或service对象;例子:
做法一:
WebApplicationContext beanFactory =WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
ItemCheckTaskDao itemCheckTaskDao = (ItemCheckTaskDao)beanFactory.getBean("itemCheckTaskDao");
做法二:
HelloDao helloDao2 = (HelloDao) context.getBean("helloDaoImpl",HelloDaoImpl.class);
HelloDaoImpl helloDaoImpl2 = (HelloDaoImpl) context.getBean("helloDaoImpl");
做法三:
对于普通的wen工程使用以上二种做法测试没有问题,但是对于使用resin服务器的,并且数据源是在resin配置文件通过jndi配置的,这时候测试你就需要使用单元测试,并且使用spring注解的方法去加载配置文件,还需要在META-INF文件夹下配置一个app.properties文件存放数据源信息;具体做法如下:
测试类
packagecom.hudong.postmessage.dao.test;
importorg.junit.Test;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.test.context.ContextConfiguration;
importorg.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
importcom.hudong.postmessage.dao.WikiSentMsgDAO;
importcom.hudong.postmessage.dao.data.WikiSendSmsDO;
@ContextConfiguration(locations= { "classpath:/bean/test-postmsg-dao-env-bean.xml" })
publicclass TestPostsendSMs extends AbstractJUnit4SpringContextTests {
@Autowired
@Qualifier("wikiSendSmsDAO")
private WikiSentMsgDAO wikiSendSmsDAO;
public WikiSentMsgDAO getWikiSendSmsDAO() {
return wikiSendSmsDAO;
}
public voidsetWikiSendSmsDAO(WikiSentMsgDAO wikiSendSmsDAO) {
this.wikiSendSmsDAO = wikiSendSmsDAO;
}
@Test
public void test() {
try {
WikiSendSmsDO wikiSendSms = newWikiSendSmsDO();
wikiSendSms.setSmsMobile(Long.valueOf("12345678911"));
wikiSendSms.setSmsContent("互动yk");
wikiSendSms.setSmsKey("10");
wikiSendSms.setSoftwareSerialNo("100012");
wikiSendSms.setSmsState(0);
wikiSendSms.setSmsPriority(1);
wikiSendSmsDAO.saveSms(wikiSendSms);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Spring配置文件
mysqlAppsDataSource" />
classpath:/ibatis/postmsg-sqlmap.xml
app.properties文件
apps.mysql.jdbc.driverClassName=com.mysql.jdbc.Driver
apps.mysql.jdbc.url=jdbc:mysql://172.16.1.201:3306/apps useUnicode=true&characterEncoding=UTF-8
apps.mysql.jdbc.username=wiki_write
apps.mysql.jdbc.password=wiki_write
2. .错误:Caused by: org.xml.sax.SAXParseException:Attribute "resultMap" must be declared for element type"insert".
错误原因:ibatis的配置文件中相关的增删改查的地方多写一个resultMap属性;如下:
解决: 去掉resultMap"=“RM-SAVE”;
最后结果:
注:ibatis的一个sql配置文件中可以有多个resultMap属性,也就是说多个bean的结果集配置可以在同一个sql配置文件中,而且不需要特殊指明区别,只是在插入调用的时候指明使用的是哪个插入方法就行
3.测试出现同一个bean的两个对象不能指向同一个bean实体的错误
错误异常:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:No unique bean of type [com.hudong.postmessage.dao.WikiSentMsgDAO] is defined