MISBoot快速开发平台

Mapper持久层实现

一、 概述

1.作用

1.sql与数据库交互

2.结果与实体类进行映射

2.位置

src.main.resources.cn.ewsd.xxxx.mapper.XxxxMapper.xml

3.命名方式

首字母大写,驼峰命名,如:XxxxMapper.xml

二、示例代码

1.自定义方法示例:
查询select
    <select id="select" resultType="cn.ewsd.system.model.SysUser">
           select
           uuid,user_name,user_name_id,password,sge,sex
           from sys_user
    </select>
保存save
    <insert id="save" parameterType="cn.ewsd.system.model.SysUser">
            insert into sys_user
            (
                `uuid`,
                `create_time`,
                `creator`,
                `creator_id`,
                `is_del`,
                `modifier`,
                `modifier_id`,
                `modify_time`,
                `creator_org_id`,
                `remark`,
                `org_id`,
                `org_name`,
                `user_account`,
                `user_group`,
                `data_auth`,
                `user_name`,
                `user_name_id`,
                `password`,
                `id_card_no`,
                `age`,
                `id_card`,
                `sex`,
                `status`
            )
            values
            (
                #{uuid},
                #{create_time},
                #{creator},
                #{creator_id},
                #{is_del},
                #{modifier},
                #{modifier_id},
                #{modify_time},
                #{creator_org_id},
                #{remark},
                #{org_id},
                #{org_name},
                #{user_name},
                #{user_name_id},
                #{password},
                #{age},
                #{sex}

            )
        </insert>
更新update
    <update id="update" parameterType="cn.ewsd.office.model.SysUser">
        update sys_user
        <set>
            <if test="password != null">&#x60;password&#x60; = #{password}, </if>
        </set>
        where uuid = #{uuid}
    </update>
删除delete
    <delete id="deleteBatch">
        delete from sys_user where uuid in
        <foreach item="uuid" collection="array" open="(" separator="," close=")">
        #{uuid}
        </foreach>
    </delete>
2.用户Mapper.xml示例
用户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="cn.ewsd.system.mapper.SysUserMapper">

        <!-- 可根据自己的需求,是否要使用 -->
        <resultMap type="cn.ewsd.system.model.SysUser" id="sysUserMap">
            <result property="uuid" column="uuid"/>
            <result property="create_time" column="create_time"/>
            <result property="creator" column="creator"/>
            <result property="creator_id" column="creator_id"/>
            <result property="is_del" column="is_del"/>
            <result property="modifier" column="modifier"/>
            <result property="modifier_id" column="modifier_id"/>
            <result property="modify_time" column="modify_time"/>
            <result property="creator_org_id" column="creator_org_id"/>
            <result property="remark" column="remark"/>
            <result property="org_id" column="org_id"/>
            <result property="org_name" column="org_name"/>
            <result property="user_account" column="user_account"/>
            <result property="user_group" column="user_group"/>
            <result property="data_auth" column="data_auth"/>
            <result property="user_name" column="user_name"/>
            <result property="user_name_id" column="user_name_id"/>
            <result property="password" column="password"/>
            <result property="age" column="age"/>
            <result property="sex" column="sex"/>
            <result property="status" column="status"/>

        </resultMap>

      <sql id="Base_Column_List" >
                             uuid
              ,                          create_time
              ,                          creator
              ,                          creator_id
              ,                          is_del
              ,                          modifier
              ,                          modifier_id
              ,                          modify_time
              ,                          creator_org_id
              ,                          remark
              ,                          org_id
              ,                          org_name
              ,                          post
              ,                          school
              ,                          user_account
              ,                          user_group
              ,                          data_auth
              ,                          user_name
              ,                          user_name_id
              ,                          password
              ,                          id_card_no
              ,                          age
              ,                          id_card
              ,                          sex
              ,                          status
                      </sql>

        <select id="getPageSet" resultType="cn.ewsd.system.model.SysUser">
            select
            <include refid="Base_Column_List"></include>
            from sys_user
            <where>
                <if test="filterSort != null">
                    ${filterSort}
                </if>
            </where>
        </select>

        <select id="queryObject" resultType="cn.ewsd.system.model.SysUser">
            select  <include refid="Base_Column_List" />  from sys_user where uuid = #{value}
        </select>

        <select id="queryList" resultType="cn.ewsd.system.model.SysUser">
            select  <include refid="Base_Column_List" />  from sys_user
            <choose>
                <when test="sidx != null and sidx.trim() != &#x27;&#x27;">
                    order by ${sidx} ${order}
                </when>
                <otherwise>
                    order by uuid desc
                </otherwise>
            </choose>
            <if test="offset != null and limit != null">
                limit #{offset}, #{limit}
            </if>
        </select>

        <select id="queryTotal" resultType="int">
            select count(*) from sys_user
        </select>

        <insert id="executeSave" parameterType="cn.ewsd.system.model.SysUser">
            insert into sys_user
            (
                `uuid`,
                `create_time`,
                `creator`,
                `creator_id`,
                `is_del`,
                `modifier`,
                `modifier_id`,
                `modify_time`,
                `creator_org_id`,
                `remark`,
                `org_id`,
                `org_name`,
                `user_account`,
                `user_group`,
                `data_auth`,
                `user_name`,
                `user_name_id`,
                `password`,
                `id_card_no`,
                `age`,
                `id_card`,
                `sex`,
                `status`
            )
            values
            (
                #{uuid},
                #{create_time},
                #{creator},
                #{creator_id},
                #{is_del},
                #{modifier},
                #{modifier_id},
                #{modify_time},
                #{creator_org_id},
                #{remark},
                #{org_id},
                #{org_name},
                #{user_account},
                #{user_group},
                #{data_auth},
                #{user_name},
                #{user_name_id},
                #{password},
                #{id_card_no},
                #{age},
                #{id_card},
                #{sex},
                #{status},

            )
        </insert>

        <update id="executeUpdate" parameterType="cn.ewsd.system.model.SysUser">
            update sys_user
            <set>
                <if test="create_time != null">&#x60;create_time&#x60; = #{create_time}, </if>
                <if test="creator != null">&#x60;creator&#x60; = #{creator}, </if>
                <if test="creator_id != null">&#x60;creator_id&#x60; = #{creator_id}, </if>
                <if test="is_del != null">&#x60;is_del&#x60; = #{is_del}, </if>
                <if test="modifier != null">&#x60;modifier&#x60; = #{modifier}, </if>
                <if test="modifier_id != null">&#x60;modifier_id&#x60; = #{modifier_id}, </if>
                <if test="modify_time != null">&#x60;modify_time&#x60; = #{modify_time}, </if>
                <if test="creator_org_id != null">&#x60;creator_org_id&#x60; = #{creator_org_id}, </if>
                <if test="remark != null">&#x60;remark&#x60; = #{remark}, </if>
                <if test="org_id != null">&#x60;org_id&#x60; = #{org_id}, </if>
                <if test="org_name != null">&#x60;org_name&#x60; = #{org_name}, </if>
                <if test="post != null">&#x60;post&#x60; = #{post}, </if>
                <if test="school != null">&#x60;school&#x60; = #{school}, </if>
                <if test="user_account != null">&#x60;user_account&#x60; = #{user_account}, </if>
                <if test="user_group != null">&#x60;user_group&#x60; = #{user_group}, </if>
                <if test="data_auth != null">&#x60;data_auth&#x60; = #{data_auth}, </if>
                <if test="user_name != null">&#x60;user_name&#x60; = #{user_name}, </if>
                <if test="user_name_id != null">&#x60;user_name_id&#x60; = #{user_name_id}, </if>
                <if test="password != null">&#x60;password&#x60; = #{password}, </if>
                <if test="id_card_no != null">&#x60;id_card_no&#x60; = #{id_card_no}, </if>
                <if test="age != null">&#x60;age&#x60; = #{age}, </if>
                <if test="id_card != null">&#x60;id_card&#x60; = #{id_card}, </if>
                <if test="sex != null">&#x60;sex&#x60; = #{sex}, </if>
                <if test="status != null">&#x60;status&#x60; = #{status}</if>
            </set>
            where uuid = #{uuid}
        </update>

        <delete id="executeDelete">
            delete from sys_user where uuid = #{value}
        </delete>

        <delete id="executeDeleteBatch">
            delete from sys_user where uuid in
            <foreach item="uuid" collection="array" open="(" separator="," close=")">
                #{uuid}
            </foreach>
        </delete>

    </mapper>
QQ在线咨询
客服 QQ
251122361
微信/手机
18973127736
办公电话
0731-89565588