springboot系列-集成MyBatis

【配置】

1 pom导入依赖

<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>

 2 application.yml 配置mybatis相关信息

mybatis:
  type-aliases-package: pers.sun.domain
  mapper-locations: classpath:mybatis/mapper/*.xml

【操作】

  ---- 采用映射文件编写sql语句

1 建立xxxMapper接口

需要的注解
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Mapper 
@Repository

 

2 建立mapper映射文件

<?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="org.mybatis.example.BlogMapper">

</mapper>

 

 

【总结】

配置方式

(1)mybatis配置类+mapper注解

(2)mybatis.xml配置文件+mapper.xml映射文件

细节如下:

mapper接口
  注解 @Mapper
sql映射方式
(1)注解
    在接口的方法上写注解
(2)mapper配置文件:xxx.xml
    同时在applciation.yml中配置 加载的mapper文件位置
mapper-locations: classpath:mybatis/mapper/*.xml
mybatis配置方式
(1)采用配置类
   configurationCustomizer类
(2)编写配置文件xxx.xml
   同时在applciation.yml中配置
   mybatis:
    type-aliases-package: pers.sun.domain
    mapper-locations: classpath:mybatis/mapper/*.xml
    config-location: classpath:mybatis/mybatis-config.xml


 

posted @ 2020-08-14 21:37  #Kouch  阅读(167)  评论(0)    收藏  举报