MyBatis 用法整理

标签使用说明举例
selectid :唯一标识,和定义mybatis接口方法名一致。

service层:

Map<String, String> map = new HashMap<String, String>();
map.put("a", a);
map.put("b", b);

// 调用Dao层mybatis接口
List<String> listData = MyBatisRepsository.selectData(map);

Dao层:

public List<String> selectData(Map<String> param);

MyBatis层:

<select id="selectData" parameterType="map" resultType="list">

parameterType :传入的参数类型,全路径名或别名。 例:com.test.poso.User或user
resultType :返回值类型或别名,和定义mybatis接口方法返回类型一致。
resultMap:配置java对象属性与查询结果集中列名对应的关系。可以参照:关于Mybatis的resultMap用法_ly741236987的博客-CSDN博客
insert

id :和定义mybatis接口方法名一致。


parameterType :传入的参数类型,全路径名或别名。 例:com.test.poso.User或user

resultType :返回值类型或别名,和定义mybatis接口方法返回类型一致。

<insert id="insertUser" parameterType="User">
     insert into

        User(ID, NAME, AGE)

        values(

                #{ id }, #{name}, #{age})
</insert>

update<update id="updateUser" parameterType="User">
     update User set NAME=#{name}, AGE=#{age} where ID=#{id}
</update>
delete<delete id="deleteUser" parameterType="User"> 
      delete from User 
      where ID = #{id} 
</delete>
iftest: 判断条件

<select id="getUserName" parameterType="User" resultType="String">     
    SELECT

        NAME

    from

        User
    <if test="id!=null and id!='' ">     
        WHERE ID = #{id}  
    </if>     
</select> 

foreachcollection:collection属性的值有三个分别是list、array、map三种,分别对应的参数类型为:List、数组、map集合。
item :表示在迭代过程中每一个元素的别名。
index :表示在迭代过程中每次迭代到的位置(下标)。
open :前缀。
close :后缀。
separator :分隔符,表示迭代时每个元素之间以什么分隔。

<select id="selectIn" resultType="String">
        select  NAME  from User 
        where ID in
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
</select>

choose

子标签
<when></when>
<otherwise></otherwise>

when中test:判断条件

<select id="getName" parameterType="User" resultType="String">     
    SELECT NAME from USER WHERE 1=1    
    <where>     
        <choose>     
            <when test="ID!=null and ID!='' ">     
                   AND ID = #{id}
            </when>            
            <otherwise>     
                    AND ID = "1"
            </otherwise>     
        </choose>     
    </where>     
</select>   
where相当于SQL的where关键字,不需要添加1=1,
如果name为空的情况,在拼接id条件时会自动剔除掉and(or)
<select id="getName" parameterType="User" resultType="String">     
    SELECT NAME from USER      
       <where>   
         <if test="name!=null and name!='' ">     
            NAME LIKE CONCAT(CONCAT('%', #{name}),'%')    
         </if>     
         <if test="id!= null and id!= '' ">     
            AND ID = #{id}      
         </if>  
       </where>        
</select>    
set如果age为空只更新name的情况,使用set标签会自动去除最后面逗号<update id="updateUser" parameterType="User">     
    UPDATE STUDENT      
    <set>     
        <if test="name!=null and name!='' ">     
            NAME = #{name},      
        </if> 
        <if test="age!=null and age!='' ">     
            AGE = #{age}    
        </if>     
    </set>     
    WHERE ID = #{id};      
</update>  
sql多个sql查询了相同的字段,可以将相同的字段用<sql>的方式定义

<sql id="Base_Column_List">
        ID,NAME,AGE
</sql>

<select id="selectAll" resultType="User">
      SELECT
        <include refid="Base_Column_List" />
      FROM
        User
</select>

includerefid: 调用对应sql标签的id名称
collection包在resultMap里作为结果集中某个属性,同结果集其他属性一起返回

  <resultMap id="userResultCollection" type="com.cloudwalk.shark.model.User">
        <id property="id" column="ID" jdbcType="INTEGER"></id>
        <result property="userName" column="user_name" jdbcType="VARCHAR"></result>
        <collection property="roleList" select="selectRoles" column="{id=id,userName213=user_name}" ofType="com.cloudwalk.shark.model.Role" >
        </collection>
    </resultMap>

    <select id="selectRoles" resultType="com.cloudwalk.shark.model.Role">
        select role_name from t_shark_user t join  t_shark_role r
        where  t.id = r.user_id
    </select>


    <select id="queryAllUser"  resultMap="userResultCollection">
       SELECT    u.id,    user_name FROM    t_shark_user u JOIN t_shark_role r ON u.id = r.user_id GROUP BY    u.id,user_name
    </select>

 <resultMap id="userResultCollection" type="com.cloudwalk.shark.model.User">
        <id property="id" column="ID" jdbcType="INTEGER"></id>
        <result property="userName" column="user_name" jdbcType="VARCHAR"></result>
        <collection property="roleList"  ofType="com.cloudwalk.shark.model.Role" >
            <result property="roleName" column="role_name" jdbcType="VARCHAR"></result>
        </collection>
    </resultMap>


    <select id="queryAllUser"  resultMap="userResultCollection">
       SELECT    u.id,    user_name,role_name FROM    t_shark_user u JOIN t_shark_role r ON u.id = r.user_id
    </select>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值