Springboot 中 mybatis 使用入门
概要
Springboot 中 mybatis 使用入门简介
正文
1、配置 application.yml
在 springboot 的配置文件中配置项目的 mybatis 信息。config-location 是 mybatis.xml 的位置,一般就位于 resources 目录下。mapper-location 表示 Mapper.xml 的位置,一般会放置于 resouces/mapper 目录下。
# Mybatis configurations
mybatis:
mapper-locations: classpath:mapper/**/*.xml
config-location: classpath:mybatis.xml
2、配置 mybatis.xml
此配置文件中配置 mybatis 本身相关的一些配置,此处只添加了一项配置就是将数据库字段的下划线转为驼峰格式。这个配置主要用于将数据库中的字段映射为 POJO 类中的字段(数据库中一般使用下划线分割单词,POJO 中一般使用驼峰分割单词)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
</configuration>
参考:
https://mybatis.org/mybatis-3/zh/configuration.html
3、定义 Mapper 的接口
AccountMapper.java
package com.jiangzl.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author jiangzhuolin
*/
@Mapper
@Component
public interface AccountMapper {
/**
* find account data
* @return List<String>
*/
List<String> findAll();
}
4、配置 Mapper.xml
配置具体的 SQL 的配置文件,与 Mapper 的 Java 接口一一对应
a. 首先是接口文件名与 Mapper.xml 文件名相同(AccountMapper.java 与 AccountMapper.xml)
b. 其次 namespace 要填写 Mapper 接口的全路径。
c. SQL 的 id 也要与 Mapper 接口中定义的方法名称要一模一样
AccountMapper.xml
<?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.jiangzl.mdm.mapper.AccountMapper">
<select id="findAll" resultType="java.lang.String">
select *
from dbo.C_BO_PRTY BP
left join dbo.C_BO_PRTY_RLE_ALT_ID RAI ON BP.ROWID_OBJECT = RAI.PRTY_FK
WHERE BP.ROWID_OBJECT IN ('2069448', '2069449', '206945', '2069450', '2069451')
FOR JSON AUTO, INCLUDE_NULL_VALUES
</select>
</mapper>
5、配置 pom.xml
在 pom.xml 中加入如下 resources 节点和内容,将 mybatis 相关的配置文件打包进项目里
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
注:如果不设置这个配置,在本地运行时你的 target 目录下没有 mapper 相关的 xml,或者打的包里没有相关的 xmp,导致运行失败,报找不到 xml 文件错误。
6、配置 MapperScan
在 springboot 的入口类上添加注解 @MapperScan,将扫描路径配置到后面,可以模糊匹配,如果你想项目启动性能更高啥的,可以配置的目录更精确一些
package com.jiangzl.application;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
* entrance of application
* @author jiangzl5
* @date 2020/05/28
*/
@SpringBootApplication
@MapperScan({"com.jiangzl.*"})
@ComponentScan(basePackages={"com.jiangzl.*"})
public class MdmCdcApplication {
public static void main(String[] args) {
SpringApplication.run(MdmCdcApplication.class, args);
}
}
结尾
就这几步,相对来说使用还是相当简单的。
附录
参考:
https://mybatis.org/mybatis-3/zh/index.html
近期评论