springboot2.0.2+mybatisplus+shiro+freemarker框架使用过程记录

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方法中加上下面红色这句,开发时如果有小的改动(不新增方法、变量)时,可以不自动重启而代码生效。

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页面中通过

<script type="application/javascript">
	var ctx = '${request.contextPath}';
</script>
<script src="${request.contextPath}/statics/libs/jquery.min.js"></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;
	}
}






猜你喜欢

转载自blog.csdn.net/liyongjian12/article/details/80289332