当前位置:   article > 正文

Mybatis-Spring1.0.0与Mybatis-Spring3.0.0版本在与spring整合时的区别_mybatis-spring版本

mybatis-spring版本

一、概述:

现在网上的大多数教程都是mybatis-spring1版本与spring整合,而现在spring已更新到6.0相应的mybatis-spring也更新到3.0这两个版本在配置spring配置文件上还是有差别的


二、环境:

·IDE2021

·jdk17

pom文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.bjpowernode</groupId>
  6. <artifactId>spring_sm</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <name>spring_sm</name>
  9. <!-- FIXME change it to the project's website -->
  10. <url>http://www.example.com</url>
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. <maven.compiler.source>1.7</maven.compiler.source>
  14. <maven.compiler.target>1.7</maven.compiler.target>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>junit</groupId>
  19. <artifactId>junit</artifactId>
  20. <version>4.12</version>
  21. <scope>test</scope>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework</groupId>
  25. <artifactId>spring-aspects</artifactId>
  26. <version>6.1.3</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework</groupId>
  30. <artifactId>spring-context</artifactId>
  31. <version>6.1.4</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework</groupId>
  35. <artifactId>spring-tx</artifactId>
  36. <version>6.1.3</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework</groupId>
  40. <artifactId>spring-jdbc</artifactId>
  41. <version>6.1.4</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.mybatis</groupId>
  45. <artifactId>mybatis-spring</artifactId>
  46. <version>3.0.3</version>
  47. </dependency>
  48. <dependency>
  49. <groupId>mysql</groupId>
  50. <artifactId>mysql-connector-java</artifactId>
  51. <version>8.0.22</version>
  52. </dependency>
  53. <dependency>
  54. <groupId>com.alibaba</groupId>
  55. <artifactId>druid</artifactId>
  56. <version>1.2.8</version>
  57. </dependency>
  58. <dependency>
  59. <groupId>org.mybatis</groupId>
  60. <artifactId>mybatis</artifactId>
  61. <version>3.5.15</version>
  62. </dependency>
  63. </dependencies>
  64. <build>
  65. <resources>
  66. <resource>
  67. <directory>src/main/java</directory>
  68. <includes>
  69. <include>**/*.properties</include>
  70. <include>**/*.xml</include>
  71. </includes>
  72. </resource>
  73. <resource>
  74. <directory>src/main/resources</directory>
  75. <includes>
  76. <include>**/*.properties</include>
  77. <include>**/*.xml</include>
  78. </includes>
  79. </resource>
  80. </resources>
  81. </build>
  82. </project>

三、目录结构和spring配置文件

applicationContext_mapper.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. 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">
  6. <!--扫描器,加载properties文件-->
  7. <context:property-placeholder location="jdbc.properties"></context:property-placeholder>
  8. <!--使用druid连接池,创建数据源-->
  9. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  10. <property name="driverClassName" value="${jdbc.driver}"></property>
  11. <property name="url" value="${jdbc.url}"></property>
  12. <property name="username" value="${jdbc.username}"></property>
  13. <property name="password" value="${jdbc.password}"></property>
  14. </bean>
  15. <!--配置sqlSessionFactoyBean类-->
  16. <bean class="org.mybatis.spring.SqlSessionFactoryBean">
  17. <!--配置数据源-->
  18. <property name="dataSource" ref="dataSource"></property>
  19. <!--配饰Mybatis核心配置文件-->
  20. <property name="configuration" value="SqlMapConfig.xml"></property>
  21. <!--设置实体类别名-->
  22. <property name="typeAliasesPackage" value="com.bjpowernode.pojo"></property>
  23. </bean>
  24. <!--注册mapper文件-->
  25. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  26. <!--使用basePackage是批量注册,注册一个包下的所有实体类-->
  27. <property name="basePackage" value="com.bjpowernode.mapper"></property>
  28. </bean>
  29. </beans>

applicationContext_service.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. 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">
  6. <!--倒入mapper的配置文件-->
  7. <import resource="applicationContext_mapper.xml"></import>
  8. <!--添加包扫描-->
  9. <context:component-scan base-package="com.bjpowernode.service.impl"></context:component-scan>
  10. <!--事务处理-->
  11. </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中属性名改下就可以了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/498250
推荐阅读
相关标签
  

闽ICP备14008679号