SpringBoot-整合MyBatis

依赖



	org.mybatis.spring.boot
	mybatis-spring-boot-starter
	2.1.1



	org.springframework.boot
	spring-boot-starter-jdbc


	org.springframework.boot
	spring-boot-starter-web



	mysql
	mysql-connector-java
	runtime

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 selectUserList();
}

controller

@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @GetMapping("/userList")
    public List getUserList(){
        return userMapper.selectUserList();
    }
}

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/dcd8f51c30.html