1、使用mybatisplus-spring-boot-starter时,不要在使用mybatis-spring-boot-starter,这是个坑。
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>${mybatisplus.spring.boot.version}</version>
</dependency>
2、加上devtools应用支持热部署,提高开发者的开发效率,无需手动重启Spring Boot应用。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
自动重启spring boot 也挺耗时间的,在App类main方法中加上下面红色这句,开发时如果有小的改动(不新增方法、变量)时,可以不自动重启而代码生效,System.setProperty("spring.devtools.restart.enabled", "false");。
public static void main(String[] args) {
System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(DataApplication.class, args);
}
3、s使用AbstractRoutingDataSource实现多数据源
直接上代码,使用方法1、手工切换2、AOP实现
public class DynamicDataSource extends AbstractRoutingDataSource {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
public DynamicDataSource(DataSource defaultTargetDataSource, Map<Object, Object> targetDataSources) {
super.setDefaultTargetDataSource(defaultTargetDataSource);
super.setTargetDataSources(targetDataSources);
super.afterPropertiesSet();
}
@Override
protected Object determineCurrentLookupKey() {
return getDataSource();
}
public static void setDataSource(String dataSource) {
contextHolder.set(dataSource);
}
public static String getDataSource() {
return contextHolder.get();
}
public static void clearDataSource() {
contextHolder.remove();
}
}
手工切换方式如下
DynamicDataSource.setDataSource("second");
AcctUserEntity entity = acctUserService.selectById(11);
DynamicDataSource.clearDataSource();
AOP方法切换
4、在freemarker页面中获取contextPath的几种方式
- 1
<script type="application/javascript">
var ctx = '${request.contextPath}';
</script>
- 2
<script src="${request.contextPath}/statics/libs/jquery.min.js"></script>
- 3
<#assign ctx=request.getContextPath()>
然后在js或者页面里就可以直接这样用了
<script src="${ctx}/plugins/jquery.min.js" type="text/javascript"></script>
<script>
var ctx = '${ctx}';
</script>
5、shiro认证成功后,跳转到认证前的URL,想让他统一跳到主页
public class MyFormAuthenticationFilter extends FormAuthenticationFilter {
@Override
public boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request,
ServletResponse response) throws IOException {
// 无论认证前的url是什么,认证成功后跳到index
WebUtils.issueRedirect(request, response, getSuccessUrl());
return false;
}
}
WebUtils.issueRedirect(request, response, getSuccessUrl());
return false;
}
}
6、websocket,在js中链接时报302的错误。
原因:1、链接被shiro拦截 2、springboot 和ssm框架中不一样,需要注册@Bean ServerEndpointExporter
7、在freemarker中使用shiro标签shiroTag
在jsp中使用shiro标签比较简单,在freemarker中使用要加入maven依赖
<dependency>
<groupId>net.mingsoft</groupId>
<artifactId>shiro-freemarker-tags</artifactId>
<version>0.1</version>
</dependency>
配置类FreemarkerConfig来对Freemark使用Shiro标签进行配置,引入
@Configuration
public class FreemarkerConfig {
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("classpath:/templates");
Map<String, Object> variables = new HashMap<>(1);
variables.put("shiro", new ShiroTags());
configurer.setFreemarkerVariables(variables);
Properties settings = new Properties();
settings.setProperty("default_encoding", "utf-8");
settings.setProperty("number_format", "0.##");
configurer.setFreemarkerSettings(settings);
return configurer;
}
}
我们看下其中的ShiroTags类,查看每个tag的定义,我们也可以自定义自己的标签
package com.jagregory.shiro.freemarker;
import freemarker.template.SimpleHash;
/**
* Shortcut for injecting the tags into Freemarker
*
* <p>Usage: cfg.setSharedVeriable("shiro", new ShiroTags());</p>
*/
public class ShiroTags extends SimpleHash {
public ShiroTags() {
put("authenticated", new AuthenticatedTag());
put("guest", new GuestTag());
put("hasAnyRoles", new HasAnyRolesTag());
put("hasPermission", new HasPermissionTag());
put("hasRole", new HasRoleTag());
put("lacksPermission", new LacksPermissionTag());
put("lacksRole", new LacksRoleTag());
put("notAuthenticated", new NotAuthenticatedTag());
put("principal", new PrincipalTag());
put("user", new UserTag());
}
}
在freemarker页面中使用标签
<@shiro.hasPermission name="P_D_AUDIT">
<li class="accident-analyze">
<a href="${ctx}/analyze/congestion/analyze">
<i class="fa fa-user"></i> </a>
</li>
</@shiro.hasPermission>
<@shiro.principal property="userName" /> </span>
<@shiro.guest>
游客访问 <a href = "login.jsp"></a>
</@shiro.guest>