zoukankan      html  css  js  c++  java
  • Java分页、Json工具类

     1.JSON数据转换工具类

    import java.util.List;
    import java.util.Map;
    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    import com.google.gson.reflect.TypeToken;
    
    /**
     * JSON数据互转工具类
     */
    public class JsonUtils {
        
        static Gson gson = new  GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        
        /** 将list转化为json字符串
         */
        public static String list2json(List<?> list) {
            if(list==null) return "[]";
            String str = gson.toJson(list);
            return str;
        }
        
        /** 将对象转化为json字符串(类 或者 Map)
         */
        public static String obj2json(Object obj) {
            if(obj==null) return "{}";
            String str = gson.toJson(obj);
            return str;
        }
    
        /** 将json字符串转化为List<String>
         */
        public static List<String> json2list(String jsonStr) {
            try{
                List<String> list = gson.fromJson(jsonStr, new TypeToken<List<String>>() {
                }.getType());
                return list;
            }catch(Exception e){
                //e.printStackTrace();
            }
            return null;
        }
        
        /** 将json字符串转化为List<Integer>
         */
        public static List<Integer> json2listInt(String jsonStr) {
            try{
                List<Integer> list = gson.fromJson(jsonStr, new TypeToken<List<Integer>>() {
                }.getType());
                return list;
            }catch(Exception e){
                //e.printStackTrace();
            }
            return null;
        }
        
        /** 将json字符串转化为List<Map<Object,Object>>
         */
        public static List<Map<Object,Object>> json2listMap(String jsonStr) {
            try{
                List<Map<Object,Object>> list = gson.fromJson(jsonStr, new TypeToken<List<Map<Object,Object>>>() {
                }.getType());
                return list;
            }catch(Exception e){
                e.printStackTrace();
            }
            return null;
        }
        /** 将json字符串转化为List<Map<String,Object>>
         */
        public static List<Map<String,Object>> json2listMapStrObj(String jsonStr) {
            try{
                List<Map<String,Object>> list = gson.fromJson(jsonStr, new TypeToken<List<Map<String,Object>>>() {
                }.getType());
                return list;
            }catch(Exception e){
                e.printStackTrace();
            }
            return null;
        }
        /** 将json字符串转化为List<Map<String,String>>
         */
        public static List<Map<String,String>> json2listMapStr(String jsonStr) {
            try{
                List<Map<String,String>> list = gson.fromJson(jsonStr, new TypeToken<List<Map<String,String>>>() {
                }.getType());
                return list;
            }catch(Exception e){
                e.printStackTrace();
            }
            return null;
        }
        /** 将json字符串转化为List<Object>
         */
        public static List<Object> json2listObj(String jsonStr) {
            try{
                List<Object> list = gson.fromJson(jsonStr, new TypeToken<List<Object>>() {
                }.getType());
                return list;
            }catch(Exception e){
                //e.printStackTrace();
            }
            return null;
        }
        
        /** 将json字符串转化为List<List<String>
         */
        public static List<List<String>> json2list2String(String jsonStr) {
            try{
                List<List<String>> list = gson.fromJson(jsonStr, new TypeToken<List<List<String>>>() {
                }.getType());
                return list;
            }catch(Exception e){
                //e.printStackTrace();
            }
            return null;
        }
        
        /** 将json字符串转化为Map<String,Object>
         */
        public static Map<String,Object> json2Map(String jsonStr) {
            try{
                Map<String,Object> map = gson.fromJson(jsonStr, new TypeToken<Map<String,Object>>() {
                }.getType());
                return map;
            }catch(Exception e){
                //e.printStackTrace();
            }
            return null;
        }
        
        /** 将json字符串转化为Map<String,String>
         */
        public static Map<String,String> json2MapSingleStr(String jsonStr) {
            try{
                Map<String,String> map = gson.fromJson(jsonStr, new TypeToken<Map<String,String>>() {
                }.getType());
                return map;
            }catch(Exception e){
                //e.printStackTrace();
            }
            return null;
        }
        
        /** 将json字符串转化为Map<String,Double>
         */
        public static Map<String,Double> json2MapSingleDou(String jsonStr) {
            try{
                Map<String,Double> map = gson.fromJson(jsonStr, new TypeToken<Map<String,Double>>() {
                }.getType());
                return map;
            }catch(Exception e){
                //e.printStackTrace();
            }
            return null;
        }
        
        /** 将json字符串转化为Map<String,Map<String,String>>
         */
        public static Map<String,Map<String,String>> json2MapInMapStr(String jsonStr) {
            try{
                Map<String,Map<String,String>> map = gson.fromJson(jsonStr, new TypeToken<Map<String,Map<String,String>>>() {
                }.getType());
                return map;
            }catch(Exception e){
                //e.printStackTrace();
            }
            return null;
        }
    
        /** 将json字符串转化为Map<String,Map<String,Float>>
         */
        public static Map<String,Map<String,Float>> json2MapInMapFloat(String jsonStr) {
            try{
                Map<String,Map<String,Float>> map = gson.fromJson(jsonStr, new TypeToken<Map<String,Map<String,Float>>>() {
                }.getType());
                return map;
            }catch(Exception e){
                //e.printStackTrace();
            }
            return null;
        }
        /** 将json字符串转化为Map<String,List<Integer>
         */
        public static Map<String,List<Integer>> json2MapInListInt(String jsonStr) {
            try{
                Map<String,List<Integer>> map = gson.fromJson(jsonStr, new TypeToken<Map<String,List<Integer>>>() {
                }.getType());
                return map;
            }catch(Exception e){
                //e.printStackTrace();
            }
            return null;
        }
        /** 将json字符串转化为Map<String,List<String>
         */
        public static Map<String,List<String>> json2MapInListString(String jsonStr) {
            try{
                Map<String,List<String>> map = gson.fromJson(jsonStr, new TypeToken<Map<String,List<String>>>() {
                }.getType());
                return map;
            }catch(Exception e){
                //e.printStackTrace();
            }
            return null;
        }
        /** 将json字符串转化为Map<String,List<List<String>>
         */
        public static Map<String,List<List<String>>> json2MapInListStringListString(String jsonStr) {
            try{
                Map<String,List<List<String>>> map = gson.fromJson(jsonStr, new TypeToken<Map<String,List<List<String>>>>() {
                }.getType());
                return map;
            }catch(Exception e){
                //e.printStackTrace();
            }
            return null;
        }
        /** 将json字符串转化为Map<String,List<Integer>
         */
        public static Map<Integer,List<Integer>> json2MapInListInt_int(String jsonStr) {
            try{
                Map<Integer,List<Integer>> map = gson.fromJson(jsonStr, new TypeToken<Map<Integer,List<Integer>>>() {
                }.getType());
                return map;
            }catch(Exception e){
                //e.printStackTrace();
            }
            return null;
        }
        /** 将json字符串转化为List<String[]>
         */
        public static List<String[]> json2ListStrArr(String jsonStr) {
            try{
                List<String[]> list = gson.fromJson(jsonStr, new TypeToken<List<String[]>>() {
                }.getType());
                return list;
            }catch(Exception e){
                //e.printStackTrace();
            }
            return null;
        }
        
        /** 将json字符串转化为List<Float[]>
         */
        public static List<Float[]> json2ListFloatArr(String jsonStr) {
            try{
                List<Float[]> list = gson.fromJson(jsonStr, new TypeToken<List<Float[]>>() {
                }.getType());
                return list;
            }catch(Exception e){
                //e.printStackTrace();
            }
            return null;
        }
        
        /** 将json字符串转化为List<Float>
         */
        public static List<Float> json2ListFloat(String jsonStr) {
            try{
                List<Float> list = gson.fromJson(jsonStr, new TypeToken<List<Float>>() {
                }.getType());
                return list;
            }catch(Exception e){
                //e.printStackTrace();
            }
            return null;
        }
        
        /** 将json字符串转化为AjaxDto
         */
        /*public static AjaxDto json2AjaxDto(String jsonStr) {
            AjaxDto map = gson.fromJson(jsonStr, new TypeToken<AjaxDto>() {
            }.getType());
            return map;
        }*/
        
        
    }
    View Code

      注:需要引入cosn.jar包。

    2.分页工具类

    /**分页工具类
     */
    public class Pager<T> {
        
        /** 换数据类型时使用
         */
        public Pager(Pager<?> pager,List<?> datas){
            this.currPage=pager.currPage;
            this.pages=pager.pages;
            this.pageSize=pager.pageSize;
            this.count=pager.count;
            this.ind=pager.ind;
            this.datas=(List<T>)datas;
        }
    
        public Pager(int currPage){
            this.currPage=currPage<1?1:currPage;
        }
        public Pager(int currPage,int pageSize){
            this.currPage=currPage<1?1:currPage;
            this.pageSize=pageSize;
        }
        
        
        private int currPage;//当前页     设置-100时,则不分页和获取页数(只在有需要的地方加此逻辑)
        private int pages;//总页数
        private int pageSize=10;//每页条数
        private int count;//总条数
        private int ind;
        private List<T> datas;
        private Object other;
        
        
        public Pager<T> setData(int count,List<T> datas){
            this.setCount(count);
            this.datas=datas;
            return this;
        }
        /** 不能确认每页条数才使用
         */ 
        public Pager<T> setDataLikeCount(int pages,List<T> datas){
            this.pages=pages;
            this.datas=datas;
            return this;
        }
        public List<T> getData(){
            return this.datas;
        }
        
        private void setCount(int count) {
            this.count = count;
            this.ind = ((currPage-1)*pageSize)+1;
            this.pages = count==0?1:((int)(count/pageSize+(count%pageSize==0?0:1)));
        }
        public int getCount(){
            return count;
        }
        /** 获取每页条数
         */
        public int getPageSize(){
            return this.pageSize;
        }
        
        public int getStartCount(){
            return (currPage - 1) * pageSize;
        }
        public Object getOther() {
            return other;
        }
        public void setOther(Object other) {
            this.other = other;
        }
        public String toJson(){
            return JsonUtils.obj2json(this);
        }
        public static Pager<Object> getBlankDatas(){
            Pager<Object> pager = new Pager<Object>(1);
            pager.setData(0,new ArrayList<Object>(0));
            return pager;
        }
    }
    View Code

       注:该类中引用了JSON数据转换工具类,可以改成自己需要的,也可以删除。

    3.接口返回工具类

    public class AjaxDto {
        
        public AjaxDto(){}
        
        public AjaxDto(int result){
            this.result=result;
        }
        
        public AjaxDto(int result, String msg, Object obj) {
            this.result = result;
            this.msg = msg;
            this.obj = obj;
        }
    
        public AjaxDto(int result, String msg) {
            this.result = result;
            this.msg = msg;
        }
        
        public AjaxDto(int result, String msg,HttpServletRequest request) {
            this.result = result;
            this.msg = msg;
            this.request=request;
        }
        
        public AjaxDto(int result, String msg, Object obj,HttpServletRequest request) {
            this.result = result;
            this.msg = msg;
            this.obj = obj;
            this.request=request;
        }
    
        /**
         * 0表示失败 1表示成功
         */
        private int result;
        /**
         * 提示信息
         */
        private String msg;
        /**
         * 附加对象,用来存储一些特定的返回信息
         */
        protected Object obj;
        
        private transient HttpServletRequest request;
    
        public int getResult() {
            return result;
        }
    
        public void setResult(int result) {
            this.result = result;
        }
        
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
        public Object getObj() {
            return obj;
        }
        public void setObj(Object obj) {
            this.obj = obj;
        }
        public  HttpServletRequest getRequest() {
            return request;
        }
    
        public void setRequest(HttpServletRequest request) {
            this.request = request;
        }
    
        
        public String toJson(){
            if(request==null)
                return JsonUtils.obj2json(this);
            else
                return toJson(request);
        }
        public String toJson(HttpServletRequest request){
            return JsonUtils.obj2json(this);
        }
    }
    View Code

      注:该类中引用了JSON数据转换工具类,可以改成自己需要的,也可以删除。

  • 相关阅读:
    Spring5源码分析(011)——IoC篇之解析bean标签总览
    Spring5源码分析(010)——IoC篇之加载BeanDefinition:解析和注册BeanDefinition
    Spring5源码分析(009)——IoC篇之加载BeanDefinition:获取 Document 实例
    Spring Boot2(015):JSON
    Spring Boot2(014):国际化 Internationalization
    Spring Boot2(013):日志
    Spring Boot2(012):Profiles
    Spring Boot2(011):外部化配置 Externalized Configuration
    Spring5源码分析(008)——IoC篇之加载BeanDefinition:获取XML的验证模式
    ubuntu 16.04下ssh访问提示错误
  • 原文地址:https://www.cnblogs.com/bl123/p/13719776.html
Copyright © 2011-2022 走看看