1. 创建项目结构
2. 编写application.yml/application.properties配置文件
3. 启动类开启映射包扫描
4. 接口测试
创建项目结构
导入依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
编写application.yml/application.properties配置文件
启动类开启映射包扫描
@SpringBootApplication @MapperScan({"com.cn.mybatis.zy.zymybatis.dao"}) public class ZyMybatisApplication { public static void main(String[] args) { SpringApplication.run(ZyMybatisApplication.class, args); } }
接口测试
@Resource private UserService userService; @GetMapping("/info") public String info(Model model){ List<User> allUsers = userService.getAllUsers(); model.addAttribute("user",allUsers); allUsers.stream().forEach(a -> { System.out.println(a); }); return "index"; }