spring和mybatis 加载多数据库的问题

sinat_31841789 2015-10-26 12:00:33
<import resource="ApplicationContext-service.xml"/>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="username" value="xxx"/>
<property name="password" value="xxx"/>
<property name="url" value="jdbc:mysql://xxx/x?useUnicode=true&characterEncoding=utf8"/>
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.flf.mapper" />
<property name="sqlSessionTemplateBeanName" value="centerSqlSession" />
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" name="cneterSqlSessionFactory">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath*:mybatis/*.xml"/>
<property name="configLocation" value="classpath:mybatis/config.xml"/>
</bean>

<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--center db end-->
<!--exdb-->

<bean id="dataSourceEx" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://xxx/x2?useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="xx"/>
<property name="password" value="xxxx"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.flf.mapper"/>
<property name="sqlSessionTemplateBeanName" value="exSqlSessionFactory"/>
</bean>
<bean id="sqlSessionFactoryEx" class="org.mybatis.spring.SqlSessionFactoryBean" name="exSqlSessionFactory">
<property name="dataSource" ref="dataSourceEx"></property>
<property name="mapperLocations" value="classpath*:mybatis/*.xml"/>
<property name="configLocation" value="classpath:mybatis/config.xml"/>
</bean>
<bean id="transactionManagerEx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceEx"/>
</bean>
配置两个数据库,按照搜索到的做法对配置文件进行了修改,但是在启动服务时候总是报错,请高手指教,也可以加我的qq:1723537119。

...全文
121 点赞 收藏 6
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
hsliwei 2015-10-27
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.flf.mapper" /> <property name="sqlSessionTemplateBeanName" value="centerSqlSession" /> //这里的value应该是下面bean的id属性才对吧。而且就算是name属性,也写的不对少个Factory </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" name="cneterSqlSessionFactory"> //不知道这里写个name属性撒意思 <property name="dataSource" ref="dataSource"></property> <property name="mapperLocations" value="classpath*:mybatis/*.xml"/> <property name="configLocation" value="classpath:mybatis/config.xml"/> </bean>
回复
行间 2015-10-26
<!-- 日志数据库 --> <bean id="LogSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="LogDataSource" p:configLocation="classpath:/config/mybatis.xml" p:mapperLocations="classpath:cn/com/companyl/log/mapper/*.xml" /> <!-- 日志数据库 --> <bean id="LogDataSource" class="org.apache.commons.dbcp.BasicDataSource" p:driverClassName="${log.driver}" p:url="${log.url}" p:username="${log.username}" p:password="${log.password}" p:maxIdle="50" p:maxActive="10" p:maxWait="3000" p:minIdle="3" p:removeAbandoned="true" p:removeAbandonedTimeout="36000" p:testOnBorrow="true" p:testOnReturn="true" p:testWhileIdle="true" p:validationQuery="select sysdate();"/>
回复
sinat_31841789 2015-10-26
嗯,是这个错误,那要怎么做呢,是不是还要写一下相关的方法,求具体操作
回复
行间 2015-10-26
错误提示很明显了 Bean property 'sqlSessionTemplateBeanName' is not writable or has an invalid setter method
回复
sinat_31841789 2015-10-26
具体错误是: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.mybatis.spring.mapper.MapperScannerConfigurer#0' defined in class path resource [spring/ApplicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'sqlSessionTemplateBeanName' of bean class [org.mybatis.spring.mapper.MapperScannerConfigurer]: Bean property 'sqlSessionTemplateBeanName' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1361) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1079) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:651) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:407) at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4765) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5260) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1362) at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1450) at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:295) at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119) at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90) at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1338) at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1496) at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1506) at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1485) at java.lang.Thread.run(Thread.java:619)
回复
bayitianshi 2015-10-26
你的dataSource的类导错包了
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-10-26 12:00
社区公告
暂无公告