***.xml拼接sql使用記錄

xml中配置sql:

<insert id = "insert">
INSERT INTO xf_message(
		id,
		question
		) VALUES (
		#{id},
		#{question}
		)
</insert>

<update id="delete">
		DELETE FROM xf_message
		WHERE id = #{id}
	</update>

<update id="update">
		UPDATE xf_message
		<set>
			<if test="question!= null and question!=''">
				question = #{question},
			</if>
			<if test="dyfield5 != null and dyfield5 !=''">
				dyfield5 = #{dyfield5}
			</if>
		</set>
		WHERE id = #{id}
	</update>
<if test="question!= null and question!=''">
				question = #{question},
</if>

這是判斷所需要的數據是否存在的當然最好再加一個undefined,當數據爲這些數據的時候包裹的字符串就不會被拼裝到sql中

<sql id="xfMessageColumns">
		a.id AS "id",
		a.question AS "question",
		a.answer AS "answer",
	</sql>
<sql id="xfMessageJoins">
	</sql>
<select id="get" resultType="XfMessage">
		SELECT
		<include refid="xfMessageColumns" />
		FROM xf_message a
		<include refid="xfMessageJoins" />
		WHERE a.id = #{id}
	</select>
	<select id="findList" resultType="XfMessage">
		SELECT
		a.id AS "id",
		a.question AS "question",
	    u.name as "username",
	    u1.name as "answerusername"
		FROM xf_message a
		left join xf_user u on u.id = a.questionerid
		left join xf_user u1 on u1.id = a.answererid
		<where>
			<if test="question != null and question != ''">
				AND a.question LIKE
				<if test="dbName == 'oracle'">'%'||#{question}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{question}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{question},'%')</if>
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				limit #{startIndex},#{pageSize}
			</otherwise>
		</choose>
	</select>
<select id="findList2Count" resultType="Integer">
	SELECT
	count(1)
	FROM xf_message a
	left join xf_user u on u.id = a.questionerid
	left join xf_user u1 on u1.id = a.answererid
	<where>
		<if test="question != null and question != ''">
			AND a.question LIKE
			<if test="dbName == 'oracle'">'%'||#{question}||'%'</if>
			<if test="dbName == 'mssql'">'%'+#{question}+'%'</if>
			<if test="dbName == 'mysql'">concat('%',#{question},'%')</if>
		</if>
		<if test="answer != null and answer != ''">
			AND a.answer LIKE
			<if test="dbName == 'oracle'">'%'||#{answer}||'%'</if>
			<if test="dbName == 'mssql'">'%'+#{answer}+'%'</if>
			<if test="dbName == 'mysql'">concat('%',#{answer},'%')</if>
		</if>
	</where>
</select>

	<select id="findAllList" resultType="XfMessage">
		SELECT
		<include refid="xfMessageColumns" />
		FROM xf_message a
		<include refid="xfMessageJoins" />
		<where>	</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
			</otherwise>
		</choose>
	</select>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章