1. 程式人生 > >超簡單的SpringBoot整合mybatis

超簡單的SpringBoot整合mybatis

source info app tor del mode artifact dao prop

  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"; }

超簡單的SpringBoot整合mybatis