Mybatis笔记

还是做了一部分笔记

<?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">
<!-- sql-mybatis常用元素 mapper:用于区分不同的mapper全局唯一 cache:配置给定命名空间的缓存 cache-ref:从其他命名空间引用缓存配置 
    resultMap:用来描述数据库结果和对象的对应关系 sql:可以重用的sql语句块,也可以被其他 语句引用 insert:映射插入语句 update:映射更新语句 
    delete:映射删除语句 select:映射查询语句 -->
<!-- mapper文件 配置数据库sql语句namespace用于区分不同的mapper文件全局唯一 的使用接口实现名字必须和接口限定名一样 -->
<mapper namespace="dao.UserMapper">
    <!-- select查询映射文件是mybatis最常用的元素之一属性id该命名空间下唯一标识符resultType表面返回值类型 -->
    <!-- 查询用户数 -->
    <select id="count" resultType="int">select count(*) from smbms_user
    </select>
    <!-- 查询用户名 -->
    <select id="getUserlist" resultType="entity.User">
        SELECT userName FROM
        smbms_user
    </select>
    <!-- 根据用户名模糊查询 parameterType:入参数据类型string单个值入参 -->
    <select id="getUserListByUserName" resultType="entity.User"
        parameterType="string">
        SELECT userName FROM smbms_user WHERE userName LIKE
        CONCAT('%',#{userName},'%')
    </select>
    <!-- 使用引用类型 -->
    <select id="getUserListByUserNameUser" resultType="entity.User"
        parameterType="entity.User">
        SELECT userName FROM smbms_user WHERE userName LIKE
        CONCAT('%',#{userName},'%')
    </select>
    <!-- 使用map集合入参 -->
    <select id="getUserListByUsermap" resultType="entity.User"
        parameterType="map">
        SELECT userName FROM smbms_user WHERE userName LIKE
        CONCAT('%',#{userName},'%')
    </select>
    <!-- 使用resultMap进行结果映射 foreach主要是用于一个循环,大部分用到的是循环的生成sql,下面说一下动态foreach的属性: 
        foreach元素的属性主要有item,index,collection,open,separator,close。 1、collection表示如何来得到这个集合,如果传入的直接为一个List,那么collection值就为list,如果直接传入的为一个array不可变数组, 
        那么collection值就为array,如果传入的为一个dto,比如dto里面的array变量名为idLists,那么collection的值就为idLists。 
        2、item表示集合中每一个元素进行迭代时的别名,比如item为value,那么,每次获取的都使用#{value}即可 3、index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,一般很少使用这个 
        4、open表示该语句以什么开始 5、separator表示在每次进行迭代之间以什么符号作为分隔符 6、close表示以什么结束 collection最关键并最容易出错的属性,需要格外注意,该属性必须指定不同的情况下该属性有不同的属性值,主要有一下三种情况 
        1.诺入参为单参数且参数类型是一个list的时候,collection属性值为list。 2.诺入参为单参数且参数类型是数组的时候collection属性值为array 
        3.诺传入参数为多参数,就需要把他们封装为一个map进行处理 -->
    <select id="getUserListByUserrmap" resultMap="ListUser">
        SELECT id,userName FROM smbms_user WHERE gender IN
        <foreach item="gender" collection="array" separator="," open="("
            close=")">
            #{gender}
        </foreach>

    </select>
    <!-- resultMap其中type是返回的结果类型 id对应着resulMap的值 -->
    <resultMap type="entity.User" id="ListUser">
        <!-- 子元素resul用于标识属性property表示属性名column表示数据库字段名 -->
        <result property="id" column="id" />
        <result property="userName" column="userName" />
    </resultMap>
    <!-- 要点 :resultMap与resultType不能同时存在resultType进行基本数据类型映射 resultType进行条件映射每个属性赋指定的值 -->
    <!-- 插入数据 使用注解传值要使用$ -->
    <insert id="add" parameterType="java.lang.String">
        INSERT `smbms_user`(`userCode`)
        VALUES ('${userName}')
    </insert>
    <!-- 删除数据其中parameterType要使用注解入参否则就会获取不到数据可以使用对象 -->
    <delete id="delete" parameterType="java.lang.String">
        DELETE FROM`smbms_user` WHERE
        userCode='${userCole}'
    </delete>
    <!-- 修改数据使用多个参数入参时建议使用注解,太多时建议使用对象 -->
    <update id="update" parameterType="string">
        UPDATE
        `smbms_user`SET`userName`='${userName}' WHERE
        `userCode`='${userCode}'
    </update>
    <!-- 使用association进行一对一结果映射 -->
    <select id="getUserListByRoleId" resultMap="role" parameterType="int">
        select u.*,r.id as r_id,r.roleCode,r.roleName from smbms_user
        u,smbms_role r where u.userRole=${userRoleId} and u.userRole=r.id
    </select>
    <resultMap type="entity.User" id="role">
        <!-- id减少内存开销 -->
        <id property="id" column="id" />
        <result property="userCode" column="userCode" />
        <result property="userName" column="userName" />
        <result property="userRole" column="userRole" />
        <!--javaType要映射值得类型association一对一数据映射 增加result属性可导入外部resultMap进行映射具体实现如下 -->
        <!-- <association property="role" resultMap="某个resultMap的id如role"></association> -->
        <association property="role" javaType="entity.Role">
            <id property="id" column="r_id" />
            <result property="roleCode" column="roleCode" />
            <result property="roleName" column="roleName" />
        </association>
    </resultMap>
    <!-- 使用collection进行一对多结果映射 -->
    <select id="getAddressListByUserId" resultMap="address"
        parameterType="int">
        SELECT u.*,a.id as
        a_id,a.contact,a.addressDesc,a.postCode,a.tel
        FROM smbms_user
        u,smbms_address a where u.id=a.userId AND u.id=#{addressid}
    </select>
    <resultMap type="entity.User" id="address">
        <!-- id减少内存开销 -->
        <id property="id" column="id" />
        <result property="userCode" column="userCode" />
        <result property="userName" column="userName" />
        <!-- property属性名javaType属性类型column数据库字段名 -->
        <collection property="addresses" javaType="entity.Address"
            resultMap="addressResult" />
    </resultMap>
    <resultMap type="entity.Address" id="addressResult">
        <id property="id" column="a_id" />
        <result property="postCode" column="postCode" />
        <result property="tel" column="tel" />
        <result property="contact" column="contact" />
        <result property="addressDesc" column="addressDesc" />
    </resultMap>

    <select id="getUserList_choose" resultType="entity.User">
        select userName from smbms_user where 1=1
        <!-- choose条件判断语句相当于java中的switch语句一般要配合when和otherwise使用 -->
        <choose>
            <!-- test判断条件一旦有条件成立时 即跳出choose -->
            <when test="userName!=null and userName!=''">
                and userName like CONCAT('%',#{userName},'%')
            </when>
            <when test="userRole !=null and userRole!=null">
                and userRole like CONCAT('%',#{userRole},'%')
            </when>
            <otherwise>
                and 1=1
            </otherwise>
        </choose>
    </select>

</mapper>
原文地址:https://www.cnblogs.com/liuduanwu/p/9350721.html