SpringBoot-- integration Mybatis + druid

  Divided into two parts, first of all replace the default data source for Ali druids and add monitoring, followed by the use Mybatis under SpringBoot

Replacement data source Druid

  Basic information of first configure the database connection in the configuration file, such as username password url, etc., it is important to replace the default type Druid. Doing data source has been replaced druid, but if you want to complete the customized settings, such as setting druid initialSize, only the configuration is not in yml Lane, need to implement a configuration class and implement the background monitoring in this configuration class Configuration.

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 mark indicates that this is a comment, use @Bean added to the container Bean, Bean is of type DataSource
  • Add @ConfigurationProperties (prefix = "spring.dataSource"), so yml in configuration to take effect on druid
  • Filter Servlet and to add to the vessel in the form of a druid add background monitoring, and add the appropriate configuration
@Configuration
public class DruidConfig {

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

    // configuration druid monitoring 
    @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.setınitparameters (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.setınitparameters (map);
        bean.setUrlPatterns(Arrays.asList("/*"));

        return bean;
    }

}

Integration Mybatis

  Write interface and the interface implementation class xml files and general use Mybatis no difference, the key is location location allows SpringBoot interface implementation class, and Mybatis profile. You need to tell SpringBoot yml in a specific location, where config-location corresponding to the location profile, mapper-locations corresponding to the position of the interface class. In order to make the project tidy, I have two profiles placed under resources / mybatis.

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

 

Guess you like

Origin www.cnblogs.com/AshOfTime/p/10926569.html