搬运一下 MyBatis-Spring-Boot-Starter 的官网介绍:
The MyBatis-Spring-Boot-Starter help you build quickly MyBatis applications on top of the Spring Boot.
By using this module you will achieve:
Build standalone applications
Reduce the boilerplate to almost zero
Less XML configuration
MyBatis-Spring-Boot-Starter可以做什么?:
- 会自动扫描带有@mapper注解的mappers,当然也可以用@mapperscan手动指定
我主要讲一下按照官方的指南如何做下去
1. 建工程、添加maven依赖
建工程的时候按照Intellij建工程的流程走就可以,记得把mybatis也加到依赖包里边。
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
2.mapper,entity文件
在包名如 com.example.test 下建立两个名为mapper、entity的文件夹
mapper/fundmapper.java
package com.example.testmybatis.mapper;
import com.example.testmybatis.entity.fund;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface fundmapper {
@Select("SELECT * FROM fundkey WHERE fundcode = #{fundcode}")
fund findByState(@Param("fundcode") String state);
}
entity/fund.java 这里我用了@Data注解,所以不要写getter、setter方法
package com.example.testmybatis.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("fundkey")
public class fund {
@TableId(type = IdType.AUTO)
private Integer id;
private String fundcode;
private String fundname;
private String fundtype;
}
3.数据库、yml文件准备
数据库我是这么建立的:
CREATE TABLE `fundkey` (
`id` int NOT NULL AUTO_INCREMENT,
`fundcode` varchar(25) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`fundname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`fundtype` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 139 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
yml文件里加上了数据库相关的配置:
server:
port: 8080
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3307/fund?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapper-locations: classpath:mapping/*Mapper.xml
type-aliases-package: com.example.testmybatis.entity
4.增加启动类
直接在com.example.testmybatis下增加启动类,基本就是复制官网的再改一改
package com.example.testmybatis;
import com.example.testmybatis.mapper.fundmapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SampleMybatisApplication implements CommandLineRunner {
@Autowired
private final fundmapper fundmapper;
public SampleMybatisApplication(fundmapper fundmapper) {
this.fundmapper = fundmapper;
}
public static void main(String[] args) {
SpringApplication.run(SampleMybatisApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println(this.fundmapper.findByState("004851"));
}
}
然后run一下这个启动类就可以看到结果了
官网地址:http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/index.html