8. sql 片段

sql 片段:
<sql id="columnBase">
`id`,
`title`,
`author_id` as authorId,
`state`,
`featured`,
`style`
</sql>
 
实际应用在代码中:
 1 <sql id="periodicalColumns">
 2         a.id AS "id",
 3         a.name AS "name",
 4         a.country AS "country",
 5         a.type AS "type",
 6         a.publish AS "publish",
 7         a.introduction AS  "introduction",
 8         a.logo AS "logo",
 9         a.company AS "company",
10         a.CN AS "cn",
11         a.size AS "size",
12         a.address AS "address",
13         a.ISSN AS "issn",
14         a.languages AS "languages",
15         a.email AS "email",
16         a.year AS "year",
17         a.create_by AS "createBy.id",
18         a.create_date AS "createDate",
19         a.update_by AS "updateBy.id",
20         a.update_date AS "updateDate",
21         a.remarks AS "remarks",
22         a.del_flag AS "delFlag"
23     </sql>

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

  1 <?xml version="1.0" encoding="UTF-8" ?>
  2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3 <mapper namespace="com.thinkgem.jeesite.modules.tpydg.periodical.dao.PeriodicalDao">
  4     
  5     <sql id="periodicalColumns">
  6         a.id AS "id",
  7         a.name AS "name",
  8         a.country AS "country",
  9         a.type AS "type",
 10         a.publish AS "publish",
 11         a.introduction AS  "introduction",
 12         a.logo AS "logo",
 13         a.company AS "company",
 14         a.CN AS "cn",
 15         a.size AS "size",
 16         a.address AS "address",
 17         a.ISSN AS "issn",
 18         a.languages AS "languages",
 19         a.email AS "email",
 20         a.year AS "year",
 21         a.create_by AS "createBy.id",
 22         a.create_date AS "createDate",
 23         a.update_by AS "updateBy.id",
 24         a.update_date AS "updateDate",
 25         a.remarks AS "remarks",
 26         a.del_flag AS "delFlag"
 27     </sql>
 28     
 29     <sql id="periodicalJoins">
 30     </sql>
 31     
 32     <select id="get" resultType="Periodical">
 33         SELECT 
 34             <include refid="periodicalColumns"/>
 35         FROM periodical a
 36         <include refid="periodicalJoins"/>
 37         WHERE a.id = #{id}
 38     </select>
 39     
 40     <select id="findList" resultType="Periodical">
 41         SELECT 
 42             <include refid="periodicalColumns"/>
 43         FROM periodical a
 44         <include refid="periodicalJoins"/>
 45         <where>
 46             a.del_flag = #{DEL_FLAG_NORMAL}
 47             <if test="type != null and type != ''">
 48                 AND  type = #{type}
 49             </if>
 50             <if test="name != null and name != ''">
 51                 AND a.name LIKE 
 52                     <if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
 53                     <if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
 54                     <if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
 55             </if>
 56         </where>
 57         <choose>
 58             <when test="page !=null and page.orderBy != null and page.orderBy != ''">
 59                 ORDER BY ${page.orderBy}
 60             </when>
 61             <otherwise>
 62                 ORDER BY a.update_date DESC
 63             </otherwise>
 64         </choose>
 65     </select>
 66     
 67     <select id="findAllList" resultType="Periodical">
 68         SELECT 
 69             <include refid="periodicalColumns"/>
 70         FROM periodical a
 71         <include refid="periodicalJoins"/>
 72         <where>
 73             a.del_flag = #{DEL_FLAG_NORMAL}
 74         </where>        
 75         <choose>
 76             <when test="page !=null and page.orderBy != null and page.orderBy != ''">
 77                 ORDER BY ${page.orderBy}
 78             </when>
 79             <otherwise>
 80                 ORDER BY a.update_date DESC
 81             </otherwise>
 82         </choose>
 83     </select>
 84     
 85     <insert id="insert">
 86         INSERT INTO periodical(
 87             id,
 88             name,
 89             country,
 90             type,
 91             publish,
 92             introduction,
 93             logo,
 94             company,
 95             CN,
 96             size,
 97             address,
 98             ISSN,
 99             languages,
100             email,
101             year,
102             create_by,
103             create_date,
104             update_by,
105             update_date,
106             remarks,
107             del_flag
108         ) VALUES (
109             #{id},
110             #{name},
111             #{country},
112             #{type},
113             #{publish},
114             #{introduction},
115             #{logo},
116             #{company},
117             #{cn},
118             #{size},
119             #{address},
120             #{issn},
121             #{languages},
122             #{email},
123             #{year},
124             #{createBy.id},
125             #{createDate},
126             #{updateBy.id},
127             #{updateDate},
128             #{remarks},
129             #{delFlag}
130         )
131     </insert>
132     
133     <update id="update">
134         UPDATE periodical SET     
135             name = #{name},
136             country = #{country},
137             type = #{type},
138             introduction = #{introduction},
139             logo = #{logo},
140             company = #{company},
141             CN = #{cn},
142             size = #{size},
143             address = #{address},
144             ISSN = #{issn},
145             languages = #{languages},
146             email = #{email},
147             year = #{year}
148         WHERE id = #{id}
149     </update>
150     
151     <update id="delete">
152         UPDATE periodical SET 
153             del_flag = #{DEL_FLAG_DELETE}
154         WHERE id = #{id}
155     </update>
156     
157 </mapper>

猜你喜欢

转载自www.cnblogs.com/zhukaixin/p/9155987.html