欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot整合

程序员文章站 2022-07-12 22:39:27
...

1、springboot配置数据库连接池druid

druid所需pom依赖

<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid-spring-boot-starter</artifactId>
     <version>1.1.10</version>
  </dependency>

application.yml配置druid

server:
  port: 80
  servlet:
    context-path: /


spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
    username: root
    password: 123
    druid:
      initial-size: 5
      min-idle: 5
      max-active: 20
      max-wait: 60000
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 30000
      validation-query: SELECT 1 FROM DUAL
      test-while-idle: true
      test-on-borrow: true
      test-on-return: false
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      filter:
        stat:
          merge-sql: true
          slow-sql-millis: 5000
      web-stat-filter:
        enabled: true
        url-pattern: /*
        exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
        session-stat-enable: true
        session-stat-max-count: 100
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        reset-enable: true
        login-username: admin
        login-password: admin
        allow: 127.0.0.1

#pagehelper分页插件配置
pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql

#显示日志
logging:
  level:
    com.javaxl.springboot02.mapper: debug

测试代码

package com.zyc.springboot02.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/demo")
public class HelloController {

    @RequestMapping("/say1")
    public String say1(){
        return "说活1";
    }

    @RequestMapping("/say2")
    public String say2(){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "说活2";
    }

    @RequestMapping("/say3")
    public String say3(){
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "说活3";
    }

}

进入druid,用户名和密码在yml文件中有
springboot整合

2、springboot整合mybatis

整合mybatis只需要在springboot启动类中加上以**解就可以了
@MapperScan()

package com.zyc.springboot02;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.transaction.annotation.EnableTransactionManagement;
//整合mybatis
@MapperScan("com.zyc.springboot02.mapper")
//开启动态代理
@EnableTransactionManagement
//启动切面注解
@EnableAspectJAutoProxy
@SpringBootApplication
public class Springboot02Application {

    public static void main(String[] args) {
        SpringApplication.run(Springboot02Application.class, args);
    }

}

测试代码

package com.zyc.springboot02.service;


import com.zyc.springboot02.model.Book;
import com.zyc.springboot02.util.PageBean;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

/**
 * @author 简单定义存在
 * @site https://i.csdn.net/#/uc/profile
 * @company xxx公司
 * @create  2019-11-28 16:34
 */
@SpringBootTest
public class BookServiceTest{
    @Autowired
    private BookService bookService;

    @Test
    public void selectByPrimaryKey() {
        System.out.println(bookService.selectByPrimaryKey(1));
    }

    @Test
    public void deleteByPrimaryKey() {
        bookService.deleteByPrimaryKey(1);
    }

    @Test
    public void listPager() {
        PageBean pageBean = new PageBean();
        pageBean.setPage(2);
        pageBean.setRows(20);
        Book book = new Book();
        book.setBname("%圣墟%");
        List<Book> books = bookService.listPager(book, pageBean);
        for (Book book1 : books) {
            System.out.println(book1);
        }
    }

}

springboot整合

3、springboot整合pagehelper

解决没有@aspect注解的问题的pom依赖

<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>

在yml文件中导入分页插件

#pagehelper分页插件配置
pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql

#显示日志
logging:
  level:
    com.javaxl.springboot02.mapper: debug

定义切面类

package com.zyc.springboot02.aspect;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyc.springboot02.util.PageBean;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Aspect
public class PagerAspect {

    @Around("execution(* *..*Service.*Pager(..))")
    public Object invoke(ProceedingJoinPoint args) throws Throwable{
        Object[] params = args.getArgs();
        PageBean pageBean = null;
        for (Object param : params) {
            if(param instanceof PageBean){
                pageBean = (PageBean) param;
                break;
            }
        }

        if (pageBean !=null && pageBean.isPagination())
            PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
        System.out.println("当前页:"+pageBean.getPage());
        System.out.println("页码:"+pageBean.getRows());

        Object proceed = args.proceed(params);

        if (pageBean !=null && pageBean.isPagination()){
            PageInfo pageInfo = new PageInfo((List)proceed);
            pageBean.setTotal(pageInfo.getTotal()+"");
            System.out.println("总条数:"+pageInfo.getTotal());
        }
        return proceed;
    }
}

还需要在启动类中加上以**解

//启动事务管理器
@EnableTransactionManagement
//启动切面注解
@EnableAspectJAutoProxy

当然还需要pageBean,在这里就不展示了
测试一个以Pager结尾的方法

@Test
    public void listPager() {
        PageBean pageBean = new PageBean();
        pageBean.setPage(2);
        pageBean.setRows(20);
        Book book = new Book();
        book.setBname("%圣墟%");
        List<Book> books = bookService.listPager(book, pageBean);
        for (Book book1 : books) {
            System.out.println(book1);
        }
    }

结果
springboot整合