MyBatis常用标签

1.定义SQL语句

1.1 select 标签

属性介绍:
  • id:唯一的标识符
  • parameterType:传给此语句的参数的全路径名或别名 例:com.test.pojo.User或user
  • resultType:语句返回值类型或别名。注意,如果是集合,那么这里填写的是集合的泛型,而不是集合本身(resultType 与resultMap 不能并用)
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="Object">
        select * from student where id=#{id}
</select>

 


1.2 insert 标签

属性介绍:
  • id:唯一的标识符
  • parameterType:传给此语句的参数的全路径名或别名 例:com.test.pojo.User
<insert id="insert" parameterType="Object">
    insert into student    
    <trim prefix="(" suffix=")" suffixOverrides="," >    
      <if test="name != null"> NAME,  </if>    
    </trim>
    <trim prefix="values(" suffix=")" suffixOverrides="," >
      <if test="name != null">  #{name}, </if>    
    </trim>
</insert>

 


1.3 delete 标签

属性介绍:
  • id:唯一的标识符
  • parameterType:传给此语句的参数的全路径名或别名 例:com.test.pojo.User
<delete id="deleteByPrimaryKey" parameterType="Object">
    delete from student where id = #{id}
</delete>

 



1.4 update 标签

属性介绍:
  • id:唯一的标识符
  • parameterType:传给此语句的参数的全路径名或别名 例:com.test.pojo.User
<update id="updateByName" parameterType="com.test.pojo.User">
    UPDATE t_user SET update_time = NOW()
    <trim suffixOverrides=",">
        <if test="name != null">, username = #{name}</if>
    </trim>
    WHERE userid = #{id}
</update>

 



2. 建立JAVA对象与数据库列名对应关系

2.1 resultMap 标签

基本作用
  • 建立SQL查询结果字段与实体属性的映射关系信息。
  • 查询的结果集转换为Java对象,方便进一步操作。
  • 将结果集中的列与Java对象中的属性对应起来并将值填充进去


标签说明

   
   主标签:

  • id:resultMap 的标志
  • type:返回值的类名

   子标签

  • id:用于设置主键字段与领域模型属性的映射关系,此处主键为ID,对应id。
  • result:用于设置普通字段与领域模型属性的映射关系

   

示例说明


在数据库中创建一张 user 表, 分别有 user_id,user_name,user_password 三个字段


项目中创建一个实体类 user.java


resultMap为:

<resultMap id="userResultMap" type="com.zyu.cupid.pojo.vo.ArticleVo">
    <result property="id"       column="user_id"/>
    <result property="name"     column="user_name"/>
    <result property="password" column="user_password"/>
</resultMap>
    
<!--查询时resultMap引用userResultMap -->
<select id="selectByPrimaryKey" resultMap="userResultMap" parameterType="Object">
    select id,name,password from user where id=#{id}
</select>

 



3. 动态SQL拼接

3.1 if 标签

if标签通常用于WHERE语句、UPDATE语句、INSERT语句中,通过判断参数值来决定是否使用某个查询条件、判断是否更新某一个字段、判断是否插入某个字段的值

<if test="name != null and name != ''">
     and NAME = #{name}
</if>

 



3.2 foreach 标签

foreach标签主要用于构建in条件,可在sql中对集合进行迭代。也常用到批量删除、添加等操作中。

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


参数:

List<Object> list = new ArrayList();
JSONObject param = new JSONObect();
param.put("idArray", list)

 


使用:
<select id="findList" resultMap="ArticleVo">
    SELECT * FROM article A WHERE art_id IN
      <foreach collection="param.idArray" item="item" index="index" open="(" close=")" separator="," >#{item.id}
      </foreach>
</select>

 



3.3 trim 标签

trim标记是一个格式化的标记,主要用于拼接sql的条件语句(前缀或后缀的添加或忽略),可以完成set或者是where标记的功能。

属性介绍:
  • prefix:前缀覆盖并增加其内容
  • suffix:后缀覆盖并增加其内容
  • prefixOverrides:前缀判断的条件
  • suffixOverrides:后缀判断的条件

例如:

<update id="updateByPrimaryKey" parameterType="Object">
    update user set
    <trim suffixOverrides=",">
        <if test="name != null  ">
            user_name = #{name},
        </if>
        <if test="password != null  ">
            user_password = #{password},
        </if>
    </trim>
    where user_id = #{id}
</update>

 

 

如果password的值为空的话,会执行如下语句

update user set user_name = 'XXX' where user_id = 'XXX'

 

会忽略最后一个 “,”

posted @ 2020-01-16 17:18  橘子味儿的猫  阅读(612)  评论(0编辑  收藏  举报