java中的数据库查询流程
controller
@RequestMapping(value = "/list")
@ResponseBody
@ApiOperation("社区列表")
public ApiResponse<List<CommunityZone>> list() {
return ApiResponseUtils.success(zoneService.getAll());
}
service
List<CommunityZone> getAll();
ServiceImpl
@Override
public List<CommunityZone> getAll() {
List<CommunityZone> list = communityMapper.allAndVillageNum();
return list;
}
Mapper
package com.smart.mapper.userExtendInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.model.userExtendInfo.Community;
import com.smart.model.userExtendInfo.pojo.CommunityZone;
import java.util.List;
/**
* <p>
* 社区基础信息表 Mapper 接口
* </p>
*/
public interface CommunityMapper extends BaseMapper<Community> {
/**
* 社区列表
* @return
*/
List<CommunityZone> allAndVillageNum();
Integer count();
}
pojo,model
package com.smart.model.userExtendInfo.pojo;
import com.smart.model.userExtendInfo.Community;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
@Api("社区查询结果集")
public class CommunityZone extends Community {
@ApiModelProperty("下辖小区数量")
private Integer villiageNumbers;
public Integer getVilliageNumbers() {
return villiageNumbers;
}
public void setVilliageNumbers(Integer villiageNumbers) {
this.villiageNumbers = villiageNumbers;
}
}
package com.smart.model.userExtendInfo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.Date;
import java.util.Date;
/**
* <p>
* 社区基础信息表
* </p>
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Getter
@Setter
@Accessors(chain = true)
public class Community implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String name;
private Date createTime;
private Integer creatorId;
/**
* 范围
*/
private String limits;
private String address;
private String phone;
public static long getSerialVersionUID() {
return serialVersionUID;
}
}
mapper.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.smart.mapper.userExtendInfo.CommunityMapper">
<select id="allAndVillageNum" resultType="com.smart.model.userExtendInfo.pojo.CommunityZone">
SELECT a.*,count(b.id) villiage_numbers from community a
LEFT JOIN village b on a.id=b.community_id
GROUP BY a.id
</select>
</mapper>
逐层向下,环环相扣。