1.blackList黑名单SQL操作,删除的时候均为逻辑删除,不做物理删除
表结构:sns_black_list
Field Type Comment
id bigint(20) unsigned
user_id bigint(20)
black_user_id bigint(20)
gmt_create datetime
gmt_modified datetime
is_deleted char(1)
1.半自动化转换:声明类对象,映射到类对象
<typeAlias alias="SnsMyBlackListUser"
type="com.taobao.matrix.protocol.domain.myblacklist.SnsMyBlackListUser" />
<resultMap id="SnsMyBlackListUserResult" class="SnsMyBlackListUser">
<result property="id" column="id" />
<result property="userId" column="user_id" />
<result property="blackUserId" column="black_user_id" />
<result property="gmtCreated" column="gmt_create" />
<result property="gmtModified" column="gmt_modified" />
<result property="isDeleted" column="IS_DELETED" />
</resultMap>
2.SQL语句操作
2.1:查询某个用户的所有黑名单用户
<select id="queryMyBlackUser" resultMap="SnsMyBlackListUserResult">
<![CDATA[
SELECT id,user_id,black_user_id,is_deleted,gmt_create,gmt_modified
FROM sns_my_black_list$tablename$
WHERE user_id=#userId# AND black_user_id=#blackUserId# limit 1
]]>
</select>
2.2: 取消某个用户的某个黑名单用户
<delete id="updateMyBlackUser">
<![CDATA[
UPDATE sns_my_black_list$tablename$
SET is_deleted = #isDeleted# , gmt_modified=now()
WHERE user_id=#userId# AND black_user_id=#blackUserId#
]]>
</delete>
SELECT COUNT(*) FROM MYBALCKLIST$TALBENAME$ WHERE USERID=#userid# and ISDELETED = #0#
2.3:将某用户加黑到黑名单用户当中
二个步骤:
2.3.1:查询删除记录,如果存在则修改此条记录
查询:<select id="queryMyBlackUser" resultMap="SnsMyBlackListUserResult">
<![CDATA[
SELECT id,user_id,black_user_id,is_deleted,gmt_create,gmt_modified
FROM sns_my_black_list$tablename$
WHERE user_id=#userId# AND black_user_id=#blackUserId# limit 1
]]>
</select>
修改: <delete id="updateMyBlackUser">
<![CDATA[
UPDATE sns_my_black_list$tablename$
SET is_deleted = #isDeleted# , gmt_modified=now()
WHERE user_id=#userId# AND black_user_id=#blackUserId#
]]>
</delete>
2.3.2:否则插入一条新数据
<insert id="insertMyBlackListUser">
<![CDATA[
INSERT INTO sns_my_black_list$tablename$
(ID,USER_ID, BLACK_USER_ID,GMT_CREATE,GMT_MODIFIED,IS_DELETED)
VALUES
(#id#,#userId#,#blackUserId#,now(),now(),'n')
]]>
</insert>
2.4分页查询:根据传入的start和pagesize进行查询
SELECT black_user_id
FROM sns_my_black_list$tablename$
WHERE user_id=#userId# AND is_deleted='n'
ORDER BY gmt_modified desc
limit #start#,#pageSize#
limit #start#,#pageSize# 替代方式:
<dynamic prepend="">
<isNotNull property="_start">
<isNotNull property="_size">
limit #_start#, #_size#
</isNotNull>
</isNotNull>
</dynamic>
3.动态语句查询与书写
3.1:日志系统blog_article_sqlmqp.xml 此时为属性articleArchive不为空时执行下列语句
<select id="getArticlesByUserId" resultMap="ArticleResult">
<dynamic prepend="and">
<isNotNull property="articleArchive">
<![CDATA[
gmt_create < #articleArchive#
]]>
</isNotNull>
</dynamic>
3.2: 循环迭代输出,policy为一个List的集合
eg1: <isNotNull property="policy">
<![CDATA[ policy in ]]>
<iterate property="policy" open="(" close=")" conjunction=",">
<![CDATA[#policy[]#]]>
</iterate>
</isNotNull>
eg2:
delete from 表名 where sex=#sex#
<iterate prepend="and" property="personList" open="("
close=")" conjunction="or">
age=$personList[].age$
</iterate>
3.3: limit动态选择问题,如果为0,则去掉limit,否则为#num#
<dynamic prepend = "limit " >
<isNotEqual property="num" compareValue="0" >
0,#num#
</isNotEqual>
</dynamic>
3.4: 判断属性是否存在
<!--------------------------------------------------------------------几个常用属性-->
<isPropertyAvailable> 属性是存在
<isNotPropertyAvailable> 属性不存在
<isNull> 属性值是null
<isEmpty> 判断Collection.size<1或String.length()<1
<isEqual> 等于
<isNotEqual> 不等于
<isGreaterThan> 大于
<isGreaterEqual> 大于等于
<isLessThan> 小于
<isLessEqual> 小于等于