springboot +mybatis 实现一对多,多对一,多对多 【注解版】

MyBatis中的一对多 ,多对一,多对多【注解】

以下的例子就是针对 前端框架中所实现的 省-市-区  选择框的数据接口
前端需要的接口类型:

 后台代码:

dao层:
ProvinceDAO

/**
 *
 * @author 邬志斌
 * 2018年9月5日 下午2:51:13
 */

@Mapper
public interface ProvinceDAO {

    @Select("select ProvinceID,ProvinceName from trade_shops.`s_province_table`")
    @Results({
         @Result(property = "value", column = "ProvinceID"),
         @Result(property = "label", column = "ProvinceName"),
         @Result(property = "children", column = "ProvinceID",
                 many = @Many(select = "com.wzb.dao.CityDAO.listCity"))
    })
    List<SProvinceDO> listProvince();
  }

CityDAO

/**
 *
 * @author 邬志斌
 * 2018年9月5日 下午2:51:13
 */

@Mapper
public interface CityDAO {
    
    
    @Select("select cityid,cityname from trade_shops.s_city_table where provinceid=#{provinceid}")
    @Results({
         @Result(property = "value", column = "cityid"),
         @Result(property = "label", column = "cityname"),
         @Result(property = "children", column = "cityid",
                many = @Many(select = "com.wzb.dao.AreaDAO.listArea"))
    })
    List<Map<String, Object>> listCity(@Param("provinceid")Integer provinceid);

AreaDAO
 

/**
 *
 * @author 邬志斌
 * 2018年9月5日 下午2:51:13
 */
@Mapper
public interface AreaDAO {
    
    @Select("select id,area_name from trade_shops.s_area_table where cityid=#{cityid}")
    @Results({
         @Result(property = "value", column = "id"),
         @Result(property = "label", column = "area_name")
        
        
    })
    List<Map<String, Object>> listArea(@Param("cityid")Integer cityid);
}

model层:

SProvinceDO

/**
 *
 * @author 邬志斌
 * 2018年9月5日 下午2:53:59
 */
public class SProvinceDO {
    
    private Integer value;
    
    private String label;
    // 一对多
    private List<SCityDO> children;

    省略 get/set

 

SCityDO

/**
 *
 * @author 邬志斌
 * 2018年9月5日 下午2:55:49
 */
public class SCityDO {
    
    private Integer value;
    
    private String label;
    
    private Integer provinceid;
    // 一对多
    private List<SAreaDO> children;

省去 get/set

SAreaDO

/**
 *
 * @author 邬志斌
 * 2018年9月5日 下午2:57:07
 */
public class SAreaDO {
    
    private Integer value;
    
    private String label;
    
    private Integer cityid;

省去 get/set

service 基本上就是直接接收就可以 没有什么难点,现在一对多基本上完成。。

mysql 数据库 全国省市区 下载地址:https://download.csdn.net/download/qq_38941455/10647358

猜你喜欢

转载自blog.csdn.net/qq_38941455/article/details/82423832