springboot 1对多

本文介绍了SpringBoot中如何实现1对多的关联映射,包括在数据库表设计、Mapper文件配置以及业务操作中的具体实现步骤。通过示例代码展示了如何进行数据查询、插入和更新等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 小区Controller
 * <p>
 * Created by chenqh on 2021-03-26 16:25:01.
 */
@Controller
@Api(tags = "SubdistrictController", description = "小区配置")
@RequestMapping("/subdistrict")
public class SubdistrictController extends BaseController {
    @Autowired
    private SubdistrictService subdistrictService;

    @ApiOperation("添加小区")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@Validated @RequestBody Subdistrict param) {
        SysSubdistrict subdistrict = getSysSubdistrict(param, false);
        int count = subdistrictService.create(subdistrict);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改小区")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @Validated @RequestBody SubdistrictEnable param) {
        SysSubdistrict subdistrict = getSysSubdistrict(param, true);
        int count = subdistrictService.update(id, subdistrict);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除小区")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        int count = subdistrictService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("分页查询全部小区")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<SubdistrictVo>> list(
            @ApiParam("小区名称") @RequestParam(value = "subDisName", required = false) String subDisName,
            @ApiParam("片区名称") @RequestParam(value = "secName", required = false) String secName,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<SubdistrictVo> reasonList = subdistrictService.list(subDisName, secName, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(reasonList));
    }

    @ApiOperation("根据小区ID获取片区")
    @RequestMapping(value = "/getSectionsById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<BaseVo>> getSectionsById(
            @ApiParam("小区ID") @RequestParam("id") Long id) {
        return CommonResult.success(subdistrictService.getSectionsById(id));
    }

    @ApiOperation("获取所有小区")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<BaseVo>> listAll() {
        return CommonResult.success(subdistrictService.listAll());
    }

    /**
     * 获取小区信息
     *
     * @param param
     * @return
     */
    private SysSubdistrict getSysSubdistrict(SubdistrictBase param, boolean edit) {
        String userName = getUserName();
        SysSubdistrict subdistrict = new SysSubdistrict();
        if (edit) {//编辑
            SubdistrictEnable p = (SubdistrictEnable) param;
            subdistrict.setName(null);
            subdistrict.setSections(p.getSections().stream().map(e -> {
                BaseEnableVo enableVo = new BaseEnableVo();
                enableVo.setName(e.getName());
                enableVo.setIsEnable(e.getIsEnable());
                return enableVo;
            }).collect(Collectors.toList()));
        } else {
            Subdistrict p = (Subdistrict) param;
            subdistrict.setCreateTime(new Date());
            subdistrict.setCreateBy(userName);
            subdistrict.setName(p.getName());
            subdistrict.setSections(p.getSectionNames().stream().map(e -> {
                BaseEnableVo enableVo = new BaseEnableVo();
                enableVo.setName(e);
                return enableVo;
            }).collect(Collectors.toList()));
        }
        subdistrict.setDeleteStatus(false);
        subdistrict.setUpdateTime(new Date());
        subdistrict.setUpdateBy(userName);
        subdistrict.setDirector(param.getDirector());
        subdistrict.setPrincipal(param.getPrincipal());
        return subdistrict;
    }

}

 

import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 小区管理Service
 * Created by chenqh on 2021-03-26 16:25:01.
 */
public interface SubdistrictService {
    /**
     * 添加小区
     */
    @Transactional
    int create(SysSubdistrict subdistrict);

    /**
     * 修改小区
     */
    @Transactional
    int update(Long id, SysSubdistrict subdistrict);

    /**
     * 删除小区
     */
    @Transactional
    int delete(Long id);

    /**
     * 分页获取小区
     */
    List<SubdistrictVo> list(String subDisName, String secName, Integer pageSize, Integer pageNum);

    /**
     * 根据ID获取所有区域列表
     */
    List<BaseVo> getSectionsById(Long id);

    List<BaseVo> listAll();

}

 

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 小区Service实现类
 * Created by chenqh on 2021-03-26 16:25:01.
 */
@Service
public class SubdistrictServiceImpl implements SubdistrictService {
    @Autowired
    private SysSubdistrictMapper subdistrictMapper;

    @Autowired
    private SysSectionMapper sectionMapper;

    @Autowired
    private SysAreaMapper areaMapper;

    @Override
    public int create(SysSubdistrict subdistrict) {
        //验证名称不能重复
        SysSubdistrictExample example = new SysSubdistrictExample();
        example.createCriteria().andNameEqualTo(subdistrict.getName());
        List<SysSubdistrict> sysSubdistricts = subdistrictMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(sysSubdistricts)) {
            Asserts.fail("小区名称已存在!");
        }

        List<BaseEnableVo> sections = subdistrict.getSections();

        List<String> neams = sections.stream().map(e -> e.getName()).collect(Collectors.toList());
        Set<String> sNeams = sections.stream().map(e -> e.getName()).collect(Collectors.toSet());
        if (sNeams.size() != neams.size()) {
            Asserts.fail("片区名称重复,请核对数据!");
        }

        subdistrictMapper.insert(subdistrict);
        addOrUpdateSecionts(subdistrict, false);
        return 1;
    }

    @Override
    public int update(Long id, SysSubdistrict subdistrict) {
        subdistrict.setId(id);
        addOrUpdateSecionts(subdistrict, true);
        return subdistrictMapper.updateByPrimaryKeySelective(subdistrict);
    }

    /**
     * 添加/更新片区
     *
     * @param subdistrict
     */
    private void addOrUpdateSecionts(SysSubdistrict subdistrict, boolean edit) {
        List<BaseEnableVo> pSections = subdistrict.getSections();
        String userBy = subdistrict.getUpdateBy();
        Long id = subdistrict.getId();

        if (edit) {
            SysSectionExample example = new SysSectionExample();
            example.createCriteria().andSubdisIdEqualTo(id);

            List<SysSection> sections = sectionMapper.selectByExample(example);
            List<Long> secIds = sections.stream().map(e -> e.getId())
                    .collect(Collectors.toList());
            List<String> secNames = sections.stream().map(e -> e.getName())
                    .collect(Collectors.toList());

            //验证是否关联
            SysAreaExample areaExample = new SysAreaExample();
            areaExample.createCriteria().andSecIdIn(secIds);
            List<SysArea> sysAreas = areaMapper.selectByExample(areaExample);
            List<Long> relSelIds = sysAreas.stream().map(e -> e.getSecId())
                    .collect(Collectors.toList());
            List<String> relNames = sections.stream().filter(e -> relSelIds.contains(e.getId()))
                    .map(e -> e.getName()).collect(Collectors.toList());

            List<String> names = pSections.stream().map(e -> e.getName()).collect(Collectors.toList());
            List<String> relItems = relNames.stream().filter(e -> !names.contains(e)).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(relItems)) {
                Asserts.fail(relItems + "不能删除, 已关联巡查区域");
            }

            List<BaseEnableVo> editItems = pSections.stream().filter(e -> secNames.contains(e.getName())).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(editItems)) {
                sectionMapper.batchUpdateSelective(editItems.stream().map(e -> {
                    SysSection section = new SysSection();
                    section.setId(sections.stream().filter(o -> o.getName().equals(e.getName())).findFirst().get().getId());
                    section.setName(e.getName());
                    section.setDeleteStatus(e.getIsEnable());
                    section.setUpdateBy(userBy);
                    section.setUpdateTime(new Date());
                    return section;
                }).collect(Collectors.toList()));
            }

            List<BaseEnableVo> addItems = pSections.stream().filter(e -> !secNames.contains(e.getName())).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(addItems)) {
                pSections = addItems;
            } else {
                pSections.clear();
            }

            List<Long> delIds = sections.stream().filter(e -> !names.contains(e.getName())).map(e -> e.getId()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(delIds)) {
                example.clear();
                example.createCriteria().andIdIn(delIds);
                sectionMapper.deleteByExample(example);
            }

        }

        List<SysSection> sections = new ArrayList<>();
        pSections.forEach(d -> {
            SysSection section = new SysSection();
            section.setName(d.getName());
            section.setCreateTime(new Date());
            section.setUpdateTime(new Date());
            section.setCreateBy(userBy);
            section.setUpdateBy(userBy);
            if (d.getIsEnable() == null)
                d.setIsEnable(true);
            section.setDeleteStatus(d.getIsEnable());
            section.setSubdisId(id);
            sections.add(section);
        });

        if (!CollectionUtils.isEmpty(pSections))
            sectionMapper.batchInsert(sections);
    }

    @Override
    public int delete(Long id) {
        SysSectionExample sectionExample = new SysSectionExample();
        sectionExample.createCriteria().andSubdisIdEqualTo(id);
        sectionMapper.deleteByExample(sectionExample);
        SysSubdistrictExample example = new SysSubdistrictExample();
        example.createCriteria().andIdEqualTo(id);
        return subdistrictMapper.deleteByExample(example);
    }

    @Override
    public List<SubdistrictVo> list(String subDisName, String secName, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysSubdistrict> list = subdistrictMapper.findSubstrict(subDisName, secName);
        return list.stream().map(e -> {
            SubdistrictVo subdistrictVo = new SubdistrictVo();
            subdistrictVo.setSubDisId(e.getId());
            subdistrictVo.setSubDisName(e.getName());
            subdistrictVo.setSections(e.getSections().stream().map(
                    o -> {
                        BaseEnableVo baseEnableVo = new BaseEnableVo();
                        baseEnableVo.setId(o.getId());
                        baseEnableVo.setName(o.getName());
                        baseEnableVo.setIsEnable(o.getIsEnable());
                        return baseEnableVo;
                    }
            ).collect(Collectors.toList()));
            subdistrictVo.setLastBy(e.getUpdateBy());
            subdistrictVo.setLastTime(e.getUpdateTime());
            return subdistrictVo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<BaseVo> getSectionsById(Long id) {
        SysSectionExample example = new SysSectionExample();
        example.createCriteria().andSubdisIdEqualTo(id).andDeleteStatusEqualTo(false);
        List<SysSection> sections = sectionMapper.selectByExample(example);
        return sections.stream().map(e -> {
            BaseVo vo = new BaseVo();
            vo.setId(e.getId());
            vo.setName(e.getName());
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<BaseVo> listAll() {
        List<SysSubdistrict> sysSubdistricts = subdistrictMapper.selectByExample(new SysSubdistrictExample());
        return sysSubdistricts.stream().map(e -> {
            BaseVo vo = new BaseVo();
            vo.setId(e.getId());
            vo.setName(e.getName());
            return vo;
        }).collect(Collectors.toList());
    }
}

 

import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface SysSubdistrictMapper {
    long countByExample(SysSubdistrictExample example);

    int deleteByExample(SysSubdistrictExample example);

    int deleteByPrimaryKey(Long id);

    int insert(SysSubdistrict record);

    int insertSelective(SysSubdistrict record);

    List<SysSubdistrict> selectByExample(SysSubdistrictExample example);

    SysSubdistrict selectFirstByExample(SysSubdistrictExample example);

    SysSubdistrict selectByPrimaryKey(Long id);

    int updateByExampleSelective(@Param("record") SysSubdistrict record, @Param("example") SysSubdistrictExample example);

    int updateByExample(@Param("record") SysSubdistrict record, @Param("example") SysSubdistrictExample example);

    int updateByPrimaryKeySelective(SysSubdistrict record);

    int updateByPrimaryKey(SysSubdistrict record);

    int batchUpdate(List<SysSubdistrict> list);

    int batchUpdateSelective(List<SysSubdistrict> list);

    int batchInsert(List<SysSubdistrict> list);

    List<SysSubdistrict> findSubstrict(@Param("subDisName") String subDisName, @Param("secName") String secName);
}

 

<mapper namespace="dao.mapper.SysSubdistrictMapper">
    <resultMap id="BaseResultMap" type="dao.model.SysSubdistrict">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="director" jdbcType="VARCHAR" property="director"/>
        <result column="principal" jdbcType="VARCHAR" property="principal"/>
        <result column="delete_status" jdbcType="TINYINT" property="deleteStatus"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
    </resultMap>
    <resultMap id="SubdistrictMap" extends="BaseResultMap" type="dao.model.SysSubdistrict">
        <collection property="sections" ofType="dao.model.BaseEnableVo">
            <result column="sn_id" property="id"/>
            <result column="sn_name" property="name"/>
            <result column="sn_del_status" property="isEnable"/>
        </collection>
    </resultMap>
    <sql id="Example_Where_Clause">
        <where>
            <foreach collection="oredCriteria" item="criteria" separator="or">
                <if test="criteria.valid">
                    <trim prefix="(" prefixOverrides="and" suffix=")">
                        <foreach collection="criteria.criteria" item="criterion">
                            <choose>
                                <when test="criterion.noValue">
                                    and ${criterion.condition}
                                </when>
                                <when test="criterion.singleValue">
                                    and ${criterion.condition} #{criterion.value}
                                </when>
                                <when test="criterion.betweenValue">
                                    and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                                </when>
                                <when test="criterion.listValue">
                                    and ${criterion.condition}
                                    <foreach close=")" collection="criterion.value" item="listItem" open="("
                                             separator=",">
                                        #{listItem}
                                    </foreach>
                                </when>
                            </choose>
                        </foreach>
                    </trim>
                </if>
            </foreach>
        </where>
    </sql>
    <sql id="Update_By_Example_Where_Clause">
        <where>
            <foreach collection="example.oredCriteria" item="criteria" separator="or">
                <if test="criteria.valid">
                    <trim prefix="(" prefixOverrides="and" suffix=")">
                        <foreach collection="criteria.criteria" item="criterion">
                            <choose>
                                <when test="criterion.noValue">
                                    and ${criterion.condition}
                                </when>
                                <when test="criterion.singleValue">
                                    and ${criterion.condition} #{criterion.value}
                                </when>
                                <when test="criterion.betweenValue">
                                    and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                                </when>
                                <when test="criterion.listValue">
                                    and ${criterion.condition}
                                    <foreach close=")" collection="criterion.value" item="listItem" open="("
                                             separator=",">
                                        #{listItem}
                                    </foreach>
                                </when>
                            </choose>
                        </foreach>
                    </trim>
                </if>
            </foreach>
        </where>
    </sql>
    <sql id="Base_Column_List">
        id
        , `name`, director, principal, delete_status, create_time, create_by, update_time,
    update_by
    </sql>
    <select id="selectByExample" parameterType="dao.model.SysSubdistrictExample"
            resultMap="BaseResultMap">
        select
        <if test="distinct">
            distinct
        </if>
        <include refid="Base_Column_List"/>
        from sys_subdistrict
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
        <if test="orderByClause != null">
            order by ${orderByClause}
        </if>
        <if test="limit != null">
            <if test="offset != null">
                limit ${offset}, ${limit}
            </if>
            <if test="offset == null">
                limit ${limit}
            </if>
        </if>
    </select>
    <select id="selectFirstByExample" parameterType="dao.model.SysSubdistrictExample"
            resultMap="BaseResultMap">
        select
        <if test="distinct">
            distinct
        </if>
        <include refid="Base_Column_List"/>
        from sys_subdistrict
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
        <if test="orderByClause != null">
            order by ${orderByClause}
        </if>
        limit 1
    </select>
    <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from sys_subdistrict
        where id = #{id,jdbcType=BIGINT}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
        delete
        from sys_subdistrict
        where id = #{id,jdbcType=BIGINT}
    </delete>
    <delete id="deleteByExample" parameterType="dao.model.SysSubdistrictExample">
        delete from sys_subdistrict
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
    </delete>
    <insert id="insert" parameterType="dao.model.SysSubdistrict">
        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
            SELECT LAST_INSERT_ID()
        </selectKey>
        insert into sys_subdistrict (id, `name`, director,
        principal, delete_status, create_time,
        create_by, update_time, update_by
        )
        values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, #{director,jdbcType=VARCHAR},
        #{principal,jdbcType=VARCHAR}, #{deleteStatus,jdbcType=TINYINT}, #{createTime,jdbcType=TIMESTAMP},
        #{createBy,jdbcType=VARCHAR}, #{updateTime,jdbcType=TIMESTAMP}, #{updateBy,jdbcType=VARCHAR}
        )
    </insert>
    <insert id="insertSelective" parameterType="dao.model.SysSubdistrict">
        insert into sys_subdistrict
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="name != null">
                `name`,
            </if>
            <if test="director != null">
                director,
            </if>
            <if test="principal != null">
                principal,
            </if>
            <if test="deleteStatus != null">
                delete_status,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="createBy != null">
                create_by,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="updateBy != null">
                update_by,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=BIGINT},
            </if>
            <if test="name != null">
                #{name,jdbcType=VARCHAR},
            </if>
            <if test="director != null">
                #{director,jdbcType=VARCHAR},
            </if>
            <if test="principal != null">
                #{principal,jdbcType=VARCHAR},
            </if>
            <if test="deleteStatus != null">
                #{deleteStatus,jdbcType=TINYINT},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="createBy != null">
                #{createBy,jdbcType=VARCHAR},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateBy != null">
                #{updateBy,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>
    <select id="countByExample" parameterType="dao.model.SysSubdistrictExample"
            resultType="java.lang.Long">
        select count(*) from sys_subdistrict
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
    </select>
    <select id="findSubstrict" resultMap="SubdistrictMap">
        select ss.id id,
        ss.name name,
        ss.director director,
        ss.principal principal,
        ss.update_by update_by,
        ss.update_time update_time,
        sn.id sn_id,
        sn.name sn_name,
        sn.delete_status sn_del_status
        from sys_subdistrict ss
        left join sys_section sn
        on ss.id = sn.subdis_id
        where ss.delete_status = 0
        <if test="subDisName != null and subDisName != ''">
            and ss.`name` like concat("%",#{subDisName},"%")
        </if>
        <if test="secName != null and secName != ''">
            and sn.`name` like concat("%",#{secName},"%")
        </if>
        order by ss.create_time desc
    </select>
    <update id="updateByExampleSelective" parameterType="map">
        update sys_subdistrict
        <set>
            <if test="record.id != null">
                id = #{record.id,jdbcType=BIGINT},
            </if>
            <if test="record.name != null">
                `name` = #{record.name,jdbcType=VARCHAR},
            </if>
            <if test="record.director != null">
                director = #{record.director,jdbcType=VARCHAR},
            </if>
            <if test="record.principal != null">
                principal = #{record.principal,jdbcType=VARCHAR},
            </if>
            <if test="record.deleteStatus != null">
                delete_status = #{record.deleteStatus,jdbcType=TINYINT},
            </if>
            <if test="record.createTime != null">
                create_time = #{record.createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="record.createBy != null">
                create_by = #{record.createBy,jdbcType=VARCHAR},
            </if>
            <if test="record.updateTime != null">
                update_time = #{record.updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="record.updateBy != null">
                update_by = #{record.updateBy,jdbcType=VARCHAR},
            </if>
        </set>
        <if test="_parameter != null">
            <include refid="Update_By_Example_Where_Clause"/>
        </if>
    </update>
    <update id="updateByExample" parameterType="map">
        update sys_subdistrict
        set id = #{record.id,jdbcType=BIGINT},
        `name` = #{record.name,jdbcType=VARCHAR},
        director = #{record.director,jdbcType=VARCHAR},
        principal = #{record.principal,jdbcType=VARCHAR},
        delete_status = #{record.deleteStatus,jdbcType=TINYINT},
        create_time = #{record.createTime,jdbcType=TIMESTAMP},
        create_by = #{record.createBy,jdbcType=VARCHAR},
        update_time = #{record.updateTime,jdbcType=TIMESTAMP},
        update_by = #{record.updateBy,jdbcType=VARCHAR}
        <if test="_parameter != null">
            <include refid="Update_By_Example_Where_Clause"/>
        </if>
    </update>
    <update id="updateByPrimaryKeySelective" parameterType="dao.model.SysSubdistrict">
        update sys_subdistrict
        <set>
            <if test="name != null">
                `name` = #{name,jdbcType=VARCHAR},
            </if>
            <if test="director != null">
                director = #{director,jdbcType=VARCHAR},
            </if>
            <if test="principal != null">
                principal = #{principal,jdbcType=VARCHAR},
            </if>
            <if test="deleteStatus != null">
                delete_status = #{deleteStatus,jdbcType=TINYINT},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="createBy != null">
                create_by = #{createBy,jdbcType=VARCHAR},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>
    <update id="updateByPrimaryKey" parameterType="dao.model.SysSubdistrict">
        update sys_subdistrict
        set `name`        = #{name,jdbcType=VARCHAR},
            director      = #{director,jdbcType=VARCHAR},
            principal     = #{principal,jdbcType=VARCHAR},
            delete_status = #{deleteStatus,jdbcType=TINYINT},
            create_time   = #{createTime,jdbcType=TIMESTAMP},
            create_by     = #{createBy,jdbcType=VARCHAR},
            update_time   = #{updateTime,jdbcType=TIMESTAMP},
            update_by     = #{updateBy,jdbcType=VARCHAR}
        where id = #{id,jdbcType=BIGINT}
    </update>
    <update id="batchUpdate" parameterType="java.util.List">
        update sys_subdistrict
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="name = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    when id = #{item.id,jdbcType=BIGINT} then #{item.name,jdbcType=VARCHAR}
                </foreach>
            </trim>
            <trim prefix="director = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    when id = #{item.id,jdbcType=BIGINT} then #{item.director,jdbcType=VARCHAR}
                </foreach>
            </trim>
            <trim prefix="principal = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    when id = #{item.id,jdbcType=BIGINT} then #{item.principal,jdbcType=VARCHAR}
                </foreach>
            </trim>
            <trim prefix="delete_status = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    when id = #{item.id,jdbcType=BIGINT} then #{item.deleteStatus,jdbcType=TINYINT}
                </foreach>
            </trim>
            <trim prefix="create_time = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    when id = #{item.id,jdbcType=BIGINT} then #{item.createTime,jdbcType=TIMESTAMP}
                </foreach>
            </trim>
            <trim prefix="create_by = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    when id = #{item.id,jdbcType=BIGINT} then #{item.createBy,jdbcType=VARCHAR}
                </foreach>
            </trim>
            <trim prefix="update_time = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    when id = #{item.id,jdbcType=BIGINT} then #{item.updateTime,jdbcType=TIMESTAMP}
                </foreach>
            </trim>
            <trim prefix="update_by = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    when id = #{item.id,jdbcType=BIGINT} then #{item.updateBy,jdbcType=VARCHAR}
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach close=")" collection="list" item="item" open="(" separator=", ">
            #{item.id,jdbcType=BIGINT}
        </foreach>
    </update>
    <update id="batchUpdateSelective" parameterType="java.util.List">
        update sys_subdistrict
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="name = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.name != null">
                        when id = #{item.id,jdbcType=BIGINT} then #{item.name,jdbcType=VARCHAR}
                    </if>
                </foreach>
            </trim>
            <trim prefix="director = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.director != null">
                        when id = #{item.id,jdbcType=BIGINT} then #{item.director,jdbcType=VARCHAR}
                    </if>
                </foreach>
            </trim>
            <trim prefix="principal = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.principal != null">
                        when id = #{item.id,jdbcType=BIGINT} then #{item.principal,jdbcType=VARCHAR}
                    </if>
                </foreach>
            </trim>
            <trim prefix="delete_status = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.deleteStatus != null">
                        when id = #{item.id,jdbcType=BIGINT} then #{item.deleteStatus,jdbcType=TINYINT}
                    </if>
                </foreach>
            </trim>
            <trim prefix="create_time = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.createTime != null">
                        when id = #{item.id,jdbcType=BIGINT} then #{item.createTime,jdbcType=TIMESTAMP}
                    </if>
                </foreach>
            </trim>
            <trim prefix="create_by = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.createBy != null">
                        when id = #{item.id,jdbcType=BIGINT} then #{item.createBy,jdbcType=VARCHAR}
                    </if>
                </foreach>
            </trim>
            <trim prefix="update_time = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.updateTime != null">
                        when id = #{item.id,jdbcType=BIGINT} then #{item.updateTime,jdbcType=TIMESTAMP}
                    </if>
                </foreach>
            </trim>
            <trim prefix="update_by = case" suffix="end,">
                <foreach collection="list" index="index" item="item">
                    <if test="item.updateBy != null">
                        when id = #{item.id,jdbcType=BIGINT} then #{item.updateBy,jdbcType=VARCHAR}
                    </if>
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach close=")" collection="list" item="item" open="(" separator=", ">
            #{item.id,jdbcType=BIGINT}
        </foreach>
    </update>
    <insert id="batchInsert" parameterType="java.util.List">
        insert into sys_subdistrict (id, `name`, director,
        principal, delete_status, create_time,
        create_by, update_time, update_by
        )
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.id,jdbcType=BIGINT}, #{item.name,jdbcType=VARCHAR}, #{item.director,jdbcType=VARCHAR},
            #{item.principal,jdbcType=VARCHAR}, #{item.deleteStatus,jdbcType=TINYINT},
            #{item.createTime,jdbcType=TIMESTAMP},
            #{item.createBy,jdbcType=VARCHAR}, #{item.updateTime,jdbcType=TIMESTAMP}, #{item.updateBy,jdbcType=VARCHAR}
            )
        </foreach>
    </insert>
</mapper>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值