首页 > 数据库技术 > 详细

动态SQL

时间:2021-04-03 20:09:42      阅读:15      评论:0      收藏:0      [点我收藏+]

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

原文:https://www.cnblogs.com/yyyyyou/p/14614442.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!