springboot--整合mybatis+druid(代码片段)

ashoftime ashoftime     2022-12-13     227

关键词:

  分为两部分,首先替换默认数据源为阿里德鲁伊并添加监控,其次是SpringBoot下使用Mybatis

替换数据源为德鲁伊

  首先在配置文件里配置好数据库连接的基本信息,如username password url等,重要的是把默认的type换成Druid。这样做数据源已经换成druid了,但是如果想完成对druid的定制化设置如设置initialSize,仅仅在yml里配置是不可以的,需要再实现一个配置类,并在这个配置类里实现后台监控的配置。

spring:
  datasource:
    username: root
    password: password
    url: jdbc:mysql://localhost:3306/javaweb
    driver-class-name: com.mysql.jdbc.Driver
   # initialization-mode: always
    type: com.alibaba.druid.pool.DruidDataSource
    initialSize: 5
  • 标注@Configuration表示这是一个注解,使用@Bean向容器里添加Bean,Bean的类型是DataSource
  • 添加@ConfigurationProperties(prefix="spring.dataSource"),这样在yml里关于druid的配置才能生效
  • 以向容器中添加Servlet和Filter的形式为druid添加后台监控,并添加相应的配置
@Configuration
public class DruidConfig 

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid()
        return new DruidDataSource();
    

    //配置druid监控
    @Bean
    public ServletRegistrationBean statViewServlet()
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        HashMap<String, String> map = new HashMap<>();
        map.put("loginUsername","admin");
        map.put("loginPassword","123456");
        bean.setInitParameters(map);

        return bean;
    

    @Bean
    public FilterRegistrationBean webStatFilter()
        FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new WebStatFilter());
        HashMap<String, String> map = new HashMap<>();
        map.put("exclusions","*.js");
        bean.setInitParameters(map);
        bean.setUrlPatterns(Arrays.asList("/*"));

        return bean;
    

整合Mybatis

  编写接口和接口实现类xml文件和普通使用Mybatis没有区别,关键在于让SpringBoot接口的实现类的位置,和Mybatis配置文件的位置。需要在yml里告诉SpringBoot具体的位置,其中config-location对应配置文件的位置,mapper-locations对应接口实现类的位置。为了使项目整洁,两个配置文件我都放在了resources/mybatis下。

  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

 

springboot学习笔记-整合mybatis

Springboot学习笔记(二)- 整合MyBatis SpringBoot中整合MyBatis,并通过注解方式实现映射。整合MyBatis以Springboot学习笔记(一)-Helloworld 为基础项目,在pom.xml中添加如下依赖 <dependency>  <groupId>org.mybatis.spring.b... 查看详情

springboot整合mybatis

1、springboot配置数据库连接池druid2、springboot整合mybatis3、springboot整合pagehelper  springboot配置数据库连接池druid 新建springboot项目   相关pom依赖druid所需pom依赖 <dependency><groupId> 查看详情

springboot.04.springboot整合mybatis

SpringBoot.04.SpringBoot整合MyBatis准备工作1.mapper模板2.t_user整合MyBatis1.新建Module2.pom.xml3.application.yml4.Springboot04MybatisApplication.java5.User.java6.UserMapper.java7.UserMapper.xml8.UserService9.User 查看详情

springboot整合mybatis

SpringBoot整合MyBatis创建一个SpringBoot项目需要的pom依赖列表如下图右侧:准备数据createdatabasenyf;USEnyf;CREATETABLEcategory_(idint(11)NOTNULLAUTO_INCREMENT,namevarchar(32)DEFAULTNULL,PRIMARYKEY(id))ENGINE=MyISAMAUTO_INCREMEN 查看详情

springboot整合mybatis方式2:使用注解方式整合mybatis(代码片段)

SpringBoot整合Mybatis简介SpringBoot整合Mybatis方式2:使用注解方式整合Mybatis1.先用idea创建一个添加mybatis需要的相关依赖的工程。2.准备数据库和表3.创建表映射类4.创建mapper代理接口5.创建Service层和Service的实现层6.创建控制层࿰... 查看详情

基于springboot的完成mybatis整合(代码片段)

SpringBoot🍌掌握基于SpringBoot框架的程序开发步骤🍌使用SpringBoot配置信息修改服务器配置🍌SpringBoot完成SSM整合之SpringBoot整合junit今日目标:基于SpringBoot的完成mybatis整合一、SpringBoot整合mybatis1.1回顾Spring整合MybatisS... 查看详情

springboot+mybatis整合

    LZ今天自己搭建了下Springboot+Mybatis,比原来的Spring+SpringMVC+Mybatis简单好多。其实只用Springboot也可以开发,但是对于多表多条件分页查询,Springboot就有点力不从心了,所以LZ把Mybatis整合进去,不得不说,现在的框架... 查看详情

springboot与mybatis整合(*)

在pom.xml文件中加入数据库、spring-mybatis整合<!--springboot整合mybatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId> 查看详情

springboot+mybatis整合

  LZ今天自己搭建了下Springboot+Mybatis,比原来的Spring+SpringMVC+Mybatis简单好多。其实只用Springboot也可以开发,但是对于多表多条件分页查询,Springboot就有点力不从心了,所以LZ把Mybatis整合进去,不得不说,现在的框架搭建真的... 查看详情

springboot.04.springboot整合mybatis(代码片段)

SpringBoot.04.SpringBoot整合MyBatis准备工作1.mapper模板2.t_user整合MyBatis1.新建Module2.pom.xml3.application.yml4.Springboot04MybatisApplication.java5.User.java6.UserMapper.java7.UserMapper.xml8.UserService9.User 查看详情

springboot.04.springboot整合mybatis(代码片段)

SpringBoot.04.SpringBoot整合MyBatis准备工作1.mapper模板2.t_user整合MyBatis1.新建Module2.pom.xml3.application.yml4.Springboot04MybatisApplication.java5.User.java6.UserMapper.java7.UserMapper.xml8.UserService9.User 查看详情

springboot+mybatis整合入门

SpringBoot+Mybatis四步整合第一步添加依赖springBoot+Mybatis相关依赖<!--springBoot相关--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifa 查看详情

springboot09:整合mybatis

官方文档:http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/Maven仓库地址:https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter/2.1.2整合Mybatis新建一个模块导入依赖& 查看详情

springboot整合mybatis

...中有说过,将spring与mybatis整个后开发会更爽,基于现在springboot已经成为整个业界开发主流框架的情况下,今天在这里就直接将mybatis整合springboot了。  先简单地提一下Springboot。在Mybatis还没火起来之前,大家用的是SSH(Struts2+S... 查看详情

springboot整合mybatis之annotation

...需要下载前面一篇文章的代码,在前一章代码上进行修改.SpringBoot整合Mybatis(注解方式)复制前一个项目,修改配置文件,mybatis的相关配置为:mybatis:type-aliases-package:con.mybatis.springboot_mybatis.modelconfiguration:map-underscore-to-camel-case:truelog-im 查看详情

springboot整合mybatis以及mybatisplus

这篇文档介绍整合的过程 查看详情

springboot整合mybatis以及mybatisplus

这篇文档介绍整合的过程 查看详情

springboot之整合mybatis

  今天了解一下SpringBoot如何与我们最常用的ORM框架Mybatis整合。一、  需要在pom.xml文件里加入mybatis的依赖<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifac 查看详情