@Test public void testSet(){ List<Map<String,Object>> list=new ArrayList<Map<String, Object>>(); Map<String,Object> map=new HashMap<String, Object>(); map.put("name","name1"); map.put("age",12); list.add(map); Map<String,Object> map2=new HashMap<String, Object>(); map2.put("name","name2"); map2.put("age",13); list.add(map2); System.out.println("排序前:"+list.toString()); list=list.stream() //第一个排序条件 //假设自定义对象为Obj,则可直接写为Obj::getName .sorted(Comparator.comparing(Application::getName) //第二个排序条件(可一直延伸) .thenComparing(Application::getAge).reversed()).collect(Collectors.toList()); System.out.println("排序后:"+list.toString()); } private static String getName(Map<String,Object> map){ return (String) map.get("name"); } private static int getAge(Map<String,Object> map){ return (int) map.get("age"); }
月度归档: 2019年3月
MybatisPlus分页优化
在采用MyBatisPlus进行开发时,避免不了会引用其提供的分页插件,在引用的时候通过查看代码会发现,他的默认取值逻辑是先查出数据库中所有的数据,然后根据分页参数再取出其中的一页数据;
在一些数据量较大的地方这么拿数据明显不是最优解;
依据我们日常的习惯,肯定是会把分页直接放到sql中,直接查询出某一页的数据进行优化;
MyBatisPlus中提供了一个配置:
import com.baomidou.mybatisplus.plugins.PaginationInterceptor; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @MapperScan("com.szss.admin.dao.*")//如代码中有设置范围,则此处无需设置 public class MybatisPlusConfig { /** * mybatis-plus分页插件<br> * 文档:http://mp.baomidou.com<br> */ @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); return paginationInterceptor; } }
如开放debugSql打印,可以看出前后的sql变化;
这个时候扫描路径下的所有分页都已完成了执行逻辑
长链接转短链接
应用场景:之前手机上收到的垃圾短信都是一大串的连接;现在收到的基本上都是很短的连接 * 大部分长连接转短连接都是采用新浪微博平台的开放接口;下方实际也是采用微博提供的接口
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>生成短链接</title> </head> <!-- <script src="jquery-2.1.1.min.js"></script> --> <body> <input type="text" name="" id="url"> <button id="get_short">生成</button> <a id="myDiv">点我</a> </body> <script type="text/javascript"> // 绑定按钮事件 btn_getshort = document.getElementById('get_short'); btn_getshort.onclick = function (){ var source = '3271760578'; var url = document.getElementById('url').value; var all = 'http://api.t.sina.com.cn/short_url/shorten.json?source=' + source +'&url_long='+ url; document.getElementById("myDiv").href = all; } </script> </html>
Spring切面处理日志
业务场景:在web服务中添加日志,要求在刚进入方法和结束方法的时候打印开始和结束日志;进入方法好说,直接在第一行打印即可,但是return的时候可能会有很多分支,所以我们必须要在更上一层进行处理
实现步骤:
1.创建TestRequestBodyAdvice并实现RequestBodyAdvice接口,类上添加注解@ControllerAdvice
2.创建TestResponseBodyAdvice并实现ResponseBodyAdvice接口,类上添加注解@ControllerAdvice
3.修改其中的默认返回值;例如null改为对应值,false改为true(这个不要盲目的改)
4.引入logger类
5.在关键位置写入要打印的日志
具体代码如下:
@ControllerAdvice public class TestRequestBodyAdvice implements RequestBodyAdvice { private Logger logger= LoggerFactory.getLogger(TestRequestBodyAdvice.class); public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) { return true; } public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException { return httpInputMessage; } public Object afterBodyRead(Object object, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) { logger.debug("即将进入{}方法",methodParameter.getMethod().getName()); return object; } public Object handleEmptyBody(Object object, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) { return object; } } @ControllerAdvice public class TestResponseBodyAdvice implements ResponseBodyAdvice { private Logger logger= LoggerFactory.getLogger(TestResponseBodyAdvice.class); public boolean supports(MethodParameter methodParameter, Class aClass) { if (aClass.isAssignableFrom(MappingJackson2CborHttpMessageConverter.class)){ return true; } return false; } public Object beforeBodyWrite(Object object, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) { logger.debug("即将进入{}方法",methodParameter.getMethod().getName()); return object; } }
* 以上两个类并非只是打印日志的作用,他可以在所有请求的进入和返回时刻进行处理,例如进行简单的参数校验,或者查询为空时在此处理给前端一个默认返回值等 * 以上代码采用了适配器模式,通过supports函数来判断是否进入下面的函数进行逻辑处理