级联
级联
黄金码 发表于7个月前
级联
  • 发表于 7个月前
  • 阅读 15
  • 收藏 0
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

摘要: 用级联造个句:各年级联合行动,卫生大扫除。

级联,英语:Cascade。是计算机科学里比较常见的概念。通常是指多个对象按照一定的规则有级别地或者是有梯度地联接在一起,也就是按照树形结构有层次地把对象组织在一起。

 

■应用

级联的应用比较广泛,表现在很多方面。

功放,一个功放块或是三极管的放大效果达不到要求的功率,这时候就要用多片级连的方法来实现。

微机原理的8259(中断处理器)是可以级联在一起,从而能处理更多的中断(扩容效果)。

 

网络上的交换机,路由器级联。

级联一般用于扩展网段的设备接入数量和延长距离,如交换机和集线器就是最常见的级联设备。

 

通过这些应用是否对级联有了个初步的形象了呢。

 

在Web项目中也会经常使用到级联的。比如:

货物分类,组织体制结构,行政区划等。

这里以行政区划为例。

数据来源:http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/

■表设计

■前端

<!DOCTYPE html>

<html>

<head><title>级联</title>

<style>

.divinline {

display:inline;/* 删除Div的换行*/

}

</style>

</head>

<body>

<div id="area_0" class="divinline"></div>

<script>

window.onload = function() {

    createXmlHttp();

    xmlHttp.open("post", "http://192.168.11.32:8080/teapot/t/?_a=14", true);

    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded" );

    xmlHttp.onreadystatechange = callback;

    xmlHttp.send("owner=" + "area_0");

};

var xmlHttp ;



function createXmlHttp() {

    if(window.ActiveXObject) {

        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

    } else {

        xmlHttp = new XMLHttpRequest();

    }

}



function callback() {

    if(xmlHttp.readyState == 4) {

        if(xmlHttp.status == 200) {

            var json = JSON.parse(xmlHttp.responseText);

            // 得到的数据追加到谁的下面

            var owner = document.getElementById(json.owner);

            if ( json.options.length > 1 ) {

                var div1_old = document.getElementById(json.divid);

                if (div1_old != null) {

                    owner.removeChild(div1_old);

                }

                var div1 = document.createElement("div");

                div1.id=json.divid;

                div1.className="divinline";

                owner.appendChild(div1);

                var sel1 = document.createElement("select");

                for(var i = 0; i < json.options.length; i ++) {

                    var city = json.options[i];

                    var option = new Option(city.cdNm,city.cdVal);

                    sel1.options.add(option);

                };

                sel1.onchange = function() {

                    createXmlHttp();

                    var name = this.value;

                    if(name != "") {

                        xmlHttp.open("post", "http://192.168.11.32:8080/teapot/t/?_a=14", true);

                        xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded" );

                        xmlHttp.onreadystatechange = callback;

                        // 当前owner和值

                        xmlHttp.send("owner=" + json.divid + "&value=" + name);

                    };

                };

                div1.appendChild(sel1);

            }

        }else {

            alert("Ajax Error : " + xmlHttp.status);

        };

    }else {

    };

};

</script>

</body>

</html>

从上面的代码可以看出,html和css部分很简单。

在html里主要使用了div的嵌套。因为使用动态创建,主要处理都在Javascript里了。

Javascript中主要使用了这些计数。Ajax,Json,DOM。回调函数。

对旧的数据删除也是一个小点。

 

■后端

    public void action(HttpServletRequest req, HttpServletResponse resp)

            throws IOException {

        long st1a = System.currentTimeMillis();

        req.setCharacterEncoding("utf-8");

        String owner = (String) req.getAttribute("owner");

        String value = (String) req.getAttribute("value");

        if (value == null) {

            value = "";

        }



        String subDiv = owner;

        int idy = owner.indexOf("_");

        int idx = 0;

        if (idy >= 0) {

            idx = Integer.parseInt(owner.substring(idy + 1));

            subDiv = owner.substring(0, idy) + "_" + (idx + 1);

        } else {

            subDiv = subDiv + "_1";

        }



        String val2 = "";

        if (idx == 0) {

            val2 = "0000000000";

        }

        if (idx == 1) {

            val2 = "00000000";

        }

        if (idx == 2) {

            val2 = "000000";

        }

        if (idx == 3) {

            val2 = "000";

        }



        long ed1 = System.currentTimeMillis();

        log.info("★★★★action 1 耗时" + (ed1 - st1a));

        long st1 = System.currentTimeMillis();

        Connection conn = null;

        Statement stmt = null;

        ResultSet rs = null;

        List<TblCd> cds = new ArrayList<TblCd>();

        try {

            conn = TeapotDb.getConnection();

            stmt = conn.createStatement();

            String sqlCmd = "SELECT CD_VAL, CD_NM FROM tbl_cd WHERE CD_TYPE = 'adm_div' ";

            if (value != "" ) {

                sqlCmd = sqlCmd + "AND CD_VAL LIKE '" + value + "%' AND CD_VAL LIKE '%" + val2 + "' ";

            }

            else {

                sqlCmd = sqlCmd + " AND CD_VAL IN ( '110000000000', '120000000000', '130000000000', '140000000000', '150000000000', '210000000000', '220000000000', '230000000000', '310000000000', '320000000000', '330000000000', '340000000000', '350000000000', '360000000000', '370000000000', '410000000000', '420000000000', '430000000000', '440000000000', '450000000000', '460000000000', '500000000000', '510000000000', '520000000000', '530000000000', '540000000000', '610000000000', '620000000000', '630000000000', '640000000000', '650000000000' )";

            }

            sqlCmd = sqlCmd + " ORDER BY CD_VAL;";



            System.out.println("SQL:" + sqlCmd);

            rs = stmt.executeQuery(sqlCmd);

            System.out.println(sqlCmd);

            while (rs.next()) {

                TblCd e = new TblCd();

                if (idx == 0) {

                    e.setCdVal(rs.getString("CD_VAL").substring(0, 2));

                } else if (idx == 1) {

                    e.setCdVal(rs.getString("CD_VAL").substring(0, 4));

                } else  if (idx == 2) {

                    e.setCdVal(rs.getString("CD_VAL").substring(0, 6));

                } else  if (idx == 3) {

                    e.setCdVal(rs.getString("CD_VAL").substring(0, 9));

                } else {

                    e.setCdVal(rs.getString("CD_VAL"));

                }

                e.setCdNm(rs.getString("CD_NM"));

                cds.add(e);

            }

        } catch(Exception e) {

            e.printStackTrace();

            System.out.println(e);

        } finally {

            if (rs != null ) { try {rs.close(); } catch (SQLException e) {e.printStackTrace();} }

            if (stmt != null ) { try {stmt.close(); } catch (SQLException e) {e.printStackTrace();} }

            if (conn != null ) { try {TeapotDb.releaseConnection(conn); } catch (SQLException e) {e.printStackTrace();} }

        }

        ed1 = System.currentTimeMillis();

        log.info("★★★★action 数据库检索 耗时" + (ed1 - st1));

        st1 = System.currentTimeMillis();



        resp.setContentType("application/json;charset=UTF-8");

        resp.setHeader("Access-Control-Allow-Origin", "*");

        PrintWriter out = resp.getWriter();



        Gson gson = new Gson();

        CascadeMdl mdl = new CascadeMdl(owner, subDiv, cds);

        String jsonObject = gson.toJson(mdl);

        System.out.println(jsonObject);

        out.println(jsonObject);

        ed1 = System.currentTimeMillis();

        log.info("★★★★action 3 耗时" + (ed1 - st1) + "总耗时" + (ed1 - st1a));

    }

 

查询数据库,结果以json形式返回。

在获取省列表的时候,有些慢,主要是数据库检索的问题,参照性能测试。

 

■性能测试

在下面这句时表现不好。

SELECT CD_VAL, CD_NM FROM teapot.tbl_cd WHERE CD_TYPE = 'adm_div' AND CD_VAL LIKE '%0000000000' ORDER BY CD_VAL;

发现建表时没有指定主键,那么就先用大招一:为搜索字段建索引。

 

无PRIMARY第一次

SELECT CD_VAL, CD_NM FROM tbl_cd WHERE CD_TYPE = 'adm_div' AND CD_VAL LIKE '%0000000000' ORDER BY CD_VAL;

08:59:58.674 [http-nio-8080-exec-4] INFO  o.teapot.servlet.controller.Cascade - ★★★★action 数据库检索 耗时4083

 

无PRIMARY第二次

SELECT CD_VAL, CD_NM FROM tbl_cd WHERE CD_TYPE = 'adm_div' AND CD_VAL LIKE '%0000000000' ORDER BY CD_VAL;

09:05:37.980 [http-nio-8080-exec-2] INFO  o.teapot.servlet.controller.Cascade - ★★★★action 数据库检索 耗时1848

 

有PRIMARY

SELECT CD_VAL, CD_NM FROM tbl_cd WHERE CD_TYPE = 'adm_div' AND CD_VAL LIKE '%0000000000' ORDER BY CD_VAL;

10:13:58.131 [http-nio-8080-exec-10] INFO  o.teapot.servlet.controller.Cascade - ★★★★action 数据库检索 耗时12645

 

有PRIMARY

SELECT CD_VAL, CD_NM FROM tbl_cd WHERE CD_TYPE = 'adm_div' AND ( CD_VAL LIKE '0%' OR  CD_VAL LIKE '1%' OR  CD_VAL LIKE '2%' OR  CD_VAL LIKE '3%' OR  CD_VAL LIKE '4%' OR  CD_VAL LIKE '5%' OR  CD_VAL LIKE '6%' OR  CD_VAL LIKE '7%' OR  CD_VAL LIKE '8%' OR  CD_VAL LIKE '9%' )AND CD_VAL LIKE '%0000000000' ORDER BY CD_VAL;

10:17:37.137 [http-nio-8080-exec-4] INFO  o.teapot.servlet.controller.Cascade - ★★★★action 数据库检索 耗时9472

 

SELECT CD_VAL, CD_NM FROM tbl_cd WHERE CD_TYPE = 'adm_div'  AND CD_VAL IN ( '110000000000', '120000000000', '130000000000', '140000000000', '150000000000', '210000000000', '220000000000', '230000000000', '310000000000', '320000000000', '330000000000', '340000000000', '350000000000', '360000000000', '370000000000', '410000000000', '420000000000', '430000000000', '440000000000', '450000000000', '460000000000', '500000000000', '510000000000', '520000000000', '530000000000', '540000000000', '610000000000', '620000000000', '630000000000', '640000000000', '650000000000' , '710000000000', '810000000000', '820000000000' ) ORDER BY CD_VAL;

17:40:42.474 [http-nio-8080-exec-2] INFO  o.teapot.servlet.controller.Cascade - ★★★★action 数据库检索 耗时1428

 

SELECT CD_VAL, CD_NM FROM tbl_cd WHERE CD_TYPE = 'adm_div'  AND CD_VAL IN ( '110000000000', '120000000000', '130000000000', '140000000000', '150000000000', '210000000000', '220000000000', '230000000000', '310000000000', '320000000000', '330000000000', '340000000000', '350000000000', '360000000000', '370000000000', '410000000000', '420000000000', '430000000000', '440000000000', '450000000000', '460000000000', '500000000000', '510000000000', '520000000000', '530000000000', '540000000000', '610000000000', '620000000000', '630000000000', '640000000000', '650000000000' , '710000000000', '810000000000', '820000000000' ) ORDER BY CD_VAL;

17:41:13.834 [http-nio-8080-exec-8] INFO  o.teapot.servlet.controller.Cascade - ★★★★action 数据库检索 耗时26

还可以凑合,就这样吧。

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 123
博文 175
码字总数 281650
×
黄金码
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: