欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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í)現(xiàn)遍歷文件目錄并去除中文文件名

    基于Java實(shí)現(xiàn)遍歷文件目錄并去除中文文件名

    這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)遍歷文件目錄并去除中文文件名,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下
    2024-03-03
  • ES模糊查詢失效的坑以及解決方案

    ES模糊查詢失效的坑以及解決方案

    ES的查詢?cè)硎前捶衷~建立索引,根據(jù)要保存的內(nèi)容先分詞,然后按照分詞的結(jié)果建立索引,這篇文章主要給大家介紹了關(guān)于ES模糊查詢失效的坑及解決方案的相關(guān)資料,需要的朋友可以參考下
    2023-09-09
  • java單鏈表實(shí)現(xiàn)書籍管理系統(tǒng)

    java單鏈表實(shí)現(xiàn)書籍管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java單鏈表實(shí)現(xiàn)書籍管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Java實(shí)現(xiàn)TFIDF算法代碼分享

    Java實(shí)現(xiàn)TFIDF算法代碼分享

    這篇文章主要介紹了Java實(shí)現(xiàn)TFIDF算法代碼分享,對(duì)算法進(jìn)行了簡單介紹,概念,原理,以及實(shí)現(xiàn)代碼的分享,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • Java實(shí)現(xiàn)圖片驗(yàn)證碼具體代碼

    Java實(shí)現(xiàn)圖片驗(yàn)證碼具體代碼

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)圖片驗(yàn)證碼具體代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • 關(guān)于JDK8升級(jí)17及springboot?2.x升級(jí)3.x詳細(xì)指南

    關(guān)于JDK8升級(jí)17及springboot?2.x升級(jí)3.x詳細(xì)指南

    這篇文章主要介紹了關(guān)于JDK8升級(jí)17及springboot?2.x升級(jí)3.x的相關(guān)資料,還討論了JPA包路徑從javax改為jakarta,以及Spring?Boot版本升級(jí)和Redis配置調(diào)整等,需要的朋友可以參考下
    2025-01-01
  • 基于Spring BeanUtils的copyProperties方法使用及注意事項(xiàng)

    基于Spring BeanUtils的copyProperties方法使用及注意事項(xiàng)

    這篇文章主要介紹了基于Spring BeanUtils的copyProperties方法使用及注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java中類的定義和初始化示例詳解

    Java中類的定義和初始化示例詳解

    這篇文章主要給大家介紹了關(guān)于Java中類的定義和初始化的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Java使用StampedLock實(shí)現(xiàn)高效讀寫功能

    Java使用StampedLock實(shí)現(xiàn)高效讀寫功能

    StampedLock 是 Java 8 引入的高性能鎖,提供了三種鎖模式:寫鎖、悲觀讀鎖和樂觀讀鎖,與傳統(tǒng)的 ReentrantReadWriteLock 相比,StampedLock 更注重性能,特別適合讀多寫少的場景,所以本文給大家介紹了Java使用StampedLock實(shí)現(xiàn)高效讀寫功能,需要的朋友可以參考下
    2025-01-01
  • IDEA神器一鍵查看Java字節(jié)碼及其他類信息插件

    IDEA神器一鍵查看Java字節(jié)碼及其他類信息插件

    這篇文章主要為大家介紹了一款I(lǐng)DEA神器,可以一鍵查看Java字節(jié)碼及其他類信息,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2022-01-01

最新評(píng)論