SpringBoot篇—整合Mybatis以及使用druid数据源

SpringBoot篇—整合Mybatis以及使用druid数据源,第1张

SpringBoot篇—整合Mybatis以及使用druid数据源

整合步骤

    导入mybatis官方starter编写mapper接口(标注@Mapper注解)编写sql映射文件并且绑定mapper接口在application.yml文件中指定Mapper配置文件的位置,以及指定全局配置文件的信息(建议:将要配置在该文件中的内容写在application.yml中的mybatis.configuration项下)

1.pom文件 



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.5
         
    
    com.example
    Springboot06_mybatis
    0.0.1-SNAPSHOT
    Springboot06_mybatis
    Demo project for Spring Boot

    
        8
    

    
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.1
        
        
        
            mysql
            mysql-connector-java
            runtime
        
        
        
            org.projectlombok
            lombok
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


在我们引入mybatis-spring-boot-starter后,为我们引入的相关依赖如下,因此不需要再引入spring-boot-starter-jdbc

2.mapper接口

User实体类

package com.IT.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
// get和set方法、无参和有参构造
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private Integer id;
    private String name;
    private String email;
    private String password;
}

UserMapper接口

package com.IT.mapper;

import com.IT.pojo.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;


@Mapper
public interface UserMapper {
    List queryUserList();
    User queryUserById(int id);
}

当有很多个实体类Mapper接口时,在每个接口上都要标注@Mapper注解就显得比较麻烦,这时,我们可以将所有的@Mapper注解去掉,直接在主启动类上加上扫描路径@MapperScan("com.IT.mapper"),如下:

package com.IT;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.IT.mapper")
@SpringBootApplication
public class Springboot06MybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot06MybatisApplication.class, args);
    }

}

3.UserMapper.xml文件(sql映射文件) 




    
       select * from t_user
    
    

4.application.yml文件

spring:
  datasource:
    username: root
    password: 526260
    url: jdbc:mysql://localhost:3306/bjpowernode
    driver-class-name: com.mysql.cj.jdbc.Driver 
            
# 整合mybatis
# 配置mybatis规则
mybatis:
  # 1.全局配置文件所在的位置
  config-location: classpath:mybatis/mybatis-config.xml
  # 2.sql映射文件所在的位置
  mapper-locations: classpath:mybatis/mapper/*.xml

如果不写全局配置文件mybatis-config.xml(将其删除),我们可以将要配置在该文件中的内容写在application.yml中的mybatis.configuration项下,那么配置文件将变为:

spring:
  datasource:
    username: root
    password: 526260
    url: jdbc:mysql://localhost:3306/bjpowernode
    driver-class-name: com.mysql.cj.jdbc.Driver 

# 配置mybatis规则
mybatis:
  # sql映射文件所在的位置
  mapper-locations: classpath:mybatis/mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true
引入druid数据源的第一种方式

1.导入依赖

        
            com.alibaba
            druid
            1.1.16
        

2.编写config配置类

package com.kuang.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;

import javax.sql.DataSource;

// druid数据源配置类
@Controller
public class MyDataSourceConfig {
    // @ConfigurationProperties注解的作用是将下面这个组件所需要的属性与配置文件进行绑定
    @ConfigurationProperties("spring.datasource")
    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource=new DruidDataSource();
        return new DruidDataSource();
    }
}
引入druid数据源的第二种方式 

1.直接导入启动器依赖(不需要再编写配置类)

        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.20
        

2.配置文件application.yml

spring:
  datasource:
    druid:
      aop-patterns: com.IT.*
      filters: stat,wall   # 底层开启功能,stat(sql监控),wall(防火墙)

      stat-view-servlet:   # 配置druid的监控页功能
        enabled: true      # 是否开启
        login-username: admin
        login-password: admin
        reset-enable: false

      web-stat-filter:     # 配置web监控
        enabled: true
        url-pattern: /*
        exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'

      filter:              # 对上面filters里面的stat功能进行配置
        stat:
          slow-sql-millis: 1000
          log-slow-sql: true
          enabled: true
        wall:              # 对上面filters里面的wall功能进行配置
          enabled: true
          config:
            drop-table-allow: false

监控页面访问地址 

Druid DataSourceStathttp://localhost:8080/druid/datasource.html

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5716191.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-17
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存