天天看點

SpringBoot整合Mybatis 以及 mybatis自動生成代碼配置

1、springboot的配置檔案application.yml

[html]  view plain  copy

  1. #端口  
  2. server:  
  3.   port: 8080  
  4.   #模闆頁面  
  5.   #注釋的部分是Thymeleaf預設的配置,如有其它需求可以自行更改  
  6.   spring:  
  7.     thymeleaf:  
  8.       cache: false  
  9.       prefix: classpath:/templates/  
  10.       suffix: .html  
  11.       mode: LEGACYHTML5  
  12. #      encoding: UTF-8  
  13. #      content-type: text/html  
  14. ##資料源一  
  15. #spring:  
  16. #      datasource:  
  17. #          driverClass: com.mysql.jdbc.Driver  
  18. #          url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8  
  19. #          username: xuan  
  20. #          password: 123456  
  21. #資料源二  
  22. spring:  
  23.       datasource:  
  24.           url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8  
  25.           username: xuan  
  26.           password: 123456  
  27.           # 使用druid資料源  
  28.           type: com.alibaba.druid.pool.DruidDataSource  
  29.           driver-class-name: com.mysql.cj.jdbc.Driver  
  30.           filters: stat  
  31.           maxActive: 20  
  32.           initialSize: 1  
  33.           maxWait: 60000  
  34.           minIdle: 1  
  35.           timeBetweenEvictionRunsMillis: 60000  
  36.           minEvictableIdleTimeMillis: 300000  
  37.           validationQuery: select 'x'  
  38.           testWhileIdle: true  
  39.           testOnBorrow: false  
  40.           testOnReturn: false  
  41.           poolPreparedStatements: true  
  42.           maxOpenPreparedStatements: 20  
  43. #spring-boot整合mybatis  
  44. mybatis:  
  45.   #config-location: classpath:/mapper/config/mybatisConfig.xml #可以注射掉,沒用到該配置檔案  
  46.   mapper-locations: classpath:/mapper  
  47. public class User implements Serializable{  
  48.     private static final long serialVersionUID = 2120869894112984147L;  
  49.     private Integer id;  
  50.     private String  name;  
  51.     private  Integer age;  
  52.     private String address;  
  53.     public Integer getId() {  
  54.         return id;  
  55.     }  
  56.     public void setId(Integer id) {  
  57.         this.id = id;  
  58.     }  
  59.     public String getName() {  
  60.         return name;  
  61.     }  
  62.     public void setName(String name) {  
  63.         this.name = name;  
  64.     }  
  65.     public Integer getAge() {  
  66.         return age;  
  67.     }  
  68.     public void setAge(Integer age) {  
  69.         this.age = age;  
  70.     }  
  71.     public String getAddress() {  
  72.         return address;  
  73.     }  
  74.     public void setAddress(String address) {  
  75.         this.address = address;  
  76.     }  
  77. }  

7、dao層是使用@Mapper注解接口代理,以前spring是使用自動掃描

[java]  view plain  copy

  1. package com.xuan.mapper;  
  2. import com.xuan.entity.User;  
  3. import org.apache.ibatis.annotations.Mapper;  
  4. import java.util.List;  
  5. @Mapper  
  6. public interface UserMapper {  
  7.     public List<User> getAllUser();  
  8. }  

8、controller引用:

[java]  view plain  copy

  1. package com.xuan.controller;  
  2. import com.xuan.entity.User;  
  3. import com.xuan.mapper.UserMapper;  
  4. import org.springframework.beans.factory.annotation.Autowired;  
  5. import org.springframework.web.bind.annotation.GetMapping;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RestController;  
  8. import java.util.*;  
  9. @RestController  
  10. @RequestMapping("/user")  
  11. public class UserController {  
  12.     @Autowired  
  13.     private UserMapper userMapper;  
  14.     @GetMapping(value="/all")  
  15.     public List<User> getAllUser(){  
  16.         return userMapper.getAllUser();  
  17.     }  
  18. }  

9、效果圖:

SpringBoot整合Mybatis 以及 mybatis自動生成代碼配置

10、目錄結構:

SpringBoot整合Mybatis 以及 mybatis自動生成代碼配置

繼續閱讀