依赖
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username: root
password: root
# 整合 mybatis
mybatis:
# 别名配置
type-aliases-package: org.example.pojo
# 映射文件绑定(classpath 代表 java 或 resources 目录)
mapper-locations: classpath:mybatis/mapper/*.xml
UserMapper.java
// 声明为 mybatis 的 mapper
@Mapper
// 或者在启动类上使用注解扫描 mapper 包
// @MapperScan("org.example.mapper")
public interface UserMapper {
List<User> selectUserList();
}
controller
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/userList")
public List<User> getUserList(){
return userMapper.selectUserList();
}
}