在项目中平常须求后台程序的长久层查询数据库来获取数据,然后将数据交到服务层、调节层,最后才交给视图层。固然数据访谈缓慢,就能影响程序的运转。

1.前言

本文重要介绍使用SpringBoot与shiro达成基于数据库的细粒度动态权限管理种类实例。
应用技艺:SpringBoot、mybatis、shiro、thymeleaf、pagehelper、Mapper插件、druid、dataTables、ztree、jQuery
开辟工具:intellij idea
数据库:mysql、redis
多数是依据使用SpringSecurity的demo上改变而成,地址

为了加紧程序的运行,能够将数据归入缓存中,包含数据缓存和页面缓存。

2.表结构

抑或是用规范的5张表来表现权限。如下图:4503.com 1
分级为客商表,剧中人物表,能源表,客商剧中人物表,角色能源表。在这里个demo中应用了mybatis-generator自动生成代码。运转mybatis-generator:generate
-e 依照数据库中的表,生成
相应的model,mapper单表的增加和删除改查。可是只假设导入本项目标就别运维这几个命令了。新扩展表的话,也要改良mybatis-generator-config.xml中的tableName,内定表名再运维。

所谓缓存,就是将前后相继或系统有时要调用的对象存在内部存储器中,三次其行使时得以急忙调用,不必再去创立新的再一次的实例。那样做能够减去系统开辟,进步系统效用。

3.maven配置

<?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>

    <groupId>com.study</groupId>
    <artifactId>springboot-shiro</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-shiro</name>
    <description>Demo project for Spring Boot</description>

    <parent>        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-thymeleaf</artifactId>        </dependency>        <dependency>            <groupId>com.github.pagehelper</groupId>            <artifactId>pagehelper-spring-boot-starter</artifactId>            <version>1.1.0</version>        </dependency>        <dependency>            <groupId>tk.mybatis</groupId>            <artifactId>mapper-spring-boot-starter</artifactId>            <version>1.1.1</version>        </dependency>        <dependency>            <groupId>org.apache.shiro</groupId>            <artifactId>shiro-spring</artifactId>            <version>1.3.2</version>        </dependency>        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>druid</artifactId>            <version>1.0.29</version>        </dependency>        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>        </dependency>        <dependency>            <groupId>net.sourceforge.nekohtml</groupId>            <artifactId>nekohtml</artifactId>            <version>1.9.22</version>        </dependency>        <dependency>            <groupId>com.github.theborakompanioni</groupId>            <artifactId>thymeleaf-extras-shiro</artifactId>            <version>1.2.1</version>        </dependency>        <dependency>            <groupId>org.crazycake</groupId>            <artifactId>shiro-redis</artifactId>            <version>2.4.2.1-RELEASE</version>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>            <plugin>                <groupId>org.mybatis.generator</groupId>                <artifactId>mybatis-generator-maven-plugin</artifactId>                <version>1.3.5</version>                <configuration>                    <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>                    <overwrite>true</overwrite>                    <verbose>true</verbose>                </configuration>                <dependencies>                    <dependency>                        <groupId>mysql</groupId>                        <artifactId>mysql-connector-java</artifactId>                        <version>${mysql.version}</version>                    </dependency>                    <dependency>                        <groupId>tk.mybatis</groupId>                        <artifactId>mapper</artifactId>                        <version>3.4.0</version>                    </dependency>                </dependencies>            </plugin>        </plugins>    </build></project>

里头页面缓存首假如oscache,能够整页或然钦点网页某一局地缓存,同期钦点他的过期时间,那样在那时间段里面访谈的数据都以千篇一律的

4.配置Druid

package com.study.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
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;


/**
 * Created by yangqj on 2017/4/19.
 */
@Configuration
public class DruidConfig {
    @Bean
    public ServletRegistrationBean druidServlet() {

        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        //登录查看信息的账号密码.

        servletRegistrationBean.addInitParameter("loginUsername","admin");
        servletRegistrationBean.addInitParameter("loginPassword","123456");
        return servletRegistrationBean;
    }
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter;
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}

在application.properties中加入:

# 数据源基础配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/shiro
spring.datasource.username=root
spring.datasource.password=root
# 连接池配置
# 初始化大小,最小,最大
spring.datasource.initialSize=1
spring.datasource.minIdle=1
spring.datasource.maxActive=20

布局好后,运维品种访谈
输入配置的账号密码admin,123456进来:4503.com 2

数据缓存则超多,有有名的ehcache,redis,memcached等。

5.配置mybatis

选择springboot 整合mybatis特别常有利,只需在application.properties

mybatis.type-aliases-package=com.study.model
mybatis.mapper-locations=classpath:mapper/*.xml
mapper.mappers=com.study.util.MyMapper
mapper.not-empty=false
mapper.identity=MYSQL
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true

pagehelper.params=count\=countSql

将相应的门道改成项目包所在的路径就可以。配置文件中可以看出来还进入了pagehelper
和Mapper插件。假设无需,把地点配置文件中的 pagehelper删除。

正文首要回顾的行使Java代码举行redis缓存,即在查询的时候先在service层从redis缓存中获取数据。即使不设有,则再通过dao层从数据库中得到,最后将查询到的数额缓存到redis中;如果存在,直接从redis缓存中读取,并提交controller层。

MyMapper:
package com.study.util;

/**
 * Created by yangqj on 2017/4/20.
 */
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
}

对于Springboot整合mybatis能够参照他事他说加以考查

xml配置:

6.thymeleaf配置

thymeleaf是springboot官方推荐的,所以来试一下。
首先步向配置:

#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
# ;charset=<encoding> is added
#spring.thymeleaf.content-type=text/html
# set to false for hot refresh
spring.thymeleaf.cache=false
spring.thymeleaf.mode=LEGACYHTML5

能够看来实际上边都是注释了的,因为springboot会依赖名噪一时的不二等秘书技帮我们布置好。所以地点注释部分是springboot自动配置的,要是须要自定义配置,只供给改正上解说部分就能够。
后两行并未有注释的局地,spring.thymeleaf.cache=false代表关闭缓存,那样改进文件后没有必要再一次开动,缓存暗许是敞开的,所以钦赐为false。可是在intellij
idea中还亟需按Ctrl + Shift + F9.
对于spring.thymeleaf.mode=LEGACYHTML5。thymeleaf对html中的语法必要极其严酷,像自家从网络找的模板,使用thymeleaf后报一批的语法错误,后来不能够,使用弱语法校验,所以参加配置spring.thymeleaf.mode=LEGACYHTML5。参与这一个布局后还亟需在maven中加入

<dependency>
    <groupId>net.sourceforge.nekohtml</groupId>
    <artifactId>nekohtml</artifactId>
    <version>1.9.22</version>
</dependency>

要不会报错的。
在前面二个页面包车型大巴头顶出席一下配备后,就能够应用thymeleaf了

<link rel="stylesheet" th:href="@{/css/bootstrap.min.css}" />

而是那个体系因为使用了datatables都是使用jquery
的ajax来访谈数据与管理数据,所以用到的thymeleaf语法很少,基本上能够参见的就是js即css的导入和相近于jsp的include功效的有的页面引进。
对此静态文件的引入:

 <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}" />

而文件在类型中之处是static-css-bootstrap.min.css。为何这样能够访谈到该公文,也是因为springboot对于静态文件会自行检索/static
public、/resources、/META-INF/resources下的公文。所以没有必要加static.

页面引进:
有的页面如下:

<div  th:fragment="top">    ...</div>

大旨页面映入格局:

<div th:include="common/top :: top"></div>

inclide=”文件路线::局地代码片段名称”

bean  property name="maxTotal" value="${redis.maxTotal}"/property property name="maxIdle" value="${redis.maxIdle}"/property property name="testOnBorrow" value="${redis.testOnBorrow}"/property property name="maxWaitMillis" value="${redis.maxWaitMillis}"/property /bean !-- jedis客户端单机版 -- bean  constructor-arg name="poolConfig" ref="poolConfig"/constructor-arg constructor-arg name="host" value="${redis.hostName}"/constructor-arg constructor-arg name="port" value="${redis.port}"/constructor-arg /bean !-- jedis集群版配置 -- bean  constructor-arg name="poolConfig" ref="poolConfig"/constructor-arg constructor-arg name="nodes" set bean  constructor-arg name="host" value="127.0.0.1"/constructor-arg constructor-arg name="port" value="7001"/constructor-arg /bean bean  constructor-arg name="host" value="127.0.0.1"/constructor-arg constructor-arg name="port" value="7002"/constructor-arg /bean bean  constructor-arg name="host" value="127.0.0.1"/constructor-arg constructor-arg name="port" value="7003"/constructor-arg /bean bean  constructor-arg name="host" value="127.0.0.1"/constructor-arg constructor-arg name="port" value="7004"/constructor-arg /bean bean  constructor-arg name="host" value="127.0.0.1"/constructor-arg constructor-arg name="port" value="7005"/constructor-arg /bean /set /constructor-arg /bean [java] view plain copyjava调用 //service层查询时代码 @Autowired private JedisClient jedisClient; @Override public ListUser list() { // TODO Auto-generated method stub try { String json = jedisClient.hget("user", "list"); if (StringUtils.isNotBlank(json)) { ListUser users = JsonUtils.jsonToList(json, User.class); return users; } } catch (Exception e) { e.printStackTrace(); } //使用持久层查询数据库 ListUser list = userMapper.list(); try { jedisClient.hset("user", "list", JsonUtils.objectToJson(list)); } catch (Exception e) { e.printStackTrace(); } return list; } 

7.shiro配置