SpringBoot整合MyBatis


首先需要搭建一个SpringBoot项目,然后整合Mybatis。

一、引入依赖

在SpringBoot项目的pom.xml文件中引入MyBatis依赖。(2.1.2版本)
引入MySQL的依赖。(5.1.47版本)

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.2</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>

二、创建mybatis-config.xml配置文件(非必须)

MyBatis的配置文件,在SpringBoot中我们采用配置类的形式进行配置,因此可以不创建mybatis-config.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!--mybatis核心配置文件的约束-->
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--在此设置mybatis的配置-->
    <settings>
    	<!--开启驼峰命名自动映射,从数据库字段列明(A_COLUMN)映射到Java属性名(aColumn)-->
    	<!--注意,在该配置文件中配置了驼峰命名自动映射,则无需在application.yml配置文件中配置,会报错-->
    	<setting name = "mapUnderscoreToCamelCase" value = "true"/>
    </settings>
</configuration>

三、在application.yml中设置mapper.xml的位置

如果使用mybatis-config.xml形式设置mybatis的配置,则还需要指定mybatis-config.xml配置文件的位置;使用配置类的形式,则可以不用指定。

mybatis:
  #指定mybatis的配置文件(将mybatis配置文件复到resources目录下根据自己的需求在mybatis配置文件中添加自己的配置)
  config-location: classpath:mybatis-config.xml
  #注意:一定要对应mapper映射xml文件的所在路径
  mapper-locations: classpath:mapper/*.xml

四、创建MyBatisMapperConfig配置类

使用配置类的形式设置mybatis的配置,可以不用编写mybatis-config.xml配置文件。使用@Configuration注解标识该类为配置类。配置类与普通类加载顺序不同。
使用MapperScan(“com.hcq.mybatis.dao”)指定dao接口的包路径。(该注解还可以放到启动类上)

@Configuration
@MapperScan("com.hcq.mybatis.dao")
public class MyBatisMpperConfig {
}

五、在application.yml中配置数据源

在application.yml配置文件中指定数据源。当开发环境和测试环境数据源不同是,可以使用springboot的多环境配置,将不同的数据源写到不同的启动配置文件中。我们还应当指定JDBC的连接池,在此处暂不配置。

spring:
  datasource:
    #设置驱动类
    driver-class-name: com.mysql.jdbc.Driver
    #指定数据库的IP地址,端口号,数据库名称和编码格式
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
    #数据库用户名称
    username: root
    #数据库用户密码
    password: password

六、编写dao层的接口

dao层的接口与mapper文件映射之后,可以在service层调用dao接口中的方法,dao层接口的方法名称与mapper文件中标签的id相对应。

public interface TestMyBatisDAO {
    String selectUser();
}

七、编写与dao接口对应的mapper.xml是文件

注意:mapper文件中的namespace必须使用全限定符与dao层的接口绑定
标签中的id必须与dao接口中的方法名相一致。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--设置命名,只想对应的接口的全限定符-->
<mapper namespace="com.hcq.mybatis.dao.TestMyBatisDAO">
    <select id="selectUser" resultType="string">
        select user_name from user where id = 2
    </select>
</mapper>

八、可能碰到的问题

java.lang.IllegalArgumentException: At least one base package must be specified

原因:@MapperScan注解不指定扫描的包无法启动,指定错误也无法启动。
@MapperScan该注解会扫描指定的包及其子包下的dao接口。
解决:注意将@MapperScan指定扫描的包路径写对

java.lang.IllegalStateException: Failed to introspect Class [org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
Caused by: java.lang.NoClassDefFoundError: org/springframework/transaction/ReactiveTransactionManager
Caused by: java.lang.ClassNotFoundException: org.springframework.transaction.ReactiveTransactionManager

原因:引入的依赖有冲突,SpringBoot版本为2.2.5.RELEASE,MyBatis版本为2.1.1发生了冲突。
解决一:将MyBatis的版本换成2.1.2。
解决二:引入spring-boot-start-jdbc依赖,版本为2.2.1.RELEASE。

java.lang.IllegalStateException: Failed to load property source from location 'classpath:/application.yml'
Caused by: org.yaml.snakeyaml.scanner.ScannerException: mapping values are not allowed here
 in 'reader', line 6, column 29:
      config-location: classpath: mybatis-config.xml

原因:application.yml配置文件中设置mapper-locations: classpath:mapper/*.xml。两个冒号后都添加空格或者都不添加空格无法启动
解决:只能在其中一个冒号后添加空格。

Field myBatisDAO in com.hcq.mybatis.service.TestMyBatisService required a bean of type 'com.hcq.mybatis.dao.TestMyBatisDAO' that could not be found.

原因:service层中注入了dao接口,但是spring并没有拿到dao的bean,MyBatis的配置类没有生效。
解决:在MyBatis配置类上添加@Configuration。

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.hcq.mybatis.dao.TestMyBatisDAO.selectUser

原因:dao接口无法与对应的mapper文件映射。
原因一:mapper-locations指定mapper.xml文件路径不对
解决:mapper-locations: classpath:mapper/*.xml
原因二:mapper文件中指定namespace时指定错误
解决:写dao接口的全限定名称

org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statement 'com.hcq.mybatis.dao.TestMyBatisDAO.selectUser'.  It's likely that neither a Result Type nor a Result Map was specified.

原因:在mapper文件中没有指定返回类型resultType或者resultMap。
解困:在mapper文件中指定返回类型。resultType或者resultMap。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值