MybBatis動態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.sunreal.mapper.BlogMapper">
<!--SQL片段,將常用SQL進行封裝-->
<sql id="if-title-author">
<if test="title != null and title != ''">
title = #{title}
</if>
<if test='state != null and state != "1"'>
and author = #{author}
</if>
</sql>
<!--如果實體類(createTime),資料庫(create_time),
需要的<settings>標籤中設定,開啟駝峰轉換
<setting name="mapUnderscoreToCamelCase" value="true"/>,
或使用<resultMap>標籤進行對映-->
<insert id="addBlog" parameterType="Blog">
INSERT INTO mybatis.blog (id, title, author, create_time, views)
VALUES (#{id}, #{title}, #{author}, #{createTime}, #{views})
</insert>
<!--<where>標籤可以自動過濾多餘的and-->
<!--注意:<test>標籤中引數要是有雙引號("")state != "1",否則無效-->
<select id="queryBlog" resultType="Blog" parameterType="map">
select *
from mybatis.blog
<where>
<include refid="if-title-author"></include>
</where>
</select>
<!--<choose>標籤,相當於JAVA的switch語句,
即使多個條件符合要求,也只會選擇優先順序最高的一條進行執行,
同時也可以自動過濾多餘(and)-->
<select id="queryBlogChoose" resultType="Blog">
select * from mybatis.blog
<where>
<choose>
<when test="title != null and title != ''">
title = #{title}
</when>
<when test="author != null and author != ''">
and author = #{author}
</when>
<otherwise>
and views = '1000'
</otherwise>
</choose>
</where>
</select>
<!--<set>標籤,在更新操作中自動過濾多餘的逗號(,)-->
<update id="updateBlog" parameterType="map">
UPDATE MYBATIS.BLOG
<set>
<if test="title != null and title != ''">
TITLE = #{title},
</if>
<if test="author != null and author != ''">
AUTHOR = #{author}
</if>
</set>
WHERE ID = #{id}
</update>
<!--foreach,需要傳入集合(ids)-->
<!--select * from mybatis.blog WHERE title = ? and ( id = ? or id = ? )-->
<select id="queryBlogFReach" resultType="Blog" parameterType="map">
select *
from mybatis.blog
<where>
<if test="title != null and title != ''">
title = #{title}
</if>
<foreach collection="ids" item="id" open="and (" close=")" separator="or">
id = #{id}
</foreach>
</where>
</select>
</mapper>
相關文章
- 動態SQLSQL
- 動態SQL intoSQL
- MyBatis 動態 SQLMyBatisSQL
- MyBatis動態SQLMyBatisSQL
- Mybatics動態sqlBATSQL
- ABAP 動態 SQLSQL
- Oracle 動態SQLOracleSQL
- PLSQL Language Referenc-PL/SQL動態SQL-何時需要動態SQLSQL
- PL/SQL執行動態SQLSQL
- 1 Mybatis動態SQLMyBatisSQL
- Mybatis--動態SQLMyBatisSQL
- MyBatis(七) 動態SQLMyBatisSQL
- 避免動態SQL(二)SQL
- 避免動態SQL(一)SQL
- 動態sql 報表SQL
- PLSQL Language Referenc-PL/SQL動態SQL-本地動態SQL(EXECUTE IMMEDIATE語句)SQL
- PL/SQL 動態sql語句例SQL
- pl/sql--動態SQL常用方法SQL
- 動態sql和利用動態sql解決資料字典的讀取SQL
- mybatis動態sql總結MyBatisSQL
- Mybatis-06 動態SqlMyBatisSQL
- Mybatis 動態 SQL 詳解MyBatisSQL
- SqlServer中的動態SqlSQLServer
- Kettle實現動態SQLSQL
- SQL SERVER 動態查詢SQLServer
- MyBatis4:動態SQLMyBatisSQL
- 動態sql無法recoverSQL
- 用動態SQL語句SQL
- 動態SQL語句 (轉)SQL
- 繫結變數在靜態sql和動態sql中變數SQL
- SQL Server動態SQL,繫結變數SQLServer變數
- 動態SQL——構造通用動態頁面查詢SQL
- oracle procedure plsql 動態sql之動態傳遞表OracleSQL
- PLSQL Language Referenc-PL/SQL動態SQL-練習:在動態SQL塊中呼叫子程式。SQL
- MyBatis框架之SQL對映和動態SQLMyBatis框架SQL
- Mybatis介紹之 動態SQLMyBatisSQL
- Java-Mybatis動態SQL整理JavaMyBatisSQL
- MyBatis對動態SQL的支援MyBatisSQL