栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot学习[版本2.6.2]数据库配置,Druid,Mybatis整合day5-1

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot学习[版本2.6.2]数据库配置,Druid,Mybatis整合day5-1

数据访问

数据源自动配置

导入配置关于数据库驱动导入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


    mysql
    mysql-connector-java
    8.0.16

application.yaml配置mysql连接
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连接池

    com.alibaba
    druid-spring-boot-starter
    1.2.8


application.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

SpringBoot 整合Mybatis(starter) 文档

https://mybatis.net.cn/

mybatis配置文件可配置设置

https://mybatis.net.cn/configuration.html#settings

整合步骤 1.导入依赖

    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    2.2.1


2.编写配置全局配置文件位置application.yaml

#配置mybatis的config和mapper的地址
mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
3.编写全局配置文件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