数据库三级分类如何用SSM的Controller+Service+mapper.xml+jsp实现 三级联动下拉框
TestMapper.xml
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sdwu.mapper.TestMapper">
<resultMap id="BaseResultMap" type="com.sd.pojo.Category">
<result column="CTG_ID" property="ctg_id"/>
<result column="PRODUCT_CATALOG_CODE_1" property="ctg_code1"/>
<result column="PRODUCT_CATALOG_NAME_1" property="ctg_name1"/>
<result column="PRODUCT_CATALOG_CODE_2" property="ctg_code2"/>
<result column="PRODUCT_CATALOG_NAME_2" property="ctg_name2"/>
<result column="PRODUCT_CATALOG_CODE_3" property="ctg_code3"/>
<result column="PRODUCT_CATALOG_NAME_3" property="ctg_name3"/>
</resultMap>
<select id="findProvince" resultType="Category">
select distinct PRODUCT_CATALOG_NAME_1 from AMS_CATEGORY
</select>
<select id="findCityByName" parameterType="String" resultType="Category">
select distinct PRODUCT_CATALOG_NAME_2 from AMS_CATEGORY where PRODUCT_CATALOG_NAME_1=#{ctg_name1}
</select>
<select id="findCountyByName" parameterType="String" resultType="Category">
select distinct PRODUCT_CATALOG_NAME_3 from AMS_CATEGORY where PRODUCT_CATALOG_NAME_2 =#{ctg_name2}
</select>
</mapper>
TestMapper
import com.sd.pojo.Category;
import java.util.List;
public interface TestMapper {
public List<Category> findProvince();
public List<Category> findCityByName(String pr_name);
public List<Category> findCountyByName(String ci_name);
}
TestService
import com.sd.pojo.Category;
import javax.annotation.Resource;
import java.util.List;
@Resource
public interface TestService {
public List<Category> findProvince();
public List<Category> findCityByName(String pr_name);
public List<Category> findCountyByName(String ci_name);
}
TestServiceImpl
import com.sd.mapper.CategoryMapper;
import com.sd.mapper.TestMapper;
import com.sd.pojo.Category;
import com.sd.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service("testService")
@Transactional
public class TestServiceImpl implements TestService {
@Autowired
private TestMapper testMapper;
@Override
public List<Category> findProvince() {
return this.testMapper.findProvince();
}
@Override
public List<Category> findCityByName(String pr_name) {
// TODO Auto-generated method stub
return this.testMapper.findCityByName(pr_name);
}
@Override
public List<Category> findCountyByName(String ci_name) {
// TODO Auto-generated method stub
return this.testMapper.findCountyByName(ci_name);
}
}
TestController
import com.sd.pojo.Category;
import com.sd.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.io.IOException;
import java.util.List;
@Controller
public class TestController {
@Autowired
private TestService testService;
@RequestMapping("findProvince")
public String findProvince(Model model) {
List<Category> province = testService.findProvince();
model.addAttribute("province", province);
return "/test/test111.jsp";
}
@RequestMapping(value = "/testjson1/{pr_name}", method = RequestMethod.GET)
@ResponseBody
public Object testJson1(@PathVariable("pr_name") String pr_name, Model model) throws IOException {
List<Category> city = testService.findCityByName(pr_name);
return city;
}
@RequestMapping(value = "/testjson2/{ci_name}", method = RequestMethod.GET)
@ResponseBody
public Object testJson2(@PathVariable("ci_name") String ci_name, Model model) throws IOException {
List<Category> county = testService.findCountyByName(ci_name);
return county;
}
}
test111.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript"
src="../js/jquery-1.9.1.min.js">
</script>
<script type="text/javascript">
function testJson1(){
var pr_name = $("#province").val();
$("#city").html('<option value="">----请选择城市----</option>');
$.ajax({
url : "${pageContext.request.contextPath}/testjson1/"+pr_name,
type : "GET",
dataType : "json",
success : function(data){
if(data!=null){
$(data).each(function(index){
$("#city").append(
'<option value="'</span>+data[index].<span class="hljs-property">ci_name+'">'+data[index].ci_name+'</option>'
);
});
}
}
});
}
function testJson2(){
var ci_name = $("#city option:selected").val();
$("#county").html('<option value="">--请选择县城--</option>');
$.ajax({
url : "${pageContext.request.contextPath }/testjson2/"+ci_name,
type : "GET",
dataType : "json",
success : function(data){
if(data!=null){
$(data).each(function(index){
$("#county").append(
'<option value="'</span>+data[index].<span class="hljs-property">co_name+'">'+data[index].co_name+'</option>'
);
});
}
}
});
}
</script>
</head>
<body>
<select name="province" id="province">
<option value="">------请选择省份-----</option>
<c:forEach items="${province}" var="c1">
<option value="${c1.pr_name}" οnclick="testJson1()">${c1.pr_name}</option>
</c:forEach>
</select>
<select name="city" id="city" οnclick="testJson2()">
<option value="">------请选择城市-----</option>
</select>
<select name="county" id="county" >
<option value="">------请选择县城-----</option>
</select>
</body>
</html>
为什么页面显示不出来数据库里的数据?