MyBatis-Spring-Boot-Starter
一、简介
使用MyBatis-Spring-Boot-Starter
可以在Spring Boot之上快速构建MyBatis应用程序。
二、安装
1、必需条件
-
MyBatis-Spring-Boot-Starter 2.1
-
MyBatis-Spring 2.0
-
Spring Boot 2.1及以上
-
Java8及以上
2、安装
- Jar
要使用MyBatis-Spring-Boot-Starter
模块,只需要将mybatis-spring-boot-autoconfigure.jar
及它的依赖项(mybatis.jar
、mybatis-spring.jar
等)添加到classpath中即可。
- Maven
如果使用Maven,直接添加以下依赖配置即可:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
三、使用
1、简介
要在Spring上使用MyBatis至少需要一个SqlSessionFactory
和一个mapper接口,MyBatis-Spring-Boot-Starter将:
-
自动检测已存在
DataSource
-
创建并注册一个
SqlSessionFactory
实例,并将该数据源DataSource
作为输入传递给SqlSessionFactoryBean
-
创建并注册从
SqlSessionFactory
中获取的SqlSessionTemplate
实例 -
自动扫描mappers,将它们连接到
SqlSessionTemplate
并注册到Spring context,以便可以把它们注入到bean中
2、样例
- pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
- 数据库表
CREATE TABLE city (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(100) DEFAULT NULL,
country varchar(30) DEFAULT NULL,
PRIMARY KEY (id)
)
id | name | country |
---|---|---|
1 | BeiJing | China |
2 | ShangHai | China |
3 | Washington | US |
- City.java
package com.example;
public class City {
private int id;
private String name;
private String country;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return String.format("ID: %s, Name: %s, Country: %s", id, name, country);
}
}
- CityMapper.java
@Mapper
public interface CityMapper {
@Insert("insert into city(name,country) values(#{name},#{country})")
//with id
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
void insertCity(City city);
@Update("update city set country = #{country} where name = #{name}")
void updateCity(@Param("name") String name, @Param("country") String country);
@Select("SELECT * FROM CITY WHERE id = #{id}")
City findCityById(@Param("id") int id);
@Delete("delete from city where id = #{id}")
void deleteCityById(@Param("id") int id);
}
- SampleMybatisApplication.java
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.example.mapper.CityMapper;
@SpringBootApplication
public class SampleMybatisApplication implements CommandLineRunner {
private final CityMapper cityMapper;
public SampleMybatisApplication(CityMapper cityMapper) {
this.cityMapper = cityMapper;
}
public static void main(String[] args) {
SpringApplication.run(SampleMybatisApplication.class, args);
}
public void run(String... args) throws Exception {
String cityName = "London";
//insert
City city = new City();
city.setName(cityName);
cityMapper.insertCity(city);
//find
int id = city.getId();
city = cityMapper.findCityById(id);
System.out.println(city);
//update
cityMapper.updateCity(cityName, "UK");
//find
city = cityMapper.findCityById(id);
System.out.println(city);
//delete
cityMapper.deleteCityById(id);
//find
city = cityMapper.findCityById(id);
System.out.println(city);
}
}
运行结果:
ID: 4, Name: London, Country: null
ID: 4, Name: London, Country: UK
null