首页 新闻 赞助 找找看

关于Spring加mybatis结合的问题

0
悬赏园豆:20 [已解决问题] 解决于 2016-08-05 17:39

这是我的applicationContext的配置<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-4.0.xsd
               http://www.springframework.org/schema/tx
               http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

<context:component-scan base-package="cn.xss"/>
<!-- 读取配置文件 -->
<bean id="propertiesConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean>

<!-- 基于spring-jdbc的DataSource -->
 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" ref="${jdbc.driverClass}"/>
<property name="url" ref="${jdbc.url}"/>
<property name="username" ref="${username}"/>
<property name="password" ref="${password}"/>
</bean> 

<!-- SqlSessionFactory -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="cn.xss.entity"/>
<property name="mapperLocations" value="classpath:cn/xss/dao/*Mapper.xml"/>
</bean>

<!-- dao -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.xss.dao"/>
</bean>

<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" autowire="byName"></bean>

<!-- 事务的通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 植入 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.xss.biz..*.*(..))"/>
</aop:config>
</beans>

这是我mybatis的配置<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="cn.xss"></context:component-scan>
<!-- 配置HandlerMapping -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> -->
<!-- 配置HandlerAdapter -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> -->

<!-- <bean id="/hello" class="cn.xss.controller.HelloController"></bean> -->

<!-- 会自动注册HandlerMapping和HandlerAdapter同时提供@valid的支持 -->
<mvc:annotation-driven/>
<!-- 配置视图解析器 -->
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

<!-- 配置一个文件处理器 必须加上id="multipartResolver"-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10240000"></property>
</bean>

<!-- 配置一个全局异常 -->
<!-- <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="exception/otherexception"></property>
<property name="exceptionMappings">
<props>
<prop key="java.lang.NullPointerException">/exception/null</prop>
<prop key="java.lang.ArithmeticException">/exception/Arithmetic</prop>
</props>
</property>
</bean> -->

<!-- 第一种:访问静态资源 mapping访问路径  location真实文件的路径-->
 <mvc:resources location="/css/" mapping="/css/**"/>
 <mvc:resources location="/images/" mapping="/images/**"/>
 <mvc:resources location="/script/" mapping="/js/**"/>
 
 <!-- 第二种方式 -->
 <!-- <mvc:default-servlet-handler/> -->
</beans>

这是报错信息
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'com.mysql.jdbc.Driver' while setting bean property 'driverClassName'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'com.mysql.jdbc.Driver' is defined

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'com.mysql.jdbc.Driver' is defined

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'com.mysql.jdbc.Driver' while setting bean property 'driverClassName'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'com.mysql.jdbc.Driver' is defined

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'com.mysql.jdbc.Driver' is defined
jar包都导了,难道是mysqljar包版本不对么?

坐墙等红杏的主页 坐墙等红杏 | 初学一级 | 园豆:136
提问于:2016-08-05 15:16
< >
分享
最佳答案
1

Cannot resolve reference to bean 'com.mysql.jdbc.Driver' while setting bean property 'driverClassName';

   No bean named 'com.mysql.jdbc.Driver' is defined

 

这点你再检查一下看看是不是写错了,或者少写了什么

 

收获园豆:20
代码飞了 | 菜鸟二级 |园豆:470 | 2016-08-05 15:42

这是mysql配置文件

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/crm?characterEncoding=UTF8
username=root
password=yangjinwei

这是数据源

<!-- 基于spring-jdbc的DataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" ref="${jdbc.driverClass}"/>
<property name="url" ref="${jdbc.url}"/>
<property name="username" ref="${username}"/>
<property name="password" ref="${password}"/>
</bean>

引用配置文件

坐墙等红杏 | 园豆:136 (初学一级) | 2016-08-05 15:46

@杨亦风: 那你的MySQL配置文件配置到applicationContext.xml中了吗,我咋没看到你配置这个

代码飞了 | 园豆:470 (菜鸟二级) | 2016-08-05 15:58

@杨亦风: 如果你要在applicationContext.xml中直接引用MySQL的配置,需要在application.xml中配置一下

代码飞了 | 园豆:470 (菜鸟二级) | 2016-08-05 16:01

@代码飞了: 我配置了呀,这就是的

<!-- 读取配置文件 -->
<bean id="propertiesConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean>

坐墙等红杏 | 园豆:136 (初学一级) | 2016-08-05 16:02

@杨亦风: 把ref改成value试试

代码飞了 | 园豆:470 (菜鸟二级) | 2016-08-05 16:04

@杨亦风: 可以了没

代码飞了 | 园豆:470 (菜鸟二级) | 2016-08-05 16:07

@杨亦风: 

<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="location" value="classpath:jdbc.properties"/>
</bean>
<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="false" />
    <property name="properties" ref="configProperties" />
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClass}"/>
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${username}" />
    <property name="password" value="${password}" />
</bean>

如果不行,你把刚才的改成这样子,试试吧

 

代码飞了 | 园豆:470 (菜鸟二级) | 2016-08-05 16:16

@代码飞了: 恩,谢谢,我试试

坐墙等红杏 | 园豆:136 (初学一级) | 2016-08-05 16:28

@代码飞了: 已经好了,谢谢,怎么把园豆给你

坐墙等红杏 | 园豆:136 (初学一级) | 2016-08-05 16:40

不会给,那就算了吧,木有事^-^

代码飞了 | 园豆:470 (菜鸟二级) | 2016-08-05 16:51

@代码飞了: 你现在在上海上班么,可以加个好友么,我也准备到上海去工作

坐墙等红杏 | 园豆:136 (初学一级) | 2016-08-05 17:06

@杨亦风: 嗯呢,好

代码飞了 | 园豆:470 (菜鸟二级) | 2016-08-05 17:07
其他回答(1)
0

的确是没有找到MYSQL的JAR包。你再看看吧

之奇一昂 | 园豆:1421 (小虾三级) | 2016-08-05 15:22

但是我真的加了jar包啊

支持(0) 反对(0) 坐墙等红杏 | 园豆:136 (初学一级) | 2016-08-05 15:26

发不了图片,第一次提问还不知道

支持(0) 反对(0) 坐墙等红杏 | 园豆:136 (初学一级) | 2016-08-05 15:28

/CRM/WebRoot/WEB-INF/lib/mysql-connector-java-5.1.0-bin.jar

这是我mysqljar包版本

支持(0) 反对(0) 坐墙等红杏 | 园豆:136 (初学一级) | 2016-08-05 15:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册