Springboot项目小笔记

本文主要记录我使用idea搭建springboot项目时小功能配置

ueditor富文本编辑器

  1. yml文件配置
    1
    2
    3
    #  富文本编辑器
    ue:
    server-url: /ueditor/jsp/controller
  2. 依赖导入
    1
    2
    3
    4
    5
    <dependency>
    <groupId>cn.jasonone.ueditor</groupId>
    <artifactId>ueditor-spring-boot-starter</artifactId>
    <version>1.1.4</version>
    </dependency>
  3. 导入ueditor
    1
    <script type="text/plain" id="editor" name="sonContent"></script>

SpringBoot的yml文件配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
server:
port: 80

logging:
level:
cn.blue: trace
#优先级从高至低依次为:OFF、FATAL、ERROR、WARN、INFO、DEBUG、TRACE

# layui模板里的时间格式配置
spring:
jackson:
date-format: yyyy-MM-dd HH:mm
time-zone: "GMT+8"
---

spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/rbac?serverTimezone=GMT%2b8
username: root
password:

# 开发阶段关闭thymeleaf的模板缓存
thymeleaf:
cache: false

mybatis:
mapper-locations: classpath:cn/blue/mapper/sqlmap/*.xml
configuration:
map-underscore-to-camel-case: true

# 配置别名扫描的包
type-aliases-package: com.hm.publicraise.bean

# 头像存储位置
head-img-path: classpath:/static/upload/heads

# 头像存储位置
raise-img-path: classpath:/static/upload/raises

# 富文本编辑器
ue:
server-url: /ueditor/jsp/controller

Invalid bound statement(not found),访问不到mapper.xml

解决方法

application配置文件中mybatis配置

1
2
3
4
mybatis:
mapper-locations: classpath:/mapper/*.xml
configuration:
map-underscore-to-camel-case: true

代码正确,无法访问controller层方法

其他包都得放在启动类包下

拦截器

登录拦截器

1.LoginController主要代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* 登录方法
*/
@PostMapping
public String login(UserInfo userInfo, HttpSession session) {
try {
UserInfo user = userInfoService.findOneUser(userInfo);
if (user != null) {
//存放引用常量的位置:ApplicationConst
session.setAttribute(ApplicationConst.LOGIN_SESSION_STATUS, user);
return "index";
}
} catch (Exception e) {
e.printStackTrace();
}
//重定向回去会调用controller,但是请求转发只会访问目录下的资源文件
return "redirect:/login";
}

2.LoginHandlerInterceptor 登录拦截器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Component
public class LoginHandlerInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
UserInfo user = (UserInfo) session.getAttribute(ApplicationConst.LOGIN_SESSION_STATUS);
if (user != null) {
//放行
return true;
} else {
response.sendRedirect(request.getContextPath() + "/login");
return false;
}
}
}

3.配置WebMvcConfigurer 拦截器,将登录拦截器添加进去

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor())
//拦截所有
.addPathPatterns("/**")
//排除资源文件和登录、注销请求
.excludePathPatterns("/login", "/logout")
.excludePathPatterns("/layui/**", "/**/*.js")
.excludePathPatterns("/**/*.jpg", "/**/*.png");
}
}
点击查看

本文标题:Springboot项目小笔记

文章作者:Liuyang

发布时间:2020年01月06日 - 23:49:34

最后更新:2020年01月13日 - 11:19:08

原始链接:http://yoursite.com/2020/01/06/idea%E6%90%AD%E5%BB%BASpringboot%E9%A1%B9%E7%9B%AE/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------------本文结束 感谢您的阅读-------------------
0%