1.导入依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
2.编写com.atguigu.admin.mapper.AccountMapper
接口
package com.atguigu.admin.mapper;
import com.atguigu.admin.bean.Account;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface AccountMapper {
public Account getAccount(Long id);
}
3.注册mapper对应的serivce:com.atguigu.admin.service.AccountService
package com.atguigu.admin.service;
import com.atguigu.admin.bean.Account;
import com.atguigu.admin.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AccountService {
@Autowired
AccountMapper accountMapper;
public Account getAccount(Long id) {
return accountMapper.getAccount(id);
}
}
4.编写sql文件mybatis/mapper/AccountMapper.xml
绑定接口
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.admin.mapper.AccountMapper">
<!--public Account getAccount(Long id);-->
<select id="getAccount" resultType="com.atguigu.admin.bean.Account">
select * from account_tbl where id = #{id}
</select>
</mapper>
5.在全局配置文件application.yml
中,指定mapper配置文件的位置,以及全局配置文件信息
mybatis:
# 要么就在config-location中写明位置,用mybatis配置文件的形式来配置,要么就用springboot全局配置,二者不能同时存在
# config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
configuration:
map-underscore-to-camel-case: true