前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mapper.xml

Mapper.xml

作者头像
二十三年蝉
发布2018-07-05 11:40:13
4800
发布2018-07-05 11:40:13
举报
文章被收录于专栏:闻道于事闻道于事

空白Mapper

代码语言:javascript
复制
<?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.**">

</mapper>

完整Mapper

代码语言:javascript
复制
<?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.bootdo.system.dao.DeptDao">

    <select id="get" resultType="com.bootdo.system.domain.DeptDO">
        select
        `dept_id`,`parent_id`,`name`,`order_num`,`del_flag` from sys_dept
        where dept_id = #{value}
    </select>

    <select id="list" resultType="com.bootdo.system.domain.DeptDO">
        select `dept_id`,`parent_id`,`name`,`order_num`,`del_flag` from
        sys_dept
        <where>
            <if test="deptId != null and deptId != ''"> and dept_id = #{deptId} </if>
            <if test="parentId != null and parentId != ''"> and parent_id = #{parentId} </if>
            <if test="name != null and name != ''"> and name = #{name} </if>
            <if test="orderNum != null and orderNum != ''"> and order_num = #{orderNum} </if>
            <if test="delFlag != null and delFlag != ''"> and del_flag = #{delFlag} </if>
        </where>
        <choose>
            <when test="sort != null and sort.trim() != ''">
                order by ${sort} ${order}
            </when>
            <otherwise>
                order by dept_id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="count" resultType="int">
        select count(*) from sys_dept
        <where>
            <if test="deptId != null and deptId != ''"> and dept_id = #{deptId} </if>
            <if test="parentId != null and parentId != ''"> and parent_id = #{parentId} </if>
            <if test="name != null and name != ''"> and name = #{name} </if>
            <if test="orderNum != null and orderNum != ''"> and order_num = #{orderNum} </if>
            <if test="delFlag != null and delFlag != ''"> and del_flag = #{delFlag} </if>
        </where>
    </select>

    <insert id="save" parameterType="com.bootdo.system.domain.DeptDO"
        useGeneratedKeys="true" keyProperty="deptId">
        insert into sys_dept
        (
        `parent_id`,
        `name`,
        `order_num`,
        `del_flag`
        )
        values
        (
        #{parentId},
        #{name},
        #{orderNum},
        #{delFlag}
        )
    </insert>

    <update id="update" parameterType="com.bootdo.system.domain.DeptDO">
        update sys_dept
        <set>
            <if test="parentId != null">`parent_id` = #{parentId}, </if>
            <if test="name != null">`name` = #{name}, </if>
            <if test="orderNum != null">`order_num` = #{orderNum}, </if>
            <if test="delFlag != null">`del_flag` = #{delFlag}</if>
        </set>
        where dept_id = #{deptId}
    </update>

    <delete id="remove">
        delete from sys_dept where dept_id = #{value}
    </delete>

    <delete id="batchRemove">
        delete from sys_dept where dept_id in
        <foreach item="deptId" collection="array" open="(" separator=","
            close=")">
            #{deptId}
        </foreach>
    </delete>
    
    <select id="listParentDept" resultType="long">
        select DISTINCT parent_id from sys_dept
    </select>

    <select id="getDeptUserNumber" resultType="int">
        select count(*) from sys_user where dept_id = #{value}
    </select>
</mapper>

Mapper常用方法

代码语言:javascript
复制
    <resultMap id="CropResultMap" type="agriculture_basedata.entity.Crop" >
        <id column="id" property="id" jdbcType="BIGINT" />
        <result column="crop" property="crop" jdbcType="VARCHAR" />
        <result column="pic" property="pic" jdbcType="VARCHAR" />
        <result column="crop_pic" property="crop_pic" jdbcType="VARCHAR" />
        <result column="status" property="status" jdbcType="INTEGER" />
        <result column="del" property="del" jdbcType="TINYINT" />
    </resultMap>

    <sql id="Crop_Column_List" >
        id, crop, status,pic, crop_pic,del
    </sql>

批量添加

代码语言:javascript
复制
    <insert id="addDayList" parameterType="java.util.List"  useGeneratedKeys="true"  keyProperty="id">
        insert into summary_day
        (energy_type,team,sum,base_data_day,company,equipment_id,total)
        values
        <foreach collection="list" item="hour" index="index"
                 separator=",">
            (
            #{hour.energy_type},#{hour.team},#{hour.sum},
            #{hour.base_data_day},#{hour.company},#{hour.equipment_id},#{hour.total}
            )
        </foreach>
    </insert>

时间常用方法:

代码语言:javascript
复制
DATE_FORMAT(sh.base_data_day, '%Y-%m')

DATE_SUB(#{year}, INTERVAL 1 YEAR)


如果为null返回0

代码语言:javascript
复制
ifnull(ys.val, 0)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-06-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档