個人博客遷到新網址:http://www.sunny58.top 歡迎訪問
1、實現思路:首先通過ajax異步請求后台加載所有省會城市-->選擇省會城市添加change時間,傳遞對應的參數加載省會城市對應的市區,選擇市區根據市區編碼查詢對應的區縣信息
2、js加載所有省會城市代碼
1 var COMPONT = { 2 /** 3 * 查詢區域信息 4 * @param isUse 是否啟用(0:否 1:是) 5 * @param jb 級別(1:省份直轄市信息 ,2:市級信息,3:縣區街道信息) 6 * @param mj 是否末級(0:否, 1:是) 7 * @param jbbm 級別編碼 8 */ 9 queryAreaInfo : function(isUse,jb,mj,jbbm) { 10 $.singleReq({ 11 data: { 12 'reqUrl': 'areaInFo', 13 'isUse' : isUse, 14 'jb' : jb, 15 'mj' : mj, 16 'jbbm' : jbbm, 17 'reqMethod': 'queryAreaInfo' 18 }, 19 success: function(resData) { 20 if(resData.retCode == GLOBAL_INFO.SYS_SUCCESS) { 21 var proviceList = resData.retObj; 22 var provicetHtml = '<option value="">--省份--</option>'; 23 var citytHtml = '<option value="">--市區--</option>'; 24 var countrytHtml = '<option value="">--縣區--</option>'; 25 if((jb == 1) && ('' == jbbm)) {//初始化數據 26 $.each(proviceList, function(index,value) { 27 provicetHtml += '<option value="'+value.jbbm+'">'+value.regionName+'</option>'; 28 }); 29 $('#js-select-provice').append(provicetHtml); 30 $('#js-select-city').append(citytHtml); 31 $('#js-select-country').append(countrytHtml); 32 } 33 if((jb == 2) && ('' != jbbm)) {//選擇省份 34 $('#js-select-city').html(''); 35 $('#js-select-country').html(''); 36 $('#js-select-country').append(countrytHtml); 37 $.each(proviceList, function(index,value) { 38 citytHtml += '<option value="'+value.jbbm+'">'+value.regionName+'</option>'; 39 }); 40 $('#js-select-city').append(citytHtml); 41 } 42 43 if((jb == 3) && (null != jbbm)) {//選擇市區 44 $('#js-select-country').html(''); 45 $.each(proviceList, function(index,value) { 46 countrytHtml += '<option value="'+value.jbbm+'">'+value.regionName+'</option>'; 47 }); 48 $('#js-select-country').append(countrytHtml); 49 } 50 51 } else { 52 UOMPComp.showFailedDialog(resData.resMsg, ''); 53 }; 54 } 55 }); 56 } 57 58 }
3、調用后台的代碼
- Controller
1 import java.util.List; 2 import java.util.Map; 3 4 import org.apache.log4j.Logger; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Component; 7 8 import com.xwtech.uomp.base.action.handler.HandlerRequestContext; 9 import com.xwtech.uomp.base.action.handler.HandlerResult; 10 import com.xwtech.uomp.base.action.handler.IHandler; 11 import com.xwtech.uomp.base.constants.IResultCode; 12 import com.xwtech.uomp.base.pojo.area.AreaInFoBean; 13 import com.xwtech.uomp.base.service.area.IAreaInfoService; 14 import com.xwtech.uomp.base.util.BeanUtil; 15 16 /** 17 * 區域信息 18 * @author sunt 19 * @dade 2017年10月18日上午11:06:57 20 * @version v1.0 21 */ 22 @Component("H_areaInFo") 23 public class AreaInFoHandle implements IHandler { 24 25 /** 26 * 日志 27 */ 28 protected static final Logger logger = Logger.getLogger(AreaInFoHandle.class); 29 30 @Autowired 31 private IAreaInfoService areaInfoService; 32 33 /** 34 * 區域信息查詢 35 * @param context 36 * @return 37 */ 38 public HandlerResult queryAreaInfo(HandlerRequestContext context) { 39 HandlerResult result = HandlerResult.newInstance(); 40 try { 41 Map<String, Object> param = BeanUtil.getMapFromRequest(context.getRequest()); 42 43 List<AreaInFoBean> list = areaInfoService.queryAreaInfo(param); 44 result.setRetObj(list); 45 result.setRetCode(IResultCode.SYS_SUCCESS); 46 result.setResMsg("查詢區域信息成功!"); 47 } catch (Exception e) { 48 logger.error("查詢區域信息異常:" + e); 49 result.setRetCode(IResultCode.SYS_FAILED); 50 e.printStackTrace(); 51 } 52 return result; 53 } 54 55 }
- JavaBean實體
1 import java.io.Serializable; 2 /** 3 * 區域信息實體Bean 4 * @author sunt 5 * @dade 2017年10月18日上午10:15:05 6 * @version v1.0 7 */ 8 public class AreaInFoBean implements Serializable{ 9 10 /** 11 * serialVersionUID 12 */ 13 private static final long serialVersionUID = -5211792909578871430L; 14 15 /** 16 * 區域編碼 17 */ 18 private String regionNum; 19 20 /** 21 * 區域名稱 22 */ 23 private String regionName; 24 25 /** 26 * 是否啟用(0:否 1:是) 27 */ 28 private int isUse; 29 30 /** 31 * 級別編碼 32 */ 33 private String jbbm; 34 35 /** 36 * 級別(1:省份直轄市信息 ,2:市級信息,3:縣區街道信息) 37 */ 38 private int jb; 39 40 /** 41 * 是否末級(0:否, 1:是) 42 */ 43 private int mj; 44 45 public String getRegionNum() { 46 return regionNum; 47 } 48 49 public String getRegionName() { 50 return regionName; 51 } 52 53 public int getIsUse() { 54 return isUse; 55 } 56 57 public String getJbbm() { 58 return jbbm; 59 } 60 61 public int getJb() { 62 return jb; 63 } 64 65 public int getMj() { 66 return mj; 67 } 68 69 public void setRegionNum(String regionNum) { 70 this.regionNum = regionNum; 71 } 72 73 public void setRegionName(String regionName) { 74 this.regionName = regionName; 75 } 76 77 public void setIsUse(int isUse) { 78 this.isUse = isUse; 79 } 80 81 public void setJbbm(String jbbm) { 82 this.jbbm = jbbm; 83 } 84 85 public void setJb(int jb) { 86 this.jb = jb; 87 } 88 89 public void setMj(int mj) { 90 this.mj = mj; 91 } 92 93 }
- 業務接口及實現類
1 import java.util.List; 2 import java.util.Map; 3 4 import com.xwtech.uomp.base.pojo.area.AreaInFoBean; 5 6 /** 7 * 區域信息接口 8 * @author sunt 9 * @dade 2017年10月18日上午10:12:44 10 * @version v1.0 11 */ 12 public interface IAreaInfoService { 13 14 /** 15 * 根據條件查詢區域信息 16 * @param map 查詢條件 17 * @return 18 */ 19 List<AreaInFoBean> queryAreaInfo(Map<String, Object> map); 20 }
1 import java.util.List; 2 import java.util.Map; 3 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.stereotype.Service; 6 7 import com.xwtech.uomp.base.dao.area.AreaInfoMapper; 8 import com.xwtech.uomp.base.pojo.area.AreaInFoBean; 9 import com.xwtech.uomp.base.service.area.IAreaInfoService; 10 /** 11 * 區域信息實現類接口 12 * @author sunt 13 * @dade 2017年10月18日上午10:13:33 14 * @version v1.0 15 */ 16 @Service("areaInfoService") 17 public class AreaInfoServiceImpl implements IAreaInfoService { 18 19 @Autowired 20 private AreaInfoMapper areaInfoMapper; 21 22 @Override 23 public List<AreaInFoBean> queryAreaInfo(Map<String, Object> map) { 24 25 return areaInfoMapper.queryAreaInfo(map); 26 } 27 28 }
- mapper接口及映射文件
1 import java.util.List; 2 import java.util.Map; 3 4 import com.xwtech.uomp.base.pojo.area.AreaInFoBean; 5 6 /** 7 * 區域信息dao 8 * @author sunt 9 * @dade 2017年10月18日上午10:29:40 10 * @version v1.0 11 */ 12 public interface AreaInfoMapper { 13 14 List<AreaInFoBean> queryAreaInfo(Map<String, Object> map); 15 }
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper namespace="com.xwtech.uomp.base.dao.area.AreaInfoMapper" > 4 <resultMap id="BaseResultMap" type="com.xwtech.uomp.base.pojo.area.AreaInFoBean" > 5 <id column="F_REGION_NUM" property="regionNum" jdbcType="INTEGER" /> 6 <result column="F_REGION_NAME" property="regionName" jdbcType="VARCHAR" /> 7 <result column="F_IS_USE" property="isUse" jdbcType="INTEGER" /> 8 <result column="F_JBBM" property="jbbm" jdbcType="VARCHAR"/> 9 <result column="F_JB" property="jb" jdbcType="INTEGER" /> 10 <result column="F_MJ" property="mj" jdbcType="INTEGER" /> 11 </resultMap> 12 13 <sql id="Base_Column_List" > 14 F_REGION_NUM,F_REGION_NAME,F_IS_USE,F_JBBM,F_JB,F_MJ 15 </sql> 16 17 <!-- 區域信息查詢 --> 18 <select id="queryAreaInfo" parameterType="java.util.Map" resultMap="BaseResultMap"> 19 SELECT <include refid="Base_Column_List" /> FROM TB_DA_AREA T 20 <where> 21 <if test="isUse != null" > 22 AND T.F_IS_USE = #{isUse,jdbcType=INTEGER} 23 </if> 24 <if test="jb != null" > 25 AND T.F_JB = #{jb,jdbcType=INTEGER} 26 </if> 27 <if test="mj != null" > 28 AND T.F_MJ = #{mj,jdbcType=INTEGER} 29 </if> 30 <if test="jbbm != null" > 31 AND T.F_JBBM LIKE #{jbbm,jdbcType=VARCHAR} || '%' 32 </if> 33 </where> 34 ORDER BY T.F_JBBM 35 </select> 36 </mapper>
有問題的或者源碼報錯的郵件與博主取得聯系!
- 運行效果截圖