省市区三级联动代码

sql语句在上一篇文章中,需要的童鞋可以自行领取。
首先是jsp页面:

省:
	

上面是jsp,剩下的是jquery,这个可以自己放到js中,建一个类似工具类的js,需要直接调用就Ok

$(function(){
	$.post(
		"findProvince",{},function(obj){
			for(var i in obj){
				$("[name='provinceid']").append("");
			}
		},"json"
	);
})
function findCity(provinceid){
	$.post(
		"findCity",{father:provinceid},function(obj){
			$("#but1").empty();
			$("#but1").append("市:");
			for ( var i in obj) {
				$("[name='cityid']").append("");
			}
		},"json"	
	);
}
function findArea(father){
	$.post(
		"findArea",{father:father},function(obj){
			$("#but2").empty();
			$("#but2").append("区:");
			for ( var i in obj) {
				$("[name='areaid']").append("");
			}
		},"json"	
	);
	
}

controller层:

package com.lf.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lf.mapper.AreaMapper;
import com.lf.mapper.CityMapper;
import com.lf.mapper.ProvinceMapper;
import com.lf.pojo.Area;
import com.lf.pojo.City;
import com.lf.pojo.Province;

@Controller
public class CityController {

	@Autowired
	ProvinceMapper p;
	
	@Autowired
	CityMapper c;
	
	@Autowired
	AreaMapper a;
	
		@RequestMapping("findProvince")
		@ResponseBody
		public List findProvince(){
			return p.findProvince();
		}
		
		@RequestMapping("findCity")
		@ResponseBody
		public List findCity(String father){
			return c.findCity(father);
		}
		
		@RequestMapping("findArea")
		@ResponseBody
		public List findArea(String father){
			return a.findArea(father);
		}
}

下面是mapper层:
AreaMapper:

package com.lf.mapper;

import com.lf.pojo.Area;
import com.lf.pojo.AreaExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

public interface AreaMapper {
    int countByExample(AreaExample example);

    int deleteByExample(AreaExample example);

    int deleteByPrimaryKey(Integer id);

    int insert(Area record);

    int insertSelective(Area record);

    List selectByExample(AreaExample example);

    Area selectByPrimaryKey(Integer id);

    int updateByExampleSelective(@Param("record") Area record, @Param("example") AreaExample example);

    int updateByExample(@Param("record") Area record, @Param("example") AreaExample example);

    int updateByPrimaryKeySelective(Area record);

    int updateByPrimaryKey(Area record);

    @Select("select * from hat_area where father=#{father}")
	List findArea(@Param("father")String father);
}

CityMapper:

package com.lf.mapper;

import com.lf.pojo.City;
import com.lf.pojo.CityExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

public interface CityMapper {
    int countByExample(CityExample example);

    int deleteByExample(CityExample example);

    int deleteByPrimaryKey(Integer id);

    int insert(City record);

    int insertSelective(City record);

    List selectByExample(CityExample example);

    City selectByPrimaryKey(Integer id);

    int updateByExampleSelective(@Param("record") City record, @Param("example") CityExample example);

    int updateByExample(@Param("record") City record, @Param("example") CityExample example);

    int updateByPrimaryKeySelective(City record);

    int updateByPrimaryKey(City record);

    @Select("select * from hat_city where father=#{father}")
	List findCity(@Param("father")String father);
}

ProvinceMapper:

package com.lf.mapper;

import com.lf.pojo.Province;
import com.lf.pojo.ProvinceExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

public interface ProvinceMapper {
    int countByExample(ProvinceExample example);

    int deleteByExample(ProvinceExample example);

    int deleteByPrimaryKey(Integer id);

    int insert(Province record);

    int insertSelective(Province record);

    List selectByExample(ProvinceExample example);

    Province selectByPrimaryKey(Integer id);

    int updateByExampleSelective(@Param("record") Province record, @Param("example") ProvinceExample example);

    int updateByExample(@Param("record") Province record, @Param("example") ProvinceExample example);

    int updateByPrimaryKeySelective(Province record);

    int updateByPrimaryKey(Province record);

    @Select("select * from hat_province")
	List findProvince();
}

下面是实体类:
Area:

package com.lf.pojo;

public class Area {
    private Integer id;

    private String areaid;

    private String area;

    private String father;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getAreaid() {
        return areaid;
    }

    public void setAreaid(String areaid) {
        this.areaid = areaid == null ? null : areaid.trim();
    }

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area == null ? null : area.trim();
    }

    public String getFather() {
        return father;
    }

    public void setFather(String father) {
        this.father = father == null ? null : father.trim();
    }
}

City:

package com.lf.pojo;

public class City {
    private Integer id;

    private String cityid;

    private String city;

    private String father;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCityid() {
        return cityid;
    }

    public void setCityid(String cityid) {
        this.cityid = cityid == null ? null : cityid.trim();
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city == null ? null : city.trim();
    }

    public String getFather() {
        return father;
    }

    public void setFather(String father) {
        this.father = father == null ? null : father.trim();
    }
}

Province:

package com.lf.pojo;

public class Province {
    private Integer id;

    private String provinceid;

    private String province;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getProvinceid() {
        return provinceid;
    }

    public void setProvinceid(String provinceid) {
        this.provinceid = provinceid == null ? null : provinceid.trim();
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province == null ? null : province.trim();
    }
}

你可能感兴趣的:(省市区三级联动代码)