Spring Boot整合第三方框架

Stella981
• 阅读 710

1、整合jdbc

1.1 导入maven依赖

     <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
        
    <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>5.1.47</version>
    </dependency>

1.2 配置yml

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456

2、整合 druid +监控

2.1 导入maven依赖

 <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.21</version>
  </dependency>

2.2 druid 配置类

@ConfigurationProperties(prefix = "spring.datasource.druid")
@Data
public class DruidDataSourceProperties {

    private String username;

    private String password;

    private String jdbcUrl;

    private String driverClassName;

    private Integer initialSize;

    private Integer maxActive;

    private Integer minIdle;

    private long maxWait;

    private boolean poolPreparedStatements;

    public String filters;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getJdbcUrl() {
        return jdbcUrl;
    }

    public void setJdbcUrl(String jdbcUrl) {
        this.jdbcUrl = jdbcUrl;
    }

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    public Integer getInitialSize() {
        return initialSize;
    }

    public void setInitialSize(Integer initialSize) {
        this.initialSize = initialSize;
    }

    public Integer getMaxActive() {
        return maxActive;
    }

    public void setMaxActive(Integer maxActive) {
        this.maxActive = maxActive;
    }

    public Integer getMinIdle() {
        return minIdle;
    }

    public void setMinIdle(Integer minIdle) {
        this.minIdle = minIdle;
    }

    public long getMaxWait() {
        return maxWait;
    }

    public void setMaxWait(long maxWait) {
        this.maxWait = maxWait;
    }

    public boolean isPoolPreparedStatements() {
        return poolPreparedStatements;
    }

    public void setPoolPreparedStatements(boolean poolPreparedStatements) {
        this.poolPreparedStatements = poolPreparedStatements;
    }

    public String getFilters() {
        return filters;
    }

    public void setFilters(String filters) {
        this.filters = filters;
    }
}


@Configuration
@EnableConfigurationProperties(value = DruidDataSourceProperties.class)
public class DruidDataSourceConfig {

    @Autowired
    private DruidDataSourceProperties druidDataSourceProperties;

    @Bean
    public DataSource dataSource() throws SQLException {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUsername(druidDataSourceProperties.getUsername());
        druidDataSource.setPassword(druidDataSourceProperties.getPassword());
        druidDataSource.setUrl(druidDataSourceProperties.getJdbcUrl());
        druidDataSource.setDriverClassName(druidDataSourceProperties.getDriverClassName());
        druidDataSource.setInitialSize(druidDataSourceProperties.getInitialSize());
        druidDataSource.setMinIdle(druidDataSourceProperties.getMinIdle());
        druidDataSource.setMaxActive(druidDataSourceProperties.getMaxActive());
        druidDataSource.setMaxWait(druidDataSourceProperties.getMaxWait());
        druidDataSource.setFilters(druidDataSourceProperties.getFilters());
        druidDataSourceProperties.setPoolPreparedStatements(druidDataSourceProperties.isPoolPreparedStatements());
        return druidDataSource;
    }

    /**
     * 配置druid管理后台的servlet
     * @return
     */
    @Bean
    public ServletRegistrationBean statViewSerlvet() {
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        Map<String,Object> initParameters = new HashMap<>();
        initParameters.put("loginUsername","admin");
        initParameters.put("loginPassword","123456");
        bean.setInitParameters(initParameters);
        return bean;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));

        Map<String,Object> initParams = new HashMap<>();
        initParams.put("exclusions","*.js,*.css,/druid/*");
        filterRegistrationBean.setInitParameters(initParams);
        return  filterRegistrationBean;
    }
}

2.3 配置 yml

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      username: root
      password: 123456
      jdbcUrl: jdbc:mysql://10.0.0.147:3306/xxl_job
      driverClassName: com.mysql.jdbc.Driver
      initialSize: 5
      minIdle: 5
      maxActive: 20
      maxWait: 60000
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 300000
      validationQuery: SELECT 1 FROM DUAL
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      poolPreparedStatements: true
      filters: stat,wall
      maxPoolPreparedStatementPerConnectionSize: 20
      useGlobalDataSourceStat: true
      connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

druid监控访问路径:http://localhost:8080/druid/
Spring Boot整合第三方框架

3、整合 Mybatis

3.1 导入 maven依赖

 <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>5.1.45</version>
        </dependency>

        <!-- mybatis自动配置依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.4</version>
        </dependency>

Mybatis 自动配置了 sqlSessionFactory,sqlSessionTemplate

4、整合 Redis

SpringBoot2.x默认使用的是Lettuce。 关于jedis跟lettuce的区别: Lettuce 和 Jedis 的定位都是Redis的client,都可以直接连接redis server。 Jedis在实现上是直接连接的redis server,如果在多线程环境下是非线程安全的,这个时候只有使用连接池,为每个Jedis实例增加物理连接。 Lettuce的连接是基于Netty的,连接实例(StatefulRedisConnection)可以在多个线程间并发访问,因为StatefulRedisConnection是线程安全的,所以一个连接实例(StatefulRedisConnection)就可以满足多线程环境下的并发访问,当然这个也是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。

4.1 导入 maven依赖

          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.6.2</version>
        </dependency>

org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration 会自动配置redisTemplate和stringRedisTemplate

4.2 配置 yml

spring:
  redis:
    enabled: true # Redis开关/默认关闭
    database: 0
    password:    
    host: 10.0.0.147
    port: 6379
    lettuce:
      pool:
        max-active:  100 # 连接池最大连接数(使用负值表示没有限制) 
        max-idle: 100  # 连接池中的最大空闲连接 
        min-idle: 50 # 连接池中的最小空闲连接
        max-wait: 6000 # 连接池最大阻塞等待时间(使用负值表示没有限制)
    timeout: 1000

4.3 配置 RedisTemplate

使用redis 自动配置的默认的redisTemplate是使用jdk自带的序列化工具 JdkSerializationRedisSerializer。会出现乱码,需重新定义序列化方式。

@Configuration
public class RedisConfig {

    /**
     * 配置自定义redisTemplate
     * @return
     */
    @Bean
    RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {

        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);

         //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
        Jackson2JsonRedisSerializer serializer =
                new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        serializer.setObjectMapper(mapper);
        template.setValueSerializer(serializer);

        //使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(serializer);
        template.afterPropertiesSet();

        return template;
    }

}
点赞
收藏
评论区
推荐文章
kenx kenx
3年前
个人博客开发之blog-api项目整合MyBatisPlus代码自动生成CURD
整合MybatisPlus导入依赖xml<!mysql<dependency<groupIdmysql</groupId<artifactIdmysqlconnectorjava</artifactId<scoperuntime</scope
Wesley13 Wesley13
3年前
(六)SpringBoot与数据访问
1、JDBC<dependency<groupIdorg.springframework.boot</groupId<artifactIdspringbootstarterjdbc</artifactId</dependency<dependency
Stella981 Stella981
3年前
SpringBoot持久层技术
一、Springboot整合mybatismaven中添加对数据库与mybatis的依赖1<dependencies2<dependency3<groupIdorg.springframework.boot</groupId4<artifactId
Easter79 Easter79
3年前
SpringBoot打包成war包
1、添加依赖<dependency<groupIdorg.springframework.boot</groupId<artifactIdspringbootstartertomcat</artifactId<scopeprovided</scope</dependency2
Easter79 Easter79
3年前
SpringBoot持久层技术
一、Springboot整合mybatismaven中添加对数据库与mybatis的依赖1<dependencies2<dependency3<groupIdorg.springframework.boot</groupId4<artifactId
Stella981 Stella981
3年前
SpringBoot2.0应用(五):SpringBoot2.0整合MyBatis
如何整合MyBatis1、pom依赖<dependency<groupIdorg.mybatis.spring.boot</groupId<artifactIdmybatisspringbootstarte
Stella981 Stella981
3年前
Spring Boot整合redis
一、添加依赖<!SpringBoot整合redis的依赖<dependency<groupIdorg.springframework.boot</groupId<artifactIdspringbootstarter
Stella981 Stella981
3年前
Redis SpringBoot 整合
1.首先导入使用Maven导入jar包<dependency        <groupIdorg.springframework.boot</groupId        <artifactIdspringbootstarterdataredis</artifactId
Easter79 Easter79
3年前
SpringBoot2.0应用(五):SpringBoot2.0整合MyBatis
如何整合MyBatis1、pom依赖<dependency<groupIdorg.mybatis.spring.boot</groupId<artifactIdmybatisspringbootstarte
Stella981 Stella981
3年前
Spring Boot踩坑笔记一:Spring Boot整合mybatis和通用Mapper遇到的坑
一、整合步骤1、添加启动依赖<!mybatis<dependency<groupIdorg.mybatis.spring.boot</groupId<artifactIdmybatisspringbootstarter</artifa