|
| 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="Test"> |
| 4 | + <sql id="sometable"> |
| 5 | + fruits |
| 6 | + </sql> |
| 7 | + <sql id="somewhere"> |
| 8 | + WHERE |
| 9 | + category = #{category} |
| 10 | + </sql> |
| 11 | + <sql id="someinclude"> |
| 12 | + FROM |
| 13 | + <include refid="${include_target}"/> |
| 14 | + <include refid="somewhere"/> |
| 15 | + </sql> |
| 16 | + <select id="testParameters"> |
| 17 | + SELECT |
| 18 | + name, |
| 19 | + category, |
| 20 | + price |
| 21 | + FROM |
| 22 | + fruits |
| 23 | + WHERE |
| 24 | + category = #{category} |
| 25 | + AND price > ${price} |
| 26 | + </select> |
| 27 | + <select id="testInclude"> |
| 28 | + SELECT |
| 29 | + name, |
| 30 | + category, |
| 31 | + price |
| 32 | + <include refid="someinclude"> |
| 33 | + <property name="prefix" value="Some"/> |
| 34 | + <property name="include_target" value="sometable"/> |
| 35 | + </include> |
| 36 | + </select> |
| 37 | + <select id="testIf"> |
| 38 | + SELECT |
| 39 | + name, |
| 40 | + category, |
| 41 | + price |
| 42 | + FROM |
| 43 | + fruits |
| 44 | + WHERE |
| 45 | + 1=1 |
| 46 | + <if test="category != null and category !=''"> |
| 47 | + AND category = #{category} |
| 48 | + </if> |
| 49 | + <if test="price != null and price !=''"> |
| 50 | + AND price = ${price} |
| 51 | + <if test="price >= 400"> |
| 52 | + AND name = 'Fuji' |
| 53 | + </if> |
| 54 | + </if> |
| 55 | + </select> |
| 56 | + <select id="testTrim"> |
| 57 | + SELECT |
| 58 | + name, |
| 59 | + category, |
| 60 | + price |
| 61 | + FROM |
| 62 | + fruits |
| 63 | + <trim prefix="WHERE" prefixOverrides="AND|OR"> |
| 64 | + OR category = 'apple' |
| 65 | + OR price = 200 |
| 66 | + </trim> |
| 67 | + </select> |
| 68 | + <select id="testWhere"> |
| 69 | + SELECT |
| 70 | + name, |
| 71 | + category, |
| 72 | + price |
| 73 | + FROM |
| 74 | + fruits |
| 75 | + <where> |
| 76 | + AND category = 'apple' |
| 77 | + <if test="price != null and price !=''"> |
| 78 | + AND price = ${price} |
| 79 | + </if> |
| 80 | + </where> |
| 81 | + </select> |
| 82 | + <update id="testSet"> |
| 83 | + UPDATE |
| 84 | + fruits |
| 85 | + <set> |
| 86 | + <if test="category != null and category !=''"> |
| 87 | + category = #{category}, |
| 88 | + </if> |
| 89 | + <if test="price != null and price !=''"> |
| 90 | + price = ${price}, |
| 91 | + </if> |
| 92 | + </set> |
| 93 | + WHERE |
| 94 | + name = #{name} |
| 95 | + </update> |
| 96 | + <select id="testChoose"> |
| 97 | + SELECT |
| 98 | + name, |
| 99 | + category, |
| 100 | + price |
| 101 | + FROM |
| 102 | + fruits |
| 103 | + <where> |
| 104 | + <choose> |
| 105 | + <when test="name != null"> |
| 106 | + AND name = #{name} |
| 107 | + </when> |
| 108 | + <when test="category == 'banana'"> |
| 109 | + AND category = #{category} |
| 110 | + <if test="price != null and price !=''"> |
| 111 | + AND price = ${price} |
| 112 | + </if> |
| 113 | + </when> |
| 114 | + <otherwise> |
| 115 | + AND category = 'apple' |
| 116 | + </otherwise> |
| 117 | + </choose> |
| 118 | + </where> |
| 119 | + </select> |
| 120 | + <select id="testForeach"> |
| 121 | + SELECT |
| 122 | + name, |
| 123 | + category, |
| 124 | + price |
| 125 | + FROM |
| 126 | + fruits |
| 127 | + <where> |
| 128 | + category = 'apple' AND |
| 129 | + <foreach collection="apples" item="name" open="(" close=")" separator="OR"> |
| 130 | + <if test="name == 'Jonathan' or name == 'Fuji'"> |
| 131 | + name = #{name} |
| 132 | + </if> |
| 133 | + </foreach> |
| 134 | + </where> |
| 135 | + </select> |
| 136 | + <insert id="testInsertMulti"> |
| 137 | + INSERT INTO |
| 138 | + fruits |
| 139 | + ( |
| 140 | + name, |
| 141 | + category, |
| 142 | + price |
| 143 | + ) |
| 144 | + VALUES |
| 145 | + <foreach collection="fruits" item="fruit" separator=","> |
| 146 | + ( |
| 147 | + #{fruit.name}, |
| 148 | + #{fruit.category}, |
| 149 | + ${fruit.price} |
| 150 | + ) |
| 151 | + </foreach> |
| 152 | + </insert> |
| 153 | + <select id="testBind"> |
| 154 | + <bind name="likeName" value="'%' + name + '%'"/> |
| 155 | + SELECT |
| 156 | + name, |
| 157 | + category, |
| 158 | + price |
| 159 | + FROM |
| 160 | + fruits |
| 161 | + WHERE |
| 162 | + name like #{likeName} |
| 163 | + </select> |
| 164 | +</mapper> |
0 commit comments