栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot整合druid、mybatis、pagehelper、freemarker、redis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合druid、mybatis、pagehelper、freemarker、redis

1.创建Spring Initializr项目

导入如下依赖  

1.配置druid连接池
1.1 相关依赖

        
            com.alibaba
            druid-spring-boot-starter
            1.1.10
        

1.2 配置druid相关的application.yml

druid:
        #2.连接池配置
        #初始化连接池的连接数量 大小,最小,最大
        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
        # 是否缓存preparedStatement,也就是PSCache  官方建议MySQL下建议关闭   个人建议如果想用SQL防火墙 建议打开
        pool-prepared-statements: true
        max-pool-prepared-statement-per-connection-size: 20
        # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
        filter:
            stat:
                merge-sql: true
                slow-sql-millis: 5000
        #3.基础监控配置
        web-stat-filter:
            enabled: true
            url-pattern: public int getStartIndex() { //(当前页码-1)*显示记录数 return (this.getPage()-1)*this.rows; } public int getMaxPage() { int totalpage=this.total/this.rows; if(this.total%this.rows!=0) totalpage++; return totalpage; } public int getNextPage() { int nextPage=this.page+1; if(this.page>=this.getMaxPage()) nextPage=this.getMaxPage(); return nextPage; } public int getPreivousPage() { int previousPage=this.page-1; if(previousPage<1) previousPage=1; return previousPage; } @Override public String toString() { return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", isPagination=" + isPagination + "]"; } }

3.4 在service层的查全部方法中加入PageBean参数即可

4.springboot之freemarker

1. freemarker介绍
   FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据,并用来生成输出文本(HTML网页、电子邮件、配置文件、源代码等)的通用工具。
 
   注1:springboot中支持的两种模板技术
        thymeleaf/freemarker(默认)
4.1 在pom.xml引入freeMarker的依赖包

      
        org.springframework.boot
        spring-boot-starter-freemarker
      

4.2 配置application.yml

spring: 
      freemarker:
        #指定HttpServletRequest的属性是否可以覆盖controller的model的同名项
        allow-request-override: false
    #req访问request
        request-context-attribute: req
    #后缀名freemarker默认后缀为.ftl,当然你也可以改成自己习惯的.html
        suffix: .ftl
    #设置响应的内容类型
        content-type: text/html;charset=utf-8
    #是否允许mvc使用freemarker
        enabled: true
    #是否开启template caching
        cache: false
    #设定模板的加载路径,多个以逗号分隔,默认: [“classpath:/templates/”]
        template-loader-path: classpath:/templates/
    #设定Template的编码
        charset: UTF-8
      # 设置静态文件路径,js,css等
    mvc:
      static-path-pattern: /static @Configuration public class RedisConfig { @Bean public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory){ RedisTemplate redisTemplate = new RedisTemplate<>(); //设置连接工厂类 redisTemplate.setConnectionFactory(connectionFactory); //针对Stirng类型的key和value进行序列化操作 redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); //针对hash类型的key和value进行序列化操作 redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); //更改配置 redisTemplate.afterPropertiesSet(); return redisTemplate; } }

7.4 测试

package com.zking.spbootmp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zking.spbootmp.model.Book;
import com.zking.spbootmp.mapper.BookMapper;
import com.zking.spbootmp.service.BookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zking.spbootmp.util.PageBean;
import freemarker.template.utility.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Service
public class BookServiceImpl extends ServiceImpl implements BookService {

    @Resource
    private BookMapper bookMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public List queryBookAll(Book book) {
        List lst = bookMapper.selectList(new QueryWrapper().like("book_name",book.getBookName()));
        redisTemplate.opsForValue().set("book:queryBookAll",lst);
        return lst;
    }

    @Override
    public Book querySingleBookById(Integer bookId) {
        Book book = bookMapper.selectOne(new QueryWrapper()
                .eq("book_id", bookId));
        redisTemplate.opsForValue().set("book:querySingleBookById",book,20l, TimeUnit.SECONDS);
        return book;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/708416.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号