赞
踩
现在网上的大多数教程都是mybatis-spring1版本与spring整合,而现在spring已更新到6.0相应的mybatis-spring也更新到3.0这两个版本在配置spring配置文件上还是有差别的
·IDE2021
pom文件:
- <?xml version="1.0" encoding="UTF-8"?>
-
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>com.bjpowernode</groupId>
- <artifactId>spring_sm</artifactId>
- <version>1.0-SNAPSHOT</version>
-
- <name>spring_sm</name>
- <!-- FIXME change it to the project's website -->
- <url>http://www.example.com</url>
-
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <maven.compiler.source>1.7</maven.compiler.source>
- <maven.compiler.target>1.7</maven.compiler.target>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aspects</artifactId>
- <version>6.1.3</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>6.1.4</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>6.1.3</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>6.1.4</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- <version>3.0.3</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.22</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.2.8</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>3.5.15</version>
- </dependency>
- </dependencies>
-
- <build>
- <resources>
- <resource>
- <directory>src/main/java</directory>
- <includes>
- <include>**/*.properties</include>
- <include>**/*.xml</include>
- </includes>
- </resource>
- <resource>
- <directory>src/main/resources</directory>
- <includes>
- <include>**/*.properties</include>
- <include>**/*.xml</include>
- </includes>
- </resource>
- </resources>
- </build>
- </project>
applicationContext_mapper.xml
- <?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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
-
- <!--扫描器,加载properties文件-->
- <context:property-placeholder location="jdbc.properties"></context:property-placeholder>
- <!--使用druid连接池,创建数据源-->
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
- <property name="driverClassName" value="${jdbc.driver}"></property>
- <property name="url" value="${jdbc.url}"></property>
- <property name="username" value="${jdbc.username}"></property>
- <property name="password" value="${jdbc.password}"></property>
- </bean>
- <!--配置sqlSessionFactoyBean类-->
- <bean class="org.mybatis.spring.SqlSessionFactoryBean">
- <!--配置数据源-->
- <property name="dataSource" ref="dataSource"></property>
- <!--配饰Mybatis核心配置文件-->
- <property name="configuration" value="SqlMapConfig.xml"></property>
- <!--设置实体类别名-->
- <property name="typeAliasesPackage" value="com.bjpowernode.pojo"></property>
- </bean>
-
- <!--注册mapper文件-->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <!--使用basePackage是批量注册,注册一个包下的所有实体类-->
- <property name="basePackage" value="com.bjpowernode.mapper"></property>
- </bean>
- </beans>
applicationContext_service.xml
- <?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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
- <!--倒入mapper的配置文件-->
- <import resource="applicationContext_mapper.xml"></import>
- <!--添加包扫描-->
- <context:component-scan base-package="com.bjpowernode.service.impl"></context:component-scan>
- <!--事务处理-->
- </beans>
主要信息:
Failed to convert property value of type 'java.lang.String' to required type 'org.apache.ibatis.session.Configuration' for property 'configuration'; Cannot convert value of type 'java.lang.String' to required type 'org.apache.ibatis.session.Configuration' for property 'configuration': no matching editors or conversion strategy found
意思是org.apache.ibatis.session.Configuration类中有一个属性configuration的类型不匹配,也就是说我们给configuration传了一个不符合他类型的值
原因:
旧版mybatis中配置SqlSessionFactoryBean时有一个配置叫configuration,在新版的mybatis中他变为了configLocation!,把applicationContext_service.xml文件里SqlSessionFactoryBean中属性名改下就可以了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。