动态SQL


xml文件

<settings>
    <setting name="logImpl" value="STDOUT_LOGGING"/>
<!--    是否开启驼峰命名自动映射,即从经典数据库列名 A_COLUMN 映射到经典 Java 属性名 aColumn-->
    <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

    <typeAliases>
        <typeAlias type="com.yao.pojo.Blog" alias="Blog"/>
 <!--    通过包-->
<!--            <package name="com.yao.pojo"/>-->
    </typeAliases>



public interface BlogMapper {
    //插入数据
int addBlog(Blog blog);
//查询博客
List<Blog> queryBlogIf(Map map);

    List<Blog> queryBlogChoose(Map map);
//更新
int updateBlog(Map map);
}
================
 <select id="queryBlogIf" parameterType="Map" resultType="blog">
  select * from blog where 1=1
  <if test="title!=null">
   and title=#{title}
  </if>
  <if test="author!=null">
   and title=#{author}
  </if>
 </select>

<select id="queryBlogChoose" parameterType="map" resultType="blog">
 select * from blog
 <where>
  <choose>
    <when test="title!=null">
     title=#{title}
    </when>
    <when test="author!=null">
      and author=#{author}
    </when>
    <otherwise>
     and views=#{views}
    </otherwise>
  </choose>
 </where>
</select>

 <update id="updateBlog" parameterType="map">
update blog
<set>
 <if test="title!=null">
title=#{title},
 </if>
 <if test="author!=null">
  author=#{author}
 </if>
</set>
 where id=#{id}

 </update>

  

 

动态SQL

上一篇:MyBatis封装JDBC具体实现


下一篇:MySQL 5.7免安装版配置详细教程