二、SpringBoot2核心功能--03数据访问--03springboot整合mybatis

1、引入mybatis场景启动器

https://github.com/mybatis

starter(场景启动器)
SpringBoot官方的Starter:spring-boot-starter-*
第三方的: *-spring-boot-starter

 <!--引入mybatis场景启动器-->
 <dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
     <version>2.1.4</version>
 </dependency>

在这里插入图片描述

2、配置模式

  • 全局配置文件
  • SqlSessionFactory: 自动配置好了
  • SqlSession:自动配置了 SqlSessionTemplate 组合了SqlSession
  • @Import(AutoConfiguredMapperScannerRegistrar.class);
  • Mapper: 只要我们写的操作MyBatis的接口标准了 @Mapper 就会被自动扫描进来
@EnableConfigurationProperties(MybatisProperties.class) : MyBatis配置项绑定类。
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class })
public class MybatisAutoConfiguration{}

@ConfigurationProperties(prefix = "mybatis")
public class MybatisProperties

2.1、导入mybatis官方starter

在这里插入图片描述

 <!--引入mybatis场景启动器-->
 <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
     <version>2.1.4</version>
 </dependency>

2.2、在配置文件中配置相关信息

注意:mybatis全局配置文件中的所有配置都可以在springboot配置文件中进行设置。当要在springboot配置文件中进行设置时需要关闭关于mybatis全局配置文件的位置指定,并且要注释掉在mybatis全局配置文件中进行的设置。
如下所示:想要开启mybatis对驼峰命名的支持有两种方式:
方式一:
在application.yaml中指定Mapper配置文件的位置,以及指定全局配置文件的信息

# 配置mybatis规则
# config-location:指定全局配置文件位置
# mapper-locations:指定mapper配置文件位置
mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

在这里插入图片描述在mybatis全局配置文件中配置相关信息:
在这里插入图片描述

方式二:
关闭mybatis全局配置文件中的配置
在这里插入图片描述在springboot配置文件application.yaml中配置关于驼峰命名的配置:
在这里插入图片描述

2.3、编写mapper接口。标准@Mapper注解

在这里插入图片描述

import com.study.admin.bean.Account;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface AccountMapper {

    public Account getAcct(long id);
}

2.4、编写sql映射文件并绑定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="com.study.admin.mapper.AccountMapper">

    <select id="getAcct" resultType="com.study.admin.bean.Account">
        select * from account_tb1 where id=#{id}
    </select>
</mapper>

2.5、编写service层、controller层代码

service层:
在这里插入图片描述
controller层:
在这里插入图片描述

2.6、测试

在这里插入图片描述

2.7、为什么可以在springboot配置文件中设置mybatis相关配置?相关源码如下

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3、注解模式

导入依赖:

<!--引入mybatis场景启动器-->
 <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
     <version>2.1.4</version>
 </dependency>

新建City实体类
在这里插入图片描述

@Data
public class City {
    private long id;
    private String name;
    private String state;
    private String country;
}

mapper接口
在这里插入图片描述

import com.study.admin.bean.City;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface CityMapper {

    @Select("select * from city where id=#{id}")
    public City getCity(long id);
}

service层
在这里插入图片描述

import com.study.admin.bean.City;
import com.study.admin.mapper.CityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CityService {

    @Autowired(required = false)
    CityMapper cityMapper;

    public City getCity(long id){
        return cityMapper.getCity(id);
    }
}

控制层:
在这里插入图片描述

import com.study.admin.bean.City;
import com.study.admin.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class CityController {

    @Autowired
    CityService cityService;

    @ResponseBody
    @GetMapping("/getCity")
    public City getCity(@RequestParam("id") long id){
        return cityService.getCity(id);
    }

}

测试:
在这里插入图片描述

4、注解和配置文件混合模式

引入依赖

<!--引入mybatis场景启动器-->
 <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
     <version>2.1.4</version>
 </dependency>

mapper文件
在这里插入图片描述
在这里插入图片描述
代替上述方式
在这里插入图片描述

5、最佳实战

  • 引入mybatis-starter
  • 配置application.yaml中,指定mapper-location位置即可
  • 编写Mapper接口并标注@Mapper注解
  • 简单方法直接注解方式
  • 复杂方法编写mapper.xml进行绑定映射
  • @MapperScan(“com.atguigu.admin.mapper”) 简化,其他的接口就可以不用标注@Mapper注解
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值