最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot整合mybatis使用Druid做連接池的方式

 更新時(shí)間:2023年08月25日 09:13:36   作者:hzau_itdog  
這篇文章主要介紹了SpringBoot整合mybatis使用Druid做連接池的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

項(xiàng)目構(gòu)建

使用的spring官方的eclipse版本為sts-4.0.1.RELEASE

下一步

下一步

finish

pom文件修改

增加配置

<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.0</version>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.6</version>
		</dependency>

完整pom如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.waterelephant</groupId>
	<artifactId>mybatis-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>mybatis-demo</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.0.0</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.0</version>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.6</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

修改配置文件 

修改application.yml 

application.yml配置如下

server:
  port: 8089
#默認(rèn)使用配置
spring:
  profiles:
    active: dev
#公共配置與profiles選擇無關(guān) mapperLocations指的路徑是src/main/resources
#公共配置與profiles選擇無關(guān)
mybatis:
#配置別名掃描包
  typeAliasesPackage: com.example.demo.model
#配置mapper.xml文件的位置
  mapperLocations: classpath:mapper/*.xml
---
#開發(fā)配置
spring:
  profiles: dev
  datasource:
    url: jdbc:mysql://ip:3306/beadwalletloan?autoReconnect=true&useUnicode=true&zeroDateTimeBehavior=convertToNull&useServerPrepStmts=false&rewriteBatchedStatements=true&serverTimezone=GMT%2B8
    username: username
    password: password
    driverClassName: com.mysql.jdbc.Driver
    # 使用druid數(shù)據(jù)源
    type: com.alibaba.druid.pool.DruidDataSource
    # 配置獲取連接等待超時(shí)的時(shí)間
    # 下面為連接池的補(bǔ)充設(shè)置,應(yīng)用到上面所有數(shù)據(jù)源中
    # 初始化大小,最小,最大
    initialSize: 1
    minIdle: 3
    maxActive: 20
    # 配置獲取連接等待超時(shí)的時(shí)間
    maxWait: 60000
    # 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒
    timeBetweenEvictionRunsMillis: 60000
    # 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒
    minEvictableIdleTimeMillis: 30000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    # 打開PSCache,并且指定每個(gè)連接上PSCache的大小
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    # 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計(jì),'wall'用于防火墻
    filters: stat,wall,slf4j
    # 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    # 合并多個(gè)DruidDataSource的監(jiān)控?cái)?shù)據(jù)
    useGlobalDataSourceStat: true
 

增加Druid的配置

package com.example.demo.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
import java.sql.SQLException;
/**
 * @ClassName DatasourceConfig
 * @Description TODO
 * @Author yueyiming
 * @Date 2019/4/3 15:20
 */
@Log4j2
@Configuration
public class DatasourceConfig {
    @Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String password;
    @Value("${spring.datasource.driverClassName}")
    private String driverClassName;
    @Value("${spring.datasource.initialSize}")
    private int initialSize;
    @Value("${spring.datasource.minIdle}")
    private int minIdle;
    @Value("${spring.datasource.maxActive}")
    private int maxActive;
    @Value("${spring.datasource.maxWait}")
    private int maxWait;
    @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
    private int timeBetweenEvictionRunsMillis;
    @Value("${spring.datasource.minEvictableIdleTimeMillis}")
    private int minEvictableIdleTimeMillis;
    @Value("${spring.datasource.validationQuery}")
    private String validationQuery;
    @Value("${spring.datasource.testWhileIdle}")
    private boolean testWhileIdle;
    @Value("${spring.datasource.testOnBorrow}")
    private boolean testOnBorrow;
    @Value("${spring.datasource.testOnReturn}")
    private boolean testOnReturn;
    @Value("${spring.datasource.poolPreparedStatements}")
    private boolean poolPreparedStatements;
    @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
    private int maxPoolPreparedStatementPerConnectionSize;
    @Value("${spring.datasource.filters}")
    private String filters;
    @Value("${spring.datasource.connectionProperties}")
    private String connectionProperties;
    @Value("${spring.datasource.useGlobalDataSourceStat}")
    private boolean useGlobalDataSourceStat;
    @Bean
    @Primary
    public DataSource dataSource() {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(url);
        datasource.setUsername(username);
        datasource.setPassword(password);   //這里可以做加密處理
        datasource.setDriverClassName(driverClassName);
        //configuration
        datasource.setInitialSize(initialSize);
        datasource.setMinIdle(minIdle);
        datasource.setMaxActive(maxActive);
        datasource.setMaxWait(maxWait);
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        datasource.setValidationQuery(validationQuery);
        datasource.setTestWhileIdle(testWhileIdle);
        datasource.setTestOnBorrow(testOnBorrow);
        datasource.setTestOnReturn(testOnReturn);
        datasource.setPoolPreparedStatements(poolPreparedStatements);
        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        datasource.setUseGlobalDataSourceStat(useGlobalDataSourceStat);
        try {
            datasource.setFilters(filters);
        } catch (SQLException e) {
        }
        datasource.setConnectionProperties(connectionProperties);
        return datasource;
    }
    @Bean
    public ServletRegistrationBean druidServlet() {
        log.info("init Druid Servlet Configuration ");
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        // IP白名單
       // servletRegistrationBean.addInitParameter("allow", "192.168.2.25,127.0.0.1");
        // IP黑名單(共同存在時(shí),deny優(yōu)先于allow)
        //servletRegistrationBean.addInitParameter("deny", "192.168.1.100");
        //控制臺(tái)管理用戶
        servletRegistrationBean.addInitParameter("loginUsername", "admin");
        servletRegistrationBean.addInitParameter("loginPassword", "9527");
        //是否能夠重置數(shù)據(jù) 禁用HTML頁面上的“Reset All”功能
        servletRegistrationBean.addInitParameter("resetEnable", "false");
        return servletRegistrationBean;
    }
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}

配置mapperScan掃描

在啟動(dòng)類上加上

@MapperScan(basePackages = {"com.example.*.mapper" }

至此配置完成

測(cè)試

mapper.xml 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.OperateBasicMapper">
    <select id="getById" resultType="map">
        select *
          from
            bw_operate_basic
            where
              id=#{id}
    </select>
</mapper>

mapper接口

package com.example.demo.mapper;
import org.apache.ibatis.annotations.Param;
import java.util.Map;
/**
 * @ClassName OperateBasicMapper
 * @Description TODO
 * @Author yueyiming
 * @Date 2019/4/3 15:52
 */
public interface OperateBasicMapper {
     Map<String,Object> getById(@Param(value = "id") Long id);
}

service

package com.example.demo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.mapper.OperateBasicMapper;
import java.util.Map;
/**
 * @ClassName OperateBasicService
 * @Description TODO
 * @Author yueyiming
 * @Date 2019/4/3 16:02
 */
@Service
public class OperateBasicService {
    @Autowired
    private OperateBasicMapper operateBasicMapper;
    public Map<String,Object> getById(Long id){
        return operateBasicMapper.getById(id);
    }
}

controller

package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.service.OperateBasicService;
/**
 * @ClassName TestController
 * @Description TODO
 * @Author yueyiming
 * @Date 2019/4/3 15:49
 */
@RestController
public class TestController {
    @Autowired
    private OperateBasicService operateBasicService;
    @RequestMapping(value="get/{id}")
    public Object test(@PathVariable Long id) {
        return operateBasicService.getById(id);
    }
}

訪問

  • http://127.0.0.1:8089/get/1

訪問

  • http://127.0.0.1:8089/druid

登陸后

測(cè)試成功!

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 全面剖析java 數(shù)據(jù)類型與運(yùn)算符

    全面剖析java 數(shù)據(jù)類型與運(yùn)算符

    這篇文章主要介紹了Java基本數(shù)據(jù)類型和運(yùn)算符,結(jié)合實(shí)例形式詳細(xì)分析了java基本數(shù)據(jù)類型、數(shù)據(jù)類型轉(zhuǎn)換、算術(shù)運(yùn)算符、邏輯運(yùn)算符等相關(guān)原理與操作技巧,需要的朋友可以參考下
    2021-09-09
  • java虛擬機(jī)是做什么用的

    java虛擬機(jī)是做什么用的

    在本篇文章里小編給大家整理的是一篇關(guān)于java虛擬機(jī)作用等相關(guān)內(nèi)容,對(duì)此有興趣的朋友們可以學(xué)習(xí)參考下。
    2021-01-01
  • 詳談Java8新特性泛型的類型推導(dǎo)

    詳談Java8新特性泛型的類型推導(dǎo)

    這篇文章我們來看一篇關(guān)于Java8新特性之泛型的類型推導(dǎo),希望這篇文章能夠讓各位深入到了解到關(guān)于Java8新特性之泛型的類型用法,有需要的朋友們下面來一起看看吧。
    2016-09-09
  • 在JDK和Eclipse下如何編寫和運(yùn)行Java Applet

    在JDK和Eclipse下如何編寫和運(yùn)行Java Applet

    本文主要介紹了在JDK和Eclipse的環(huán)境下如何編寫和運(yùn)行Java Applet,圖文方式,適合初學(xué)者學(xué)習(xí)。
    2015-09-09
  • MyBatis之關(guān)于動(dòng)態(tài)SQL解讀

    MyBatis之關(guān)于動(dòng)態(tài)SQL解讀

    這篇文章主要介紹了MyBatis之關(guān)于動(dòng)態(tài)SQL解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 一篇看懂Java中的Unsafe類

    一篇看懂Java中的Unsafe類

    在閱讀AtomicInteger的源碼時(shí),看到了這個(gè)類:sum.msic.Unsafe,之前從沒見過。所以花了點(diǎn)時(shí)間研究了下,下面這篇文章主要給大家介紹了關(guān)于Java中Unsafe類的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • java將圖片轉(zhuǎn)為base64返回給前端

    java將圖片轉(zhuǎn)為base64返回給前端

    這篇文章主要為大家詳細(xì)介紹了java將圖片轉(zhuǎn)為base64返回給前端,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • Java多線程編程詳細(xì)解釋

    Java多線程編程詳細(xì)解釋

    這篇文章主要介紹了java多線程編程實(shí)例,分享了幾則多線程的實(shí)例代碼,具有一定參考價(jià)值,加深多線程編程的理解還是很有幫助的,需要的朋友可以參考下。
    2021-11-11
  • mybatis?mapper.xml?注釋帶參數(shù)的坑及解決

    mybatis?mapper.xml?注釋帶參數(shù)的坑及解決

    這篇文章主要介紹了mybatis?mapper.xml?注釋帶參數(shù)的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Spring?cloud網(wǎng)關(guān)gateway進(jìn)行websocket路由轉(zhuǎn)發(fā)規(guī)則配置過程

    Spring?cloud網(wǎng)關(guān)gateway進(jìn)行websocket路由轉(zhuǎn)發(fā)規(guī)則配置過程

    這篇文章主要介紹了Spring?cloud網(wǎng)關(guān)gateway進(jìn)行websocket路由轉(zhuǎn)發(fā)規(guī)則配置過程,文中還通過實(shí)例代碼介紹了Spring?Cloud?Gateway--配置路由的方法,需要的朋友可以參考下
    2023-04-04

最新評(píng)論

衢州市| 普安县| 牙克石市| 乐东| 广河县| 万全县| 新建县| 安塞县| 京山县| 鲜城| 康保县| 南京市| 鸡泽县| 惠水县| 庆云县| 于田县| 永善县| 江山市| 黔西县| 钦州市| 桃源县| 娄烦县| 广汉市| 滨州市| 南澳县| 沁源县| 东港市| 五河县| 新郑市| 垦利县| 静乐县| 开原市| 连江县| 浏阳市| 正镶白旗| 浦江县| 日照市| 安徽省| 历史| 萨迦县| 彝良县|