标签:
一:前沿
2015年新年上班的第二天,第一天就打了一天的酱油哦,只是下午开始搭建自己毕业设计的框架,搭建的是spring+spring mvc+MyBatis的框架。今天遇到了一个问题,结果弄了我一天时间,所以啊,路还是的一步步的走,弄过了才知道是怎么回事。现在记载下这个问题。
二:问题
在自己搭建的时候,我觉得自己一次都没有去搭建过,所以就想从最简单的一种开始,所以数据库连接用的就是org.springframework.jdbc.datasource.DriverManagerDataSource,说白了这种就是没有数据库连接池的作用的。
所以现在就说说配置中我自己遇到的问题了。
(1)在启动tomcat时候一直报DriverManagerDataSource里面的${driver}说加载不了,我就一直以为是由于数据库连接的问题,就换了使用阿里巴巴的数据库连接,但是换了之后还是这样,没办法我就想了我刚刚加了一个东西,就是MyBatis和spring结合的配置,配置如下
<!-- 最新的配置方式--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="com.wuhao.graduate.dao" p:sqlSessionFactoryBeanName="sqlSessionFactory"> </bean> <!-- 以前的配置方式--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.wuhao.graduate.dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean>
但是如果用最新的配置就是可以的,用最原始的配置就是不行,最后才知道原来是sqlSessionFactory这里的配置问题,我个人就觉得那里是要用ref的,但是实际上是用value的,所以一直报下面的错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘org.mybatis.spring.mapper.MapperScannerConfigurer#0‘ defined in class path resource [MyBatis-config.xml]: Cannot resolve reference to bean ‘sqlSessionFactory‘ while setting bean property ‘sqlSessionFactoryBeanName‘; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sqlSessionFactory‘ defined in class path resource [MyBatis-config.xml]: Cannot resolve reference to bean ‘dataSource‘ while setting bean property ‘dataSource‘; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘dataSource‘ defined in class path resource [MyBatis-config.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are: PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property ‘driverClassName‘ threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [${driver}] at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1471) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1216) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:116) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4994) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649) at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1247) at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1897) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) at java.util.concurrent.FutureTask.run(FutureTask.java:166) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:724) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sqlSessionFactory‘ defined in class path resource [MyBatis-config.xml]: Cannot resolve reference to bean ‘dataSource‘ while setting bean property ‘dataSource‘; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘dataSource‘ defined in class path resource [MyBatis-config.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are: PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property ‘driverClassName‘ threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [${driver}] at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1471) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1216) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) ... 29 more Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘dataSource‘ defined in class path resource [MyBatis-config.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are: PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property ‘driverClassName‘ threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [${driver}] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1508) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1216) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactor y.java:193) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) ... 39 more Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are: PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property ‘driverClassName‘ threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [${driver}] at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:121) at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1504) ... 47 more
这种错误看着就蛋疼,所以最后百度找了点资料才知道,原来MyBatis的官方文档上(http://mybatis.github.io/spring/zh/mappers.html#MapperScannerConfigurer)面都说了下面是截图
(2)第二个小问题就是我再这里指定了要扫描的配置文件
<!-- mybatis文件配置,扫描所有mapper文件 --> <!-- configLocation为mybatis属性 mapperLocations为所有mapper--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource" p:configLocation="classpath:Mapper-config.xml" p:mapperLocations="classpath:Mapper/*.xml"> </bean>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias type="com.duang.haha.vo.User" alias="user"/> </typeAliases> <mappers> <mapper resource="Mapper/User.xml"></mapper> </mappers> </configuration>
现在我来看这个代码就会觉得
<mappers><mapper resource="Mapper/User.xml"></mapper></mappers>和 p:mapperLocations="classpath:Mapper/*.xml"的配置重复了,但是这样的配置启动时会报错,这使得我很无语啊!
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sqlSessionFactory‘ defined in class path resource [MyBatis-config.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: ‘file [D:\wuhao\java\apache-tomcat-7.0.56\webapps\haha\WEB-INF\classes\Mapper\User.xml]‘; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.wh.haha.dao.UserDao.get_user
所以你需要把Mapper中配置的这个给注掉
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias type="com.wh.haha.vo.User" alias="user"/> </typeAliases> <!-- <mappers> <mapper resource="Mapper/User.xml"></mapper> </mappers> --> </configuration>
http://www.cnblogs.com/huanmieuroshui/archive/2012/12/18/2822754.html这篇博客里面说的比较详细了。
三:总结
自己的毕业设计的框架都搭建起来了,而且测试数据已经可以插入进去,以后每天都搞定一个小功能,一点点的往前走了。加油!毕业设计!终于迈出了第一步啊,这一步花了我两天时间啊,不过很值得的哦。这几天呆在公司也没什么事情,继续做吧!Go boy !
标签:
原文地址:http://www.cnblogs.com/wuhao1991/p/4301936.html