20200113 SpringBoot整合MyBatis
Posted 流星<。)#)))≦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了20200113 SpringBoot整合MyBatis相关的知识,希望对你有一定的参考价值。
环境信息
- SpringBoot 2.2.2.RELEASE
- Mybatis
- Druid
- mysql
- Lombok
实现步骤
- 引入Maven依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>study.hwj</groupId>
<artifactId>mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 编写Mapper
编写Mybatis的Mapper有多种形式,这里只选择其中一种
@Mapper
public interface UserMapper {
@Select("select * from user")
List<User> getAllUsers();
}
- 测试
@SpringBootApplication
public class MybatisApplication {
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(MybatisApplication.class, args);
UserMapper userMapper = ctx.getBean(UserMapper.class);
System.out.println(userMapper.getAllUsers());
}
}
以上是关于20200113 SpringBoot整合MyBatis的主要内容,如果未能解决你的问题,请参考以下文章
springboot整合mybatis使用xml实现sql语句的配置,在insert之后返回自增id