一个非常简单的全国城市三级联动

废话不多说直接看图(注:数据库要有数据才可以的哦)
在这里插入图片描述

前端代码

====前端主要是Vue,elmentUI,和aioxs这几个技术。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <style>
        body{
            background-color: #E9EEF3;
        }
        .el-header, .el-footer {
            background-color: #B3C0D1;
            color: #333;
            text-align: center;
            line-height: 60px;
        }

        .el-aside {
            background-color: #D3DCE6;
            color: #333;
            text-align: center;
            line-height: 200px;
        }

        .el-main {
            background-color: #E9EEF3;
            color: #333;
            text-align: center;
            line-height: 40px;
        }

        body > .el-container {
            margin-bottom: 40px;
        }

        .el-container:nth-child(5) .el-aside,
        .el-container:nth-child(6) .el-aside {
            line-height: 260px;
        }

        .el-container:nth-child(7) .el-aside {
            line-height: 320px;
        }
    </style>
</head>

<body>
<div id="box">


    <el-container>
        <el-main>
            <template>
                <el-link type="primary">City:</el-link>
                <el-select v-model="value1"  filterable  placeholder=""
                           v-on:change="city">
                <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                </el-option>
            </el-select>

                <el-link type="success">Area:</el-link><el-select
                    v-model="value2"
                    filterable
                    collapse-tags
                    style="margin-left: 20px;"
                    placeholder=""  v-on:change="County">
                <el-option
                        v-for="item in options1"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                </el-option>
            </el-select>

                <el-link type="warning">County:</el-link> <el-select v-model="value3" filterable placeholder="">
                <el-option
                        v-for="item in options2"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                </el-option>
            </el-select>
            </template>

        </el-main>
    </el-container>

</div>
</body>
<script>

    new Vue({
        el: "#box",
        data:{
            url : "http://127.0.0.1:8080/eureka/",
            options: [],
            options1: [],
            options2: [],
            value1: [],
            value2: [],
            value3: []
        },
        mounted() {
            let that = this;
            axios.get(that.url + 'firstLevelCity')
                .then(function (response) {
                    that.options = response.data;
                })
                .catch(function (error) {
                    console.log(error.data);
                });
        },
        methods:{
            city(cityCode){
               let that = this;
              console.log(cityCode) ;
              axios.get(that.url + 'secondCity?cityCode=' + cityCode)
                  .then(function (response) {
                      console.log(response.data)
                      that.options1 = response.data;
              })
            },
            County(thirdCode){
                let that = this;
                console.log(thirdCode) ;
                axios.get(that.url + 'thirdCity?thirdCode=' + thirdCode)
                    .then(function (response) {
                        console.log(response.data)
                        that.options2 = response.data;
                    })
            }
        }

    });


</script>
</html>

连接数据库用到的一个工具类

public class MySqlUtil {

   private static final String url = "jdbc:mysql://10.56.39.48:3306/test";
   private static final String username = "";
   private static final String passwword = "";

   static {
       try {
           Class.forName("com.mysql.jdbc.Driver");
       } catch (ClassNotFoundException e) {
           e.printStackTrace();
       }
   }

    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, username, passwword);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
}

后台用的是springboot项目,没有service,直接用controller


@RestController
public class CityController {

    private static Connection connection;
    static {
         connection = MySqlUtil.getConnection();
    }
    @CrossOrigin("*")
    @RequestMapping(value = "firstLevelCity" , method = RequestMethod.GET)
    public Object firstLevel() throws SQLException {
        Console.log("请求开始。。。");
        String sql = " select * from base_province";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        List< Map<String,String>> list = new ArrayList<>();
        while (resultSet.next()) {
            Map<String,String> map = new HashMap<>();
            map.put("value", resultSet.getString("Code"));
            map.put("label", resultSet.getString("Name"));
            list.add(map);
        }
        return list;
    }


    @CrossOrigin("*")
    @RequestMapping(value = "secondCity" , method = RequestMethod.GET)
    public Object secondLevel(String cityCode) throws SQLException {
        Console.log("请求开始。。。");
        String sql = " select * from base_city where cityCode = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, cityCode);
        ResultSet resultSet = preparedStatement.executeQuery();
        //结果集
        List< Map<String,String>> list = new ArrayList<>();
        while (resultSet.next()) {
            Map<String,String> map = new HashMap<>();
            list.add(map);
            map.put("value", resultSet.getString("Code"));
            map.put("label", resultSet.getString("Name"));
        }
        return list;
    }
    @CrossOrigin("*")
    @RequestMapping(value = "thirdCity", method = RequestMethod.GET)
    public Object thirdLevel(String thirdCode) throws SQLException {
        Console.log("请求开始。。。");
        String sql ="select * from base_area where areaCode = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, thirdCode);
        ResultSet resultSet = preparedStatement.executeQuery();
        List<Map<String, String>> list = new ArrayList<>();
        while (resultSet.next()){
            Map<String, String> map= new HashMap<>();
            map.put("value", resultSet.getString("Code"));
            map.put("label", resultSet.getString("Name"));
            list.add(map);
        }
        return list;
    }

}

有问题可以随时交流,互相学习哦。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值