数据源自动配置
导入配置关于数据库驱动导入mysql的驱动(看你自己的版本)application.yaml配置mysql连接Druid连接池application.yaml SpringBoot 整合Mybatis(starter)
文档mybatis配置文件可配置设置整合步骤
1.导入依赖2.编写配置全局配置文件位置application.yaml3.编写全局配置文件mybatis-config.xml(可以不写)
不写全局配置文件就直接在application.yaml里进行配置 4.编写实体类5.编写Mapper接口
注意@Mapper注解 6.编写Service7.编写Controller类8.编写sql映射文件stu4Mapper.xml
文件头部固定写法 访问http://localhost:8080/stu?id=1 (*)Spring Boot整合Mybatis(纯注解配置式)
创建项目数据库准备stu3表yaml中的配置同上面编写Stu3实体类编写Stu3Mapper编写Stu3Service编写Stu3Controller访问结果 推荐使用注解和配置文件混合的方式,注解若过于繁琐就放置在配置文件中,这里指的式Mapper.xml这个配置文件不是全局配置文件
数据源自动配置 导入配置关于数据库驱动org.springframework.boot spring-boot-starter-jdbc
由于官方不知道我们使用的是什么数据库,什么版本的数据库所以不帮我们自动导入
导入mysql的驱动(看你自己的版本)Spring Boot中MySQL的版本仲裁是8.0.22
application.yaml配置mysql连接mysql mysql-connector-java 8.0.16
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&allowPublicKeyRetrieval=true
username: root
password: 123456
Druid连接池
application.yamlcom.alibaba druid-spring-boot-starter 1.2.8
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&allowPublicKeyRetrieval=true
username: root
password: 123546
druid:
stat-view-servlet:
enabled: true
login-username: admin
login-password: 123456
reset-enable: false
web-stat-filter: #监控we'b
url-pattern: /*
exclusions: '*.png,/druid/*'
enabled: true
filters: stat,wall,slf4j #底层开启功能,stat:sql监控,wall:防火墙
filter:
stat:
slow-sql-millis: 1000
log-slow-sql: true
enabled: true
wall:
enabled: true
config:
drop-table-allow: false
aop-patterns: com.example.day3.*
jdbc:
template:
query-timeout: 3
SpringBoot 整合Mybatis(starter)
文档
https://mybatis.net.cn/
mybatis配置文件可配置设置https://mybatis.net.cn/configuration.html#settings
整合步骤 1.导入依赖2.编写配置全局配置文件位置application.yamlorg.mybatis.spring.boot mybatis-spring-boot-starter 2.2.1
#配置mybatis的config和mapper的地址 mybatis: config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mybatis/mapper/*.xml3.编写全局配置文件mybatis-config.xml(可以不写)
不写全局配置文件就直接在application.yaml里进行配置
mybatis:
configuration:
call-setters-on-nulls: true
就等同于
4.编写实体类
package com.example.day3.bean;
import lombok.Data;
@Data
public class Stu4 {
private int stuId;
private String stuName;
private int money;
}
5.编写Mapper接口
package com.example.day3.mapper;
import com.example.day3.bean.Stu4;
@Mapper
public interface Stu4Mappper {
public Stu4 getStu4(int stuId);
}
注意@Mapper注解
6.编写Service
package com.example.day3.service;
import com.example.day3.bean.Stu4;
import com.example.day3.mapper.Stu4Mappper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class Stu4Service {
@Autowired
Stu4Mappper stu4Mappper;
public Stu4 getStu4ById(int id){
return stu4Mappper.getStu4(id);
}
}
7.编写Controller类
package com.example.day3.controller;
import com.example.day3.bean.Stu4;
import com.example.day3.bean.User;
import com.example.day3.service.Stu4Service;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.servlet.HandlerInterceptor;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpSession;
@Controller
public class IndexController {
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
Stu4Service stu4Service;
@GetMapping("/stu")
public Stu4 getById(@RequestParam("id") int id) {
return stu4Service.getStu4ById(id);
}
}
8.编写sql映射文件stu4Mapper.xml
文件头部固定写法
访问http://localhost:8080/stu?id=1
这里的id取决于你数据库里的id
(*)Spring Boot整合Mybatis(纯注解配置式) 创建项目 数据库准备stu3表 yaml中的配置同上面spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&allowPublicKeyRetrieval=true
username: root
password: 123546
druid:
stat-view-servlet:
enabled: true
login-username: admin
login-password: 123456
reset-enable: false
web-stat-filter: #监控we'b
url-pattern: /*
exclusions: '*.png,/druid/*'
enabled: true
filters: stat,wall,slf4j #底层开启功能,stat:sql监控,wall:防火墙
filter:
stat:
slow-sql-millis: 1000
log-slow-sql: true
enabled: true
wall:
enabled: true
config:
drop-table-allow: false
aop-patterns: com.example.day3.*
jdbc:
template:
query-timeout: 3
编写Stu3实体类
package com.example.day5.bean;
import lombok.Data;
@Data
public class Stu3 {
private int id;
private String name;
private int age;
private String sex;
private int math;
}
编写Stu3Mapper
package com.example.day5.mapper;
import com.example.day5.bean.Stu3;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface Stu3Mapper {
@Select("select * from stu3 where id = #{id}")
public Stu3 getById(int id);
}
编写Stu3Service
package com.example.day5.service;
import com.example.day5.bean.Stu3;
import com.example.day5.mapper.Stu3Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class Stu3Service {
@Autowired
Stu3Mapper stu3Mapper;
public Stu3 getById(int id){
return stu3Mapper.getById(id);
}
}
编写Stu3Controller
package com.example.day5.controller;
import com.example.day5.bean.Stu3;
import com.example.day5.service.Stu3Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class Stu3Controller {
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
Stu3Service stu3Service;
@ResponseBody
@GetMapping("/stu")
public Stu3 getById(@RequestParam("id") int id) {
return stu3Service.getById(id);
}
}
访问结果
推荐使用注解和配置文件混合的方式,注解若过于繁琐就放置在配置文件中,这里指的式Mapper.xml这个配置文件不是全局配置文件

![springboot学习[版本2.6.2]数据库配置,Druid,Mybatis整合day5-1 springboot学习[版本2.6.2]数据库配置,Druid,Mybatis整合day5-1](http://www.mshxw.com/aiimages/31/704373.png)
