Spring-SpringMVC-Mybatis框架下的 省市区 三级联动

    初学者学习SSM框架,若有问题,请留言.

    首先看一下项目结构.

    

    配置文件Web.xml,  spring-dao.xml,   springmvc-servlet.xml,  configuration.properties 等就是常规没有特殊的,具体内容可以看我上一篇博客.

    首先看一下Mysql的表

         

       省市区三张表,市通过p_id与省相连,区通过c_id与市相连.

    通过Mybatis代码自动生成工具自动生成dao层和entity层.也就是     

    我们要做的是   在Province(后简称P)类中建立一个List<city>字段,用来储存对应的City对象,然后再City(后简称C)类中建立一个List<area>字段用来储存对应的Area(后简称A)对象. 

    

    

    P类,C类,A类属于接口,所以只需要定义方法即可. 具体如何实现,我们要在对应的Mapper.xml文件中写出来.

    这样,只要通过数据库查出所有的P,也就同时能够查出City和Area.

    在PMapper.java中建立 搜索所有P 的方法,  在CMapper.java中建立 根据pid搜索 的方法,  在AMapper.java中建立 根据cid搜索 的方法.如下图:

    

    

在P.xml中 添加这样的方法

 <!-- 返回里面有集合的话,就需要添加<collection> -->
  <resultMap id="BaseResultMapProvinceAll" type="com.test.entity.Province" >
    <id column="province_id" property="provinceId" jdbcType="INTEGER" />
    <result column="province_name" property="provinceName" jdbcType="VARCHAR" />
  	<!-- property为对应的字段名,ofType为对应集合存储对象实体类的全限定名 -->
  	<!-- column为对应的下一级搜索的参数列名 -->
  	<!-- select为对应的下一级搜索的<select>的id,这样做就能相当于嵌套搜索 -->
  	<!-- collection中的内容为集合中实体类所对应的表中列名与实体类对应的关系 -->
  	<collection property="citys" ofType="com.test.entity.City" column="province_id" select="com.test.dao.CityMapper.selectByPrivinceId" >
		 <id column="city_id" property="cityId"  />
		 <result column="city_name" property="cityName"  />
		 <result column="province_id" property="provinceId"  />
  	</collection>
  </resultMap>
  <!-- 因为P类中有个字段类型是集合,且对应的xml也有方法返回时集合,所以需要重写resultMap -->
  <select id="selectProvinceAll" resultMap="BaseResultMapProvinceAll"  >
    select 
    <!-- <include/>属于是该表的所有字段,自动生成的,如果需要,我们可以修改 -->
    <include refid="Base_Column_List" />
    from provincedb
  </select>

 

在C.xml文件中添加方法:

 

 <resultMap id="BaseResultMapCity" type="com.test.entity.City" >
    <id column="city_id" property="cityId" jdbcType="INTEGER" />
    <result column="city_name" property="cityName" jdbcType="VARCHAR" />
    <result column="province_id" property="provinceId" jdbcType="INTEGER" />
    
    <collection property="areas" ofType="com.test.entity.Area" column="city_id" select="com.test.dao.AreaMapper.selectByCityId" >
		 <id column="area_id" property="areaId" />
   		 <result column="area_name" property="areaName"  />
   		 <result column="city_id" property="cityId"  />
  	</collection>
  </resultMap>
  
  <select id="selectByPrivinceId" resultMap="BaseResultMapCity" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from citydb
    where province_id = #{privince_id,jdbcType=INTEGER}
  </select>

在A.xml文件中添加方法:

 
  <select id="selectByCityId" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from areadb
    where city_id = #{city_id,jdbcType=INTEGER}
  </select>

这样,只要我们一搜索省份所有的信息,就能得到所对应的市区数据.

下面我们建立测试方法在controller层和service层的内容,以及前端

Controller层:

package com.test.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.test.entity.Province;
import com.test.service.IndexServcie;

@Controller
@RequestMapping("/con")
public class IndexController {	
	@Autowired
	private IndexServcie service;

	@RequestMapping("/get")
	public String getPCA(HttpServletRequest req) {
		List<Province> list = service.getPCAs();
		req.getSession().setAttribute("list", list);
		return "wel";
	}
}

service层:

package com.test.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.test.dao.ProvinceMapper;
import com.test.entity.Province;

@Service
public class IndexServcie {
	@Autowired
	private ProvinceMapper dao;

	public List<Province> getPCAs() {
		return dao.selectProvinceAll();
	}
}

哦,对了.我这里是以一个空白页的jsp为起点,跳入到后台,后台获取数据后,在跳入另一个jsp页面中.

起始页index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript" src="jquery-3.3.1.min.js"></script>
<body>

</body>
<script type="text/javascript">
	$(function(){
		window.location.replace("con/get");
	})
</script>
</html>

获取数据后的跳转的页面 wel.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" 
           uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<div>
		<dl>
			<c:forEach items="${list }" var="temp">
				<dt>${temp.provinceName }</dt>
				<c:forEach items="${temp.citys }" var="temp2">
					<dd>${temp2.cityName }
						<c:forEach items="${temp2.areas }" var="temp3">
						    <p>&nbsp;&nbsp;&nbsp;&nbsp;${temp3.areaName }</p>
						</c:forEach>
					</dd>
				</c:forEach>
			</c:forEach>
		</dl>
	</div>
</body>
</html>

效果如下:

 

测试结果还可以,至少是数据取回来了.接下来我们就根据具体的要求来处理数据.

这个方法不仅仅可以做 省市区三级联动  也可以做电商平台的商品类别之类的.

类似于这样的

 

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
这个网上书城项目采用了当前最流行的框架spring-springmvc-mybatis设计。这个项目旨在提供一个方便、快捷的购书平台,用户可以通过该平台浏览、搜索并购买自己喜欢的图书。 在这个项目中,我们使用了Spring作为项目的核心框架,它提供了依赖注入和面向切面编程等功能,使得项目的开发更加简洁和易于维护。Spring MVC作为项目的Web框架,负责将用户的请求路由到对应的Controller,并负责视图的渲染和返回。而MyBatis则是作为持久层框架,将数据库的操作和Java代码的实现解耦,提供了灵活的SQL映射和缓存机制。 在这个网上书城项目中,我们设计了一套完整的功能模块:用户管理模块、图书管理模块、订单管理模块等。用户可以通过注册、登录等功能来进行用户管理,并可以在平台上对图书进行购买、收藏等操作。同时,平台还提供了搜索功能,用户可以根据图书的名称、作者等进行快速查找。 这个项目的设计更加便于扩展和维护,使用了分层架构,将业务逻辑、持久层和展示层进行了有效的分离。同时,也采用了面向接口编程的思想,降低了模块之间的耦合度,提高了代码的复用性和可测试性。 总之,这个网上书城项目基于spring-springmvc-mybatis框架的设计,充分利用了各自的优势,提供了一个高效、稳定和易于维护的购书平台。期望能为用户提供良好的购书体验,并为图书销售行业的发展做出贡献。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值