前言:
上一章 “SpringBoot 项目(若依脚手架)4”的最后登录完成后,重定向到 /index 首页报
404 ,
本章就是解决 登录系统后显示首页内容。
目录
一、简单处理404错误
即登录成功后,跳转到 /index 页面,/index页面中显示 ”欢迎来到首页!!!“ 字样
1)创建
SysIndexController
类,处理 /index 请求
package com.ruoyi.web.controller.system;
import com.ruoyi.common.core.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @author liangcy
* @create 2019/10/8 - 18:35
*/
@Controller
public class SysIndexController extends BaseController {
@GetMapping("/index")
public String index(ModelMap mmap){
return "/index";
}
}
2)创建 template/
index.html
文件,内容如下:
欢迎来到首页!!!
3)启动项目,登录测试
首先访问首页地址:http://localhost:8080/index ,
由于没有登录,被shiro拦截请求,重定向地址到 http://localhost:8080/login
正确输入用户名 、密码和 验证码后,重定向到 首页,结果如图:
二、提前展示本章节完成后的效果
三、编写Controller 层代码
分析:登录成功后,页面重定向到 /index,因此编写
SysIndexController
类处理, /index 请求。
在 SysIndexController 类中,我们需要将当前登录 user 对象、user所拥有的菜单对象放入 map中传入前端页面,代码如下:
package com.ruoyi.web.controller.system;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.domain.SysMenu;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysMenuService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import javax.annotation.Resource;
import java.util.List;
/**
* @author liangcy
* @create 2019/10/8 - 18:35
*/
@Controller
public class SysIndexController extends BaseController {
@Resource
private ISysMenuService menuService;
@GetMapping("/index")
public String index(ModelMap mmap){
//获取身份信息
SysUser user = (SysUser)SecurityUtils.getSubject().getPrincipal();;
//根据用户id取出菜单
List<SysMenu> menus = menuService.selectMenusByUser(user);
mmap.put("menus", menus);
mmap.put("user", user);
return "/index";
}
}
四、编写model 层
创建
SysMenu
类
package com.ruoyi.system.domain;
import com.ruoyi.common.core.domain.BaseEntity;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
/**
* 菜单权限表 sys_menu
* @author liangcy
* @create 2019/10/14 - 21:41
*/
@Data
public class SysMenu extends BaseEntity {
//菜单ID
private Long menuId;
//菜单名称
private String menuName;
//父菜单名称
private String parentName;
//父菜单ID
private Long parentId;
//显示顺序
private String orderNum;
//菜单URL
private String url;
//打开方式: menuItem页签 menuBlank新窗口
private String target;
//类型:0目录,1菜单,2按钮
private String menuType;
//菜单状态:0显示,1隐藏
private String visible;
//权限字符串
private String perms;
//菜单图标
private String icon;
//子菜单
private List<SysMenu> children = new ArrayList<>();
}
补充:上述代码中的 List 集合属性,需要new ArrayList() ,这样可以避免以后用到这个对象的时候出现空指针异常
五、编写Service 层
编写
ISysMenuService
接口
package com.ruoyi.system.service;
import com.ruoyi.system.domain.SysMenu;
import com.ruoyi.system.domain.SysUser;
import java.util.List;
/**
* @author liangcy
* @create 2019/10/14 - 21:50
*/
public interface ISysMenuService {
/**
* 根据用户ID查询菜单
* @param user 用户信息
* @return 菜单列表
*/
public List<SysMenu> selectMenusByUser(SysUser user);
}
编写
ISysMenuService
接口实现类
SysMenuServiceImpl
SysMenuServiceImpl
中主要是
selectMenusByUser
方法获取当前用户对于的菜单列表,而
getChildPerms
、
recursionFn
、
getChildList
、
hasChild
四个方法主要是用来构建菜单的树模型(也就是递归获取当前用户所有的子菜单)
package com.ruoyi.system.service.impl;
import com.ruoyi.system.domain.SysMenu;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysMenuMapper;
import com.ruoyi.system.service.ISysMenuService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
/**
* @author liangcy
* @create 2019/10/14 - 21:52
*/
@Service
public class SysMenuServiceImpl implements ISysMenuService {
@Resource
private SysMenuMapper menuMapper;
@Override
public List<SysMenu> selectMenusByUser(SysUser user) {
List<SysMenu> menus = new LinkedList<>();
//管理员显示所有菜单
if (user.isAdmin()) {
menus = menuMapper.selectMenuNormalAll();
} else {
menus = menuMapper.selectMenusByUserId(user.getUserId());
}
return getChildPerms(menus, 0);
}
/**
* 根据父节点的ID获取所有子节点
*
* @param list 分类表
* @param parentId 传入的父节点ID
* @return String
*/
public List<SysMenu> getChildPerms(List<SysMenu> list, int parentId) {
List<SysMenu> returnList = new ArrayList<>();
for (SysMenu menu : list) {
if (menu.getParentId() == parentId) {
recursionFn(list, menu);
returnList.add(menu);
}
}
return returnList;
}
/**
* 递归列表
*
* @param list
* @param t
*/
private void recursionFn(List<SysMenu> list, SysMenu t) {
// 得到子节点列表
List<SysMenu> childList = getChildList(list, t);
t.setChildren(childList);
for (SysMenu tChild : childList) {
if (hasChild(list, tChild)) {
// 判断是否有子节点
Iterator<SysMenu> it = childList.iterator();
while (it.hasNext()) {
SysMenu n = (SysMenu) it.next();
recursionFn(list, n);
}
}
}
}
/**
* 得到子节点列表
*/
private List<SysMenu> getChildList(List<SysMenu> list, SysMenu t) {
List<SysMenu> tlist = new ArrayList<SysMenu>();
Iterator<SysMenu> it = list.iterator();
while (it.hasNext()) {
SysMenu n = (SysMenu) it.next();
if (n.getParentId().longValue() == t.getMenuId().longValue()) {
tlist.add(n);
}
}
return tlist;
}
/**
* 判断是否有子节点
*/
private boolean hasChild(List<SysMenu> list, SysMenu t) {
return getChildList(list, t).size() > 0 ? true : false;
}
}
因很多地方都需要判断 当前用户是否是 admin 所以在
SysUser
类加上
isAdmin
方法来判断
public boolean isAdmin(){
return isAdmin(this.userId);
}
public static boolean isAdmin(Long userId) {
return userId != null && 1L == userId;
}
六、编写 Mapper层
编写
SysMenuMapper
接口
package com.ruoyi.system.mapper;
import com.ruoyi.system.domain.SysMenu;
import java.util.List;
/**
* @author liangcy
* @create 2019/10/14 - 22:09
*/
public interface SysMenuMapper {
/**
* 查询系统正常显示菜单(不含按钮)
*
* @return 菜单列表
*/
public List<SysMenu> selectMenuNormalAll();
/**
* 根据用户ID查询菜单
*
* @param userId 用户ID
* @return 菜单列表
*/
public List<SysMenu> selectMenusByUserId(Long userId);
}
编写
SysMenuMapper.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.ruoyi.system.mapper.SysMenuMapper">
<resultMap id="SysMenuResult" type="SysMenu">
<id property="menuId" column="menu_id" />
<result property="menuName" column="menu_name" />
<result property="parentName" column="parent_name" />
<result property="parentId" column="parent_id" />
<result property="orderNum" column="order_num" />
<result property="url" column="url" />
<result property="target" column="target" />
<result property="menuType" column="menu_type" />
<result property="visible" column="visible" />
<result property="perms" column="perms" />
<result property="icon" column="icon" />
<result property="createBy" column="create_by" />
<result property="createTime" column="create_time" />
<result property="updateTime" column="update_time" />
<result property="updateBy" column="update_by" />
<result property="remark" column="remark" />
</resultMap>
<sql id="selectMenuVo">
select menu_id, menu_name, parent_id, order_num, url, target, menu_type,visible, ifnull(perms, '') as perms, icon, create_by, create_time
from sys_menu
</sql>
<select id="selectMenuNormalAll" resultMap="SysMenuResult">
SELECT distinct m.menu_id, m.parent_id, m.menu_name, m.url, m.visible, ifnull(m.perms, '') as perms, m.target, m.menu_type, m.icon, m.order_num, m.create_time
FROM sys_menu m
WHERE m.menu_type in ('M', 'C') AND m.visible = 0
ORDER BY m.parent_id, m.order_num
</select>
<select id="selectMenusByUserId" parameterType="Long" resultMap="SysMenuResult">
select distinct m.menu_id, m.parent_id, m.menu_name, m.url, m.visible, ifnull(m.perms,'') as perms, m.target, m.menu_type, m.icon, m.order_num, m.create_time
from sys_menu m
left join sys_role_menu rm on m.menu_id = rm.menu_id
left join sys_user_role ur on rm.role_id = ur.role_id
LEFT JOIN sys_role ro on ur.role_id = ro.role_id
where ur.user_id = #{userId} and m.menu_type in ('M', 'C') and m.visible = 0 AND ro.status = 0
order by m.parent_id, m.order_num
</select>
</mapper>
七、编写 index.html
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="renderer" content="webkit">
<title>若依系统首页</title>
<!--[if lt IE 9]>
<meta http-equiv="refresh" content="0;ie.html"/>
<![endif]-->
<link th:href="@{favicon.ico}" rel="stylesheet"/>
<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet"/>
<link th:href="@{/css/jquery.contextMenu.min.css}" rel="stylesheet"/>
<link th:href="@{/css/font-awesome.min.css}" rel="stylesheet"/>
<link th:href="@{/css/animate.css}" rel="stylesheet"/>
<link th:href="@{/css/style.css}" rel="stylesheet"/>
<link th:href="@{/css/skins.css}" rel="stylesheet"/>
<link th:href="@{/ruoyi/css/ry-ui.css?v=4.0.0}" rel="stylesheet"/>
</head>
<body class="fixed-sidebar full-height-layout gray-bg" style="overflow: hidden" th:classappend="|${@config.getKey('sys.index.sideTheme')} ${@config.getKey('sys.index.skinName')}|">
<div id="wrapper">
<!--左侧导航开始-->
<nav class="navbar-default navbar-static-side" role="navigation">
<div class="nav-close">
<i class="fa fa-times-circle"></i>
</div>
<div class="sidebar-collapse">
<ul class="nav" id="side-menu">
<li class="logo">
<span class="logo-lg">RuoYi</span>
</li>
<li>
<div class="user-panel">
<a class="menuItem" title="个人中心" th:href="@{/system/user/profile}">
<div class="hide" th:text="个人中心"></div>
<div class="pull-left image">
<img th:src="(${user.avatar} == '') ? @{/img/profile.jpg} : @{${user.avatar}}" class="img-circle" alt="User Image">
</div>
</a>
<div class="pull-left info">
<p>[[${user.loginName}]]</p>
<a href="#"><i class="fa fa-circle text-success"></i> 在线</a>
<a th:href="@{logout}" style="padding-left:5px;"><i class="fa fa-sign-out text-danger"></i> 注销</a>
</div>
</div>
</li>
<li class="active">
<a href="index.html"><i class="fa fa-home"></i> <span class="nav-label">主页</span> <span class="fa arrow"></span></a>
<ul class="nav nav-second-level">
<li class="active"><a class="menuItem" th:href="@{/system/main}">了解若依</a></li>
</ul>
</li>
<li th:each="menu : ${menus}">
<a th:class="@{${menu.url != '' && menu.url != '#'} ? ${menu.target}}" th:href="@{${menu.url == ''} ? |#| : ${menu.url}}">
<i class="fa fa-bar-chart-o" th:class="${menu.icon}"></i>
<span class="nav-label" th:text="${menu.menuName}">一级菜单</span>
<span th:class="${menu.url == '' || menu.url == '#'} ? |fa arrow|"></span>
</a>
<ul class="nav nav-second-level collapse">
<li th:each="cmenu : ${menu.children}">
<a th:if="${#lists.isEmpty(cmenu.children)}" th:class="${cmenu.target == ''} ? |menuItem| : ${cmenu.target}" th:utext="${cmenu.menuName}" th:href="@{${cmenu.url}}">二级菜单</a>
<a th:if="${not #lists.isEmpty(cmenu.children)}" href="#">[[${cmenu.menuName}]]<span class="fa arrow"></span></a>
<ul th:if="${not #lists.isEmpty(cmenu.children)}" class="nav nav-third-level">
<li th:each="emenu : ${cmenu.children}"><a th:class="${emenu.target == ''} ? |menuItem| : ${emenu.target}" th:text="${emenu.menuName}" th:href="@{${emenu.url}}">三级菜单</a></li>
</ul>
</li>
</ul>
</li>
<li th:if="${demoEnabled}">
<a href="#"><i class="fa fa-desktop"></i><span class="nav-label">实例演示</span><span class="fa arrow"></span></a>
<ul class="nav nav-second-level collapse">
<li> <a>表单<span class="fa arrow"></span></a>
<ul class="nav nav-third-level">
<li><a class="menuItem" th:href="@{/demo/form/button}">按钮</a></li>
<li><a class="menuItem" th:href="@{/demo/form/grid}">栅格</a></li>
<li><a class="menuItem" th:href="@{/demo/form/select}">下拉框</a></li>
<li><a class="menuItem" th:href="@{/demo/form/basic}">基本表单</a></li>
<li><a class="menuItem" th:href="@{/demo/form/cards}">卡片列表</a></li>
<li><a class="menuItem" th:href="@{/demo/form/jasny}">功能扩展</a></li>
<li><a class="menuItem" th:href="@{/demo/form/sortable}">拖动排序</a></li>
<li><a class="menuItem" th:href="@{/demo/form/tabs_panels}">选项卡 & 面板</a></li>
<li><a class="menuItem" th:href="@{/demo/form/validate}">表单校验</a></li>
<li><a class="menuItem" th:href="@{/demo/form/wizard}">表单向导</a></li>
<li><a class="menuItem" th:href="@{/demo/form/upload}">文件上传</a></li>
<li><a class="menuItem" th:href="@{/demo/form/datetime}">日期和时间</a></li>
<li><a class="menuItem" th:href="@{/demo/form/summernote}">富文本编辑器</a></li>
<li><a class="menuItem" th:href="@{/demo/form/duallistbox}">左右互选组件</a></li>
<li><a class="menuItem" th:href="@{/demo/form/autocomplete}">搜索自动补全</a></li>
</ul>
</li>
<li> <a>表格<span class="fa arrow"></span></a>
<ul class="nav nav-third-level">
<li><a class="menuItem" th:href="@{/demo/table/search}">查询条件</a></li>
<li><a class="menuItem" th:href="@{/demo/table/footer}">数据汇总</a></li>
<li><a class="menuItem" th:href="@{/demo/table/groupHeader}">组合表头</a></li>
<li><a class="menuItem" th:href="@{/demo/table/export}">表格导出</a></li>
<li><a class="menuItem" th:href="@{/demo/table/remember}">翻页记住选择</a></li>
<li><a class="menuItem" th:href="@{/demo/table/pageGo}">跳转至指定页</a></li>
<li><a class="menuItem" th:href="@{/demo/table/params}">自定义查询参数</a></li>
<li><a class="menuItem" th:href="@{/demo/table/multi}">初始多表格</a></li>
<li><a class="menuItem" th:href="@{/demo/table/button}">点击按钮加载表格</a></li>
<li><a class="menuItem" th:href="@{/demo/table/fixedColumns}">表格冻结列</a></li>
<li><a class="menuItem" th:href="@{/demo/table/event}">自定义触发事件</a></li>
<li><a class="menuItem" th:href="@{/demo/table/detail}">表格细节视图</a></li>
<li><a class="menuItem" th:href="@{/demo/table/image}">表格图片预览</a></li>
<li><a class="menuItem" th:href="@{/demo/table/curd}">动态增删改查</a></li>
<li><a class="menuItem" th:href="@{/demo/table/reorder}">表格拖拽操作</a></li>
<li><a class="menuItem" th:href="@{/demo/table/editable}">表格行内编辑</a></li>
<li><a class="menuItem" th:href="@{/demo/table/other}">表格其他操作</a></li>
</ul>
</li>
<li> <a>弹框<span class="fa arrow"></span></a>
<ul class="nav nav-third-level">
<li><a class="menuItem" th:href="@{/demo/modal/dialog}">模态窗口</a></li>
<li><a class="menuItem" th:href="@{/demo/modal/layer}">弹层组件</a></li>
<li><a class="menuItem" th:href="@{/demo/modal/table}">弹层表格</a></li>
</ul>
</li>
<li> <a>操作<span class="fa arrow"></span></a>
<ul class="nav nav-third-level">
<li><a class="menuItem" th:href="@{/demo/operate/table}">表格</a></li>
<li><a class="menuItem" th:href="@{/demo/operate/other}">其他</a></li>
</ul>
</li>
<li> <a>报表<span class="fa arrow"></span></a>
<ul class="nav nav-third-level">
<li><a class="menuItem" th:href="@{/demo/report/echarts}">百度ECharts</a></li>
<li><a class="menuItem" th:href="@{/demo/report/peity}">peity</a></li>
<li><a class="menuItem" th:href="@{/demo/report/sparkline}">sparkline</a></li>
<li><a class="menuItem" th:href="@{/demo/report/metrics}">图表组合</a></li>
</ul>
</li>
<li> <a>图标<span class="fa arrow"></span></a>
<ul class="nav nav-third-level">
<li><a class="menuItem" th:href="@{/demo/icon/fontawesome}">Font Awesome</a></li>
<li><a class="menuItem" th:href="@{/demo/icon/glyphicons}">Glyphicons</a></li>
</ul>
</li>
<li>
<a href="#"><i class="fa fa-sitemap"></i> <span class="nav-label">四层菜单 </span><span class="fa arrow"></span></a>
<ul class="nav nav-second-level collapse">
<li>
<a href="#" id="damian">三级菜单1<span class="fa arrow"></span></a>
<ul class="nav nav-third-level">
<li>
<a href="#">四级菜单1</a>
</li>
<li>
<a href="#">四级菜单2</a>
</li>
</ul>
</li>
<li><a href="#">三级菜单2</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</nav>
<!--左侧导航结束-->
<!--右侧部分开始-->
<div id="page-wrapper" class="gray-bg dashbard-1">
<div class="row border-bottom">
<nav class="navbar navbar-static-top" role="navigation" style="margin-bottom: 0">
<div class="navbar-header">
<a class="navbar-minimalize minimalize-styl-2" style="color:#FFF;" href="#" title="收起菜单">
<i class="fa fa-bars"></i>
</a>
</div>
<ul class="nav navbar-top-links navbar-right welcome-message">
<li><a title="视频教程" href="http://doc.ruoyi.vip/#/standard/spjc" target="_blank"><i class="fa fa-video-camera"></i> 视频教程</a></li>
<li><a title="开发文档" href="http://doc.ruoyi.vip" target="_blank"><i class="fa fa-question-circle"></i> 开发文档</a></li>
<li><a title="全屏显示" href="javascript:void(0)" id="fullScreen"><i class="fa fa-arrows-alt"></i> 全屏显示</a></li>
<li class="dropdown user-menu">
<a href="javascript:void(0)" class="dropdown-toggle" data-hover="dropdown">
<img th:src="(${user.avatar} == '') ? @{/img/profile.jpg} : @{${user.avatar}}" class="user-image">
<span class="hidden-xs">[[${user.userName}]]</span>
</a>
<ul class="dropdown-menu">
<li class="mt5">
<a th:href="@{/system/user/profile}" class="menuItem">
<i class="fa fa-user"></i> 个人中心</a>
</li>
<li>
<a onclick="resetPwd()" class="menuItem">
<i class="fa fa-key"></i> 修改密码</a>
</li>
<li class="divider"></li>
<li>
<a th:href="@{logout}">
<i class="fa fa-sign-out"></i> 退出登录</a>
</li>
</ul>
</li>
</ul>
</nav>
</div>
<div class="row content-tabs">
<button class="roll-nav roll-left tabLeft">
<i class="fa fa-backward"></i>
</button>
<nav class="page-tabs menuTabs">
<div class="page-tabs-content">
<a href="javascript:;" class="active menuTab" data-id="/system/main">首页</a>
</div>
</nav>
<button class="roll-nav roll-right tabRight">
<i class="fa fa-forward"></i>
</button>
<a href="#" class="roll-nav roll-right tabReload"><i class="fa fa-refresh"></i> 刷新</a>
</div>
<a id="ax_close_max" class="ax_close_max" href="#" title="关闭全屏"> <i class="fa fa-times-circle-o"></i> </a>
<div class="row mainContent" id="content-main">
<iframe class="RuoYi_iframe" name="iframe0" width="100%" height="100%" data-id="/system/main"
th:src="@{/system/main}" frameborder="0" seamless></iframe>
</div>
<div class="footer">
<div class="pull-right">© [[${copyrightYear}]] RuoYi Copyright </div>
</div>
</div>
<!--右侧部分结束-->
</div>
<!-- 全局js -->
<script th:src="@{/js/jquery.min.js}"></script>
<script th:src="@{/js/bootstrap.min.js}"></script>
<script th:src="@{/js/plugins/metisMenu/jquery.metisMenu.js}"></script>
<script th:src="@{/js/plugins/slimscroll/jquery.slimscroll.min.js}"></script>
<script th:src="@{/js/jquery.contextMenu.min.js}"></script>
<script th:src="@{/ajax/libs/blockUI/jquery.blockUI.js}"></script>
<script th:src="@{/ajax/libs/layer/layer.min.js}"></script>
<script th:src="@{/ruoyi/js/ry-ui.js?v=4.0.0}"></script>
<script th:src="@{/ruoyi/index.js}"></script>
<script th:src="@{/ajax/libs/fullscreen/jquery.fullscreen.js}"></script>
<script th:inline="javascript">
var ctx = [[@{/}]];
/*用户管理-重置密码*/
function resetPwd() {
var url = ctx + 'system/user/profile/resetPwd';
$.modal.open("重置密码", url, '800', '500');
}
</script>
</body>
八、启动项目,完成登录系统后报错
前端报错信息:
后台报错信息:
index页面相关内容
九、thymeleaf 模板调用后台
后面的章节中经常会看到这种 在html 中调用后台 service 方法获取数据
1)编写 Service 类解决上面出现的问题
package com.ruoyi.framework.web.service;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @author liangcy
* @create 2019/10/15 - 9:43
*/
@Service("config")
public class ConfigService {
@Resource
private ISysConfigService configService;
/**
* 根据键名查询参数配置信息
*
* @param configKey 参数名称
* @return 参数键值
*/
public String getKey(String configKey){
return configService.selectConfigByKey(configKey);
}
}
package com.ruoyi.system.service;
/**
* @author liangcy
* @create 2019/10/15 - 9:46
*/
public interface ISysConfigService {
public String selectConfigByKey(String configKey);
}
ISysConfigService 接口的实现类 SysConfigServiceImpl
package com.ruoyi.system.service.impl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.mapper.SysConfigMapper;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @author liangcy
* @create 2019/10/15 - 9:47
*/
@Service
public class SysConfigServiceImpl implements ISysConfigService {
@Resource
private SysConfigMapper configMapper;
@Override
public String selectConfigByKey(String configKey) {
SysConfig config = new SysConfig();
config.setConfigKey(configKey);
SysConfig retConfig = configMapper.selectConfig(config);
return StringUtils.isNotNull(retConfig) ? retConfig.getConfigValue() : "";
}
}
2)编写 mapper层
package com.ruoyi.system.mapper;
import com.ruoyi.system.domain.SysConfig;
/**
* @author liangcy
* @create 2019/10/15 - 9:50
*/
public interface SysConfigMapper {
public SysConfig selectConfig(SysConfig config);
}
3)编写 mapper 映射文件
<?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.ruoyi.system.mapper.SysConfigMapper">
<resultMap type="SysConfig" id="SysConfigResult">
<id property="configId" column="config_id" />
<result property="configName" column="config_name" />
<result property="configKey" column="config_key" />
<result property="configValue" column="config_value" />
<result property="configType" column="config_type" />
<result property="createBy" column="create_by" />
<result property="createTime" column="create_time" />
<result property="updateBy" column="update_by" />
<result property="updateTime" column="update_time" />
</resultMap>
<sql id="selectConfigVo">
select config_id, config_name, config_key, config_value, config_type, create_by, create_time, update_by, update_time, remark
from sys_config
</sql>
<sql id="sqlwhereSearch">
<where>
<if test="configId != null">
and config_id = #{configId}
</if>
<if test="configKey != null and configKey != ''">
and config_Key = #{configKey}
</if>
</where>
</sql>
<select id="selectConfig" parameterType="SysConfig" resultMap="SysConfigResult">
<include refid="selectConfigVo"/>
<include refid="sqlwhereSearch"/>
</select>
</mapper>
4)编写实体类
package com.ruoyi.system.domain;
import com.ruoyi.common.core.domain.BaseEntity;
import lombok.Data;
/**
* @author liangcy
* @create 2019/10/15 - 9:52
*/
@Data
public class SysConfig extends BaseEntity {
//参数主键
private Long configId;
//参数名称
private String configName;
//参数键名
private String configKey;
//参数键值
private String configValue;
//系统内置(Y是,N否)
private String configType;
}
十、重启服务,重写登录