sql 片段:
<sql id="columnBase">
`id`,
`title`,
`author_id` as authorId,
`state`,
`featured`,
`style`
</sql>
 
实际应用在代码中:
 <sql id="periodicalColumns">
         a.id AS "id",
         a.name AS "name",
         a.country AS "country",
         a.type AS "type",
         a.publish AS "publish",
         a.introduction AS  "introduction",
         a.logo AS "logo",
         a.company AS "company",
         a.CN AS "cn",
         a.size AS "size",
         a.address AS "address",
         a.ISSN AS "issn",
         a.languages AS "languages",
         a.email AS "email",
         a.year AS "year",
         a.create_by AS "createBy.id",
         a.create_date AS "createDate",
         a.update_by AS "updateBy.id",
         a.update_date AS "updateDate",
         a.remarks AS "remarks",
         a.del_flag AS "delFlag"
     </sql>

最后贴一个分完整的代码:

 <?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">
 <mapper namespace="com.thinkgem.jeesite.modules.tpydg.periodical.dao.PeriodicalDao">

     <sql id="periodicalColumns">
         a.id AS "id",
         a.name AS "name",
         a.country AS "country",
         a.type AS "type",
         a.publish AS "publish",
         a.introduction AS  "introduction",
         a.logo AS "logo",
         a.company AS "company",
         a.CN AS "cn",
         a.size AS "size",
         a.address AS "address",
         a.ISSN AS "issn",
         a.languages AS "languages",
         a.email AS "email",
         a.year AS "year",
         a.create_by AS "createBy.id",
         a.create_date AS "createDate",
         a.update_by AS "updateBy.id",
         a.update_date AS "updateDate",
         a.remarks AS "remarks",
         a.del_flag AS "delFlag"
     </sql>

     <sql id="periodicalJoins">
     </sql>

     <select id="get" resultType="Periodical">
         SELECT
             <include refid="periodicalColumns"/>
         FROM periodical a
         <include refid="periodicalJoins"/>
         WHERE a.id = #{id}
     </select>

     <select id="findList" resultType="Periodical">
         SELECT
             <include refid="periodicalColumns"/>
         FROM periodical a
         <include refid="periodicalJoins"/>
         <where>
             a.del_flag = #{DEL_FLAG_NORMAL}
             <if test="type != null and type != ''">
                 AND  type = #{type}
             </if>
             <if test="name != null and name != ''">
                 AND a.name LIKE
                     <if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
                     <if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
                     <if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
             </if>
         </where>
         <choose>
             <when test="page !=null and page.orderBy != null and page.orderBy != ''">
                 ORDER BY ${page.orderBy}
             </when>
             <otherwise>
                 ORDER BY a.update_date DESC
             </otherwise>
         </choose>
     </select>

     <select id="findAllList" resultType="Periodical">
         SELECT
             <include refid="periodicalColumns"/>
         FROM periodical a
         <include refid="periodicalJoins"/>
         <where>
             a.del_flag = #{DEL_FLAG_NORMAL}
         </where>
         <choose>
             <when test="page !=null and page.orderBy != null and page.orderBy != ''">
                 ORDER BY ${page.orderBy}
             </when>
             <otherwise>
                 ORDER BY a.update_date DESC
             </otherwise>
         </choose>
     </select>

     <insert id="insert">
         INSERT INTO periodical(
             id,
             name,
             country,
             type,
             publish,
             introduction,
             logo,
             company,
             CN,
             size,
             address,
             ISSN,
             languages,
             email,
             year,
             create_by,
             create_date,
             update_by,
             update_date,
             remarks,
             del_flag
         ) VALUES (
             #{id},
             #{name},
             #{country},
             #{type},
             #{publish},
             #{introduction},
             #{logo},
             #{company},
             #{cn},
             #{size},
             #{address},
             #{issn},
             #{languages},
             #{email},
             #{year},
             #{createBy.id},
             #{createDate},
             #{updateBy.id},
             #{updateDate},
             #{remarks},
             #{delFlag}
         )
     </insert>

     <update id="update">
         UPDATE periodical SET
             name = #{name},
             country = #{country},
             type = #{type},
             introduction = #{introduction},
             logo = #{logo},
             company = #{company},
             CN = #{cn},
             size = #{size},
             address = #{address},
             ISSN = #{issn},
             languages = #{languages},
             email = #{email},
             year = #{year}
         WHERE id = #{id}
     </update>

     <update id="delete">
         UPDATE periodical SET
             del_flag = #{DEL_FLAG_DELETE}
         WHERE id = #{id}
     </update>

 </mapper>

8. sql 片段的更多相关文章

  1. MyBatis的一系列问题的处理(遍历Map集合和智能标签和属性和字段不一样的解决办法 和sql片段)(三)

    一.字段名与属性名(数据库的名字)不一样怎么办? 方案一:在小配置中配置一个resultMapper <!--方案一:resultMapper 字段名与属性名不一致 --> <res ...

  2. 6.Mybatis中的动态Sql和Sql片段(Mybatis的一个核心)

    动态Sql是Mybatis的核心,就是对我们的sql语句进行灵活的操作,他可以通过表达式,对sql语句进行判断,然后对其进行灵活的拼接和组装.可以简单的说成Mybatis中可以动态去的判断需不需要某些 ...

  3. mybatis动态SQL中的sql片段

    在mybatis中通过使用SQL片段可以提高代码的重用性,如下情景: 1.创建动态SQL <sql id="sql_count">select count(*)< ...

  4. ibatis复用SQL片段、引入片段 动态条件增加

    1:ibatis复用SQL片段.引入片段  使用[sql]和[include]标签: 通常情况下,你会这样写:xml 代码 <select id="selectItemCount&qu ...

  5. Mybatis Sql片段的应用

    在一个查询里,针对各种不同数据库,有时候只是一部分 SQL 是不相同的,为避免相同的部分复制多次,所以将不相同的部分进行适当的隔离,再重用就可以了. 在 MyBatis 里声明两段 <sql d ...

  6. 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma

     1 PersonTestMapper.xml中的内容如下: <?xmlversion="1.0"encoding="UTF-8"?> < ...

  7. SSM-MyBatis-05:Mybatis中别名,sql片段和模糊查询加getMapper

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 简单概述一下本讲 别名,sql片段简单写一下,模糊查询多写一点 一.别名 <typeAliases> ...

  8. sql片段

    1):定义sql片段 <!-- 定义sql片段 --> <!-- id: sql片段的标识 经验:1:基于单表来定义sql片段,这样的话sql片段的可重用性才高 2:sql片段中不要 ...

  9. 使用mybatis提供的各种标签方法实现动态拼接Sql。使用sql片段提取重复的标签内容

    Sql中可将重复的sql提取出来,使用时用include引用即可,最终达到sql重用的目的,如下: <select id="findUserByNameAndSex" par ...

  10. java-mybaits-00402-Mapper-动态sql-if、where、foreach、sql片段

    1.动态sql(重点) 通过mybatis提供的各种标签方法实现动态拼接sql. 什么是动态sql mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装.   ...

随机推荐

  1. 问题:C#控制台 停留;结果:c#控制台如何延时显示

    Thread.Sleep(毫秒数);//比如Thread.Sleep(2000)即为延时2秒需using System.Threading; 随笔5 - C#控制台窗口的显示与隐藏 1. 定义一个Co ...

  2. DAY17-Django之logging

    LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'standard': { 'format': ...

  3. 34款Firefox渗透测试插件

    1:Firebug Firefox的 五星级强力推荐插件之一,不许要多解释 2:User Agent Switcher 改变客户端的User Agent的一款插件 3:Hackbar 攻城师必备工具, ...

  4. sqlplus 连接数据库执行SP

    .bashrc export HOME= export LANG="C" .bash_profile #明码 #export LOG_USER=lhcx #export LOG_P ...

  5. PTA 估值一亿的AI核心代码

    题面 比赛时被模拟题打自闭了,本来以为是个比较麻烦的模拟,实际上只要会C++的regex不到40行就能把这个题过掉了(orz smz) regex是用来处理正则表达式,里面有个函数regex_repl ...

  6. Python 安装 第三方库的安装技巧

    Python 安装 第三方库的安装技巧 我的电脑:Windows 10 64位. Python IDE 软件:PyCharm 2016.1.4 Python version : Python 3.5. ...

  7. PCL—关键点检测(Harris)低层次点云处理

    博客转载自:http://www.cnblogs.com/ironstark/p/5064848.html 除去NARF这种和特征检测联系比较紧密的方法外,一般来说特征检测都会对曲率变化比较剧烈的点更 ...

  8. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-003比较算法及算法的可视化

    一.介绍 1. 2. 二.代码 1. package algorithms.elementary21; /*********************************************** ...

  9. Json Post到 https的坑 - the underlying connection was closed an unexpected error occurred on a send(远程服务器未知错误导致关闭)

    最近做了一个安装包,安装包会弹出dotnet的 窗体,这个安装包会去调用https的一个api.用测试程序测试窗体都是好的.一旦打入安装包后,就报错.研究了半天,原来是https惹的祸 解决方案: . ...

  10. 《Maven实战》笔记-8-构建部署Web项目

      一.Web项目结构 1.显式指定Web项目打包方式为war:   2.默认目录 根据“约定大于配置”的规则,Web项目的类及资源文件默认位置为src/main/java和src/main/reso ...