一、操作类型
select,delete,update,insert
二、基本输入参数
int | string | boolean | long | short | float | double | date
1.单一参数
parameterType="int"
2.多个参数使用map
1)使用参数map
(1)代码里设置参数Map
Map param = new HashMap();
param.put("param1","1");
param.put("param2","2");
(2)配置中设置参数类型为map parameterType="map"
2)使用命名空间参数parameterMap
(1)声明参数
<parameterMap id=”insert-product-param”class=”com.domain.Product”>
<parameter property=”id” jdbcType=”NUMERIC” javaType=”int” nullValue=”-9999999”/>
<parameter property=”description” jdbcType=”VARCHAR” nullValue=”NO_ENTRY”/>
</parameterMap>
<select id=”insertProduct” parameterMap=”insert-product-param”>
insert into PRODUCT (PRD_ID, PRD_DESCRIPTION) values (?,?);
</select>
3. 对象映射
select中使用resultMap返回映射的对象
<resultMap id="vehicleResult" type="Vehicle">
<id property=”id” column="id" />
<result property="year" column="year"/>
<result property="color" column="color"/>
<discriminator javaType="int" column="vehicle_type">
<case value="1" resultMap="carResult"/>
<case value="2" resultMap="truckResult"/>
<case value="3" resultMap="vanResult"/>
<case value="4" resultMap="suvResult"/>
</discriminator>
</resultMap>
<resultMap id="carResult" type="Car">
<result property=”doorCount” column="door_count" />
</resultMap>
三、常用条件语句
include | trim | where | set | foreach | choose | if | bind
1. include:节约代码,不过可阅读性不好
<sql id="selectItem_fragment">
FROM items WHERE parentid = #value#
<sql>
<select id="selectItemCount" parameterClass="int" resultClass="int">
SELECT COUNT(*) AS total
<include refid="selectItem_fragment"/>
<select>
2. trim:??
3. where:在多个if条件时,等于”where 1=1“,后面再跟条件,如果都添加and或or
`<select id="getTable" parameterType="Table" resultMap="Table">
select * from table
<where>
<if test="id !=null and id!='' ">
table.id =#{id}
</if>
<if test="column1!= null and column1!= '' ">
AND column1 = #{column1}
</if>
</where>
</select>`
4. set:多个if条件在一起时,只有所有条件为真,sql执行,可用于插入检查等
` <update id="updateTable" parameterType="Table">
UPDATE table
<set>
<if test="id!=null and id!='' ">
table.id = #{id},
</if>
<if test="column1!=null and column1!='' ">
table.column1 = #{column1},
</if>
</set>
WHERE table.id = #{id};
</update>`
5. choose:和when otherwise联用,多个条件中选择一个,和swich相似
`<select id="getTable" parameterType="Table" resultMap="Table">
SELECT * from table
<where>
<choose>
<when test="column1!=null and column1!='' ">
column1 = #{column1}
</when>
<when test="column2!=null and column2!='' ">
column2 = #{column2}
</when>
<otherwise>
1=1
</otherwise>
</choose>
</where>
</select> `
6. foreach:用于参数中拼接集合参数
`<select id="getTable" resultMap="Table">
SELECT * FROM talbe
WHERE id IN
<foreach collection="list" item="idList" open="(" id="," close=")">
#{idList}
</foreach>
</select> `
7. if:条件拼接sql
<update id="updateTable" parameterType="Table">
UPDATE table
<if test="id!=null and id!='' ">
table.id = #{id},
</if>
WHERE table.id = #{id};
</update>
8. bind:???
四、sql中使用比较><=
<![CDATA[ < ]]>
五、mysql中自增主键
<insert id="insertTable" parameterType="Table" useGeneratedKeys="true" keyProperty="id">
insert into table (col1, col2) values (#{col1}, #{col2})
</insert>
六、插入或更新的实现
<insert id="insertOrSave" parameterType="Entity" useGeneratedKeys="true" keyProperty="id">
<selectKey keyProperty="count" resultType="int" order="BEFORE">
select count(*) from entity where version=#{version}
</selectKey>
<if test="count > 0">
update entity set note=#{note} where version = #{version}
</if>
<if test="count==0">
insert into entity (note,version) values (#{note},#{version})
</if>
</insert>
Entity中需要有count属性,用来存储临时变量count,不是很推荐这么使用,建议在service层实现
七、时间格式化
在操作中使用 DATE_FORMAT(formatDate, "%Y-%m-%d %H:%I:%S")
1.参数说明:
%S, %s 两位数字形式的秒( 00,01, …, 59)
%I, %i 两位数字形式的分( 00,01, …, 59)
%H 两位数字形式的小时,24 小时(00,01, …, 23)
%h 两位数字形式的小时,12 小时(01,02, …, 12)
%k 数字形式的小时,24 小时(0,1, …, 23)
%l 数字形式的小时,12 小时(1, 2, …, 12)
%T 24 小时的时间形式(hh:mm:ss)
%r 12 小时的时间形式(hh:mm:ss AM 或hh:mm:ss PM)
%p AM或PM
%W 一周中每一天的名称(Sunday, Monday, …, Saturday)
%a 一周中每一天名称的缩写(Sun, Mon, …, Sat)
%d 两位数字表示月中的天数(00, 01,…, 31)
%e 数字形式表示月中的天数(1, 2, …, 31)
%D 英文后缀表示月中的天数(1st, 2nd, 3rd,…)
%w 以数字形式表示周中的天数( 0 = Sunday, 1=Monday, …, 6=Saturday)
%j 以三位数字表示年中的天数( 001, 002, …, 366)
%U 周(0, 1, 52),其中Sunday 为周中的第一天
%u 周(0, 1, 52),其中Monday 为周中的第一天
%M 月名(January, February, …, December)
%b 缩写的月名( January, February,…., December)
%m 两位数字表示的月份(01, 02, …, 12)
%c 数字表示的月份(1, 2, …., 12)
%Y 四位数字表示的年份
%y 两位数字表示的年份
%% 直接值“%”00,01, …, 23)
%h 两位数字形式的小时,12 小时(01,02, …, 12)
%k 数字形式的小时,24 小时(0,1, …, 23)
%l 数字形式的小时,12 小时(1, 2, …, 12)
%T 24 小时的时间形式(hh:mm:ss)
%r 12 小时的时间形式(hh:mm:ss AM 或hh:mm:ss PM)
%p AM或PM
%W 一周中每一天的名称(Sunday, Monday, …, Saturday)
%a 一周中每一天名称的缩写(Sun, Mon, …, Sat)
%d 两位数字表示月中的天数(00, 01,…, 31)
%e 数字形式表示月中的天数(1, 2, …, 31)
%D 英文后缀表示月中的天数(1st, 2nd, 3rd,…)
%w 以数字形式表示周中的天数( 0 = Sunday, 1=Monday, …, 6=Saturday)
%j 以三位数字表示年中的天数( 001, 002, …, 366)
%U 周(0, 1, 52),其中Sunday 为周中的第一天
%u 周(0, 1, 52),其中Monday 为周中的第一天
%M 月名(January, February, …, December)
%b 缩写的月名( January, February,…., December)
%m 两位数字表示的月份(01, 02, …, 12)
%c 数字表示的月份(1, 2, …., 12)
%Y 四位数字表示的年份
%y 两位数字表示的年份
%% 直接值“%”