Mapper.xml

空白Mapper

<?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

<?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常用方法

    <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>

批量添加

    <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>

时间常用方法:

DATE_FORMAT(sh.base_data_day, '%Y-%m')

DATE_SUB(#{year}, INTERVAL 1 YEAR)


如果为null返回0

ifnull(ys.val, 0)

猜你喜欢

转载自www.cnblogs.com/jiangwz/p/9214133.html