1、springboot的配置檔案application.yml
[html] view plain copy
- #端口
- server:
- port: 8080
- #模闆頁面
- #注釋的部分是Thymeleaf預設的配置,如有其它需求可以自行更改
- spring:
- thymeleaf:
- cache: false
- prefix: classpath:/templates/
- suffix: .html
- mode: LEGACYHTML5
- # encoding: UTF-8
- # content-type: text/html
- ##資料源一
- #spring:
- # datasource:
- # driverClass: com.mysql.jdbc.Driver
- # url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8
- # username: xuan
- # password: 123456
- #資料源二
- spring:
- datasource:
- url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8
- username: xuan
- password: 123456
- # 使用druid資料源
- type: com.alibaba.druid.pool.DruidDataSource
- driver-class-name: com.mysql.cj.jdbc.Driver
- filters: stat
- maxActive: 20
- initialSize: 1
- maxWait: 60000
- minIdle: 1
- timeBetweenEvictionRunsMillis: 60000
- minEvictableIdleTimeMillis: 300000
- validationQuery: select 'x'
- testWhileIdle: true
- testOnBorrow: false
- testOnReturn: false
- poolPreparedStatements: true
- maxOpenPreparedStatements: 20
- #spring-boot整合mybatis
- mybatis:
- #config-location: classpath:/mapper/config/mybatisConfig.xml #可以注射掉,沒用到該配置檔案
- mapper-locations: classpath:/mapper
- public class User implements Serializable{
- private static final long serialVersionUID = 2120869894112984147L;
- private Integer id;
- private String name;
- private Integer age;
- private String address;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Integer getAge() {
- return age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- }
7、dao層是使用@Mapper注解接口代理,以前spring是使用自動掃描
[java] view plain copy
- package com.xuan.mapper;
- import com.xuan.entity.User;
- import org.apache.ibatis.annotations.Mapper;
- import java.util.List;
- @Mapper
- public interface UserMapper {
- public List<User> getAllUser();
- }
8、controller引用:
[java] view plain copy
- package com.xuan.controller;
- import com.xuan.entity.User;
- import com.xuan.mapper.UserMapper;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import java.util.*;
- @RestController
- @RequestMapping("/user")
- public class UserController {
- @Autowired
- private UserMapper userMapper;
- @GetMapping(value="/all")
- public List<User> getAllUser(){
- return userMapper.getAllUser();
- }
- }
9、效果圖:
10、目錄結構: