# spring-agg **Repository Path**: archx/spring-agg ## Basic Information - **Project Name**: spring-agg - **Description**: 整合SpringMVC /MyBatis /Apache Shiro - **Primary Language**: Java - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 49 - **Forks**: 38 - **Created**: 2015-01-04 - **Last Updated**: 2022-05-16 ## Categories & Tags **Categories**: authority-management **Tags**: None ## README #SPRING-AGG > 这个一个框架整合案列,包含 SpringMVC/MyBatis/Apache Shiro 。 --- ## 演示数据 > - 演示数据请导入 [db.sql](scr/sql/db.sql) > - 演示账号密码均为 `12345` ![](src/sql/index.png) ## SpringMVC 配置 > 演示项目使用的是 [RESTful](http://baike.baidu.com/link?url=BsvYCNSLsqOmAwNsIuiaohRrpcZhwyRoidbaYsf51kSNtMnSK_G7Hk_dAl89KsE8R3-Nf4gBrE7hYY359M8noa) 风格,不是传统的拦截 `*.do`,`*.action`后缀的URL映射 **一些基本配置** 定制 **SpringMVC RESTful** 风格 1. [spring-mvc.xml](src/main/resources/spring-mvc.xml) 配置 ``` ``` 2. [web.xml](src/main/webapp/WEB-INF/web.xml) 配置 ``` spring org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring-mvc.xml 1 true spring / ``` 自定义注解和替换 **JSON** 转换器 > [spring-mvc.xml](src/main/resources/spring-mvc.xml) 中进行如下配置 ``` application/json;charset=UTF-8 ``` **控制器增强** 演示项目使用`JSON`作为返回数据,当用户进行一个没有权限的操作时会出现异常,如果异常处理跳转到视图的话返回的就不是有效的`JSON`格式数据。通过`@ControllerAdvice`注解来定义一个异常处理器返回有效`JSON`数据。 > @ControllerAdvice注解内部使用@ExceptionHandler、@InitBinder、@ModelAttribute注解的方法应用到所有的 @RequestMapping注解的方法。 1. [DefaultExceptionHandler](/src/main/java/org/springagg/web/exception/handler/DefaultExceptionHandler.java) 异常处理器 ``` /** * 异常处理器 * * @author ArchX[archx@foxmail.com] */ @ControllerAdvice public class DefaultExceptionHandler { /** * 没有权限异常 * * @param request * @param e * @return */ @ExceptionHandler({ UnauthorizedException.class }) @ResponseBody public JsonModel processUnauthenticatedException(NativeWebRequest request, UnauthorizedException e) { JsonModel json = new JsonModel(); json.setSuccess(false); json.setStatus("exception"); json.setMessage("没有权限 " + e.getMessage()); json.setObj(e.getClass().getName()); return json; } /** * 操作不允许 * * @param request * @param e * @return */ @ExceptionHandler({ OperationNotAllowedException.class }) @ResponseBody public JsonModel processOperationNotAllowedException(NativeWebRequest request, OperationNotAllowedException e) { JsonModel json = new JsonModel(); json.setSuccess(false); json.setStatus("exception"); json.setMessage("操作不允许 " + e.getMessage()); json.setObj(e.getClass().getName()); return json; } } ``` 2. [spring-mvc.xml](src/main/resources/spring-mvc.xml) 配置 ``` ``` 更多SpringMVC 配置细节请查看源码 ## Mybatis 分页插件配置 > 分页插件是基于Mybatis提供的拦截器机制实现,在SQL语句预编译之前拦截并针对不同的数据库拼接成新的分页语句再进行编译。 插件默认只实现了 **MySql** 和 **Oracle** 两种数据库的分页,配置方式如下: > 单独用MyBatis框架时,在Mybatis配置文件中进行插件配置 ``` ``` > 与Spring框架进行整合时,在 **SqlSessionFactoryBean** 的属性中进行配置 ``` ... 省略 ... ``` > 插件优先使用`dialectType` 配置,如果需要自定义分页或使用其他数据库,请实现 `org.springagg.mybatis.IDialect` 接口,然后配置 `dialectClazz` 属性。 **调用方式** 使用 `RowBounds` 及其子类作为分页参数即可触发分页插件,更多细节请查看源码 1. 接口定义 ``` @Repository public interface StudentMapper { List all(RowBounds pagination); } ``` 2. Mapper 实现 ``` ``` 关于分页插件详细使用方式可参照 [http://git.oschina.net/archx/mybatis-pagination](http://git.oschina.net/archx/mybatis-pagination) ## Apache Shiro 配置 > 使用 `shiro` 配置均参照 涛哥《跟我学Shiro》[http://jinnianshilongnian.iteye.com/blog/2018398](http://jinnianshilongnian.iteye.com/blog/2018398) 关于限制用户输入错误密码重复尝试次数的限定 > 当用户连续输入错误密码将不允许该账户登录 锁定时长由 [ehcache.xml](src/main/resources/ehcache.xml) 配置 ``` ... ``` 最多尝试次数 [spring-shiro.xml](src/main/resources/spring-shiro.xml) 配置 ``` ``` 更多细节请参照源码