SpringBoot-整合MyBatis

发布时间:2023年12月28日

依赖

<!--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();
    }
}
文章来源:https://blog.csdn.net/h_e_l_l_o_______/article/details/135255388
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。