Mybatis动态公用sql

本文介绍使用MyBatis实现多条件分页查询的方法,包括如何设置SQL语句来适应不同类型的参数,以及如何通过包含引用实现代码复用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<select id="collPageCount" parameterType="java.util.Map" resultType="long">
		  select count(1) from ( <include refid="selectPage"></include> ) a
	</select> 
	<select id="collListPage" parameterType="java.util.Map" resultMap="mycoll">
       <include refid="selectPage"></include>
	</select>
	<sql id="selectPage">
	 SELECT
	c.id as cid,
	i.id AS iid,
	i.type_id,
	i.`name`,
	i.img
	FROM
	wsp_collection c
	LEFT JOIN wsp_vf_info i ON c.vf_id = i.id 
		<where>
			<if test="userid != null and  userid != '' ">
				and c.user_id=#{userid}
			</if>
		</where>
		and  i.state=1
	</sql>

  mybatis进行多条件查询的时候,如果传入的参数有int类型,对用非空判断只需要"id!= null''"即可,不需要进行"id!=''"的判断,否则查询条件会被忽略。

<sql id="condition_sql">
        <if test="userid != null and userid != ''"> and  user_id = #{userid} </if>
        <if test="vfId != null and vfId != ''"> and  vf_id = #{vfId} </if>
        <if test="id != null"> and id =#{id}</if><!-- id为int型-->
    </sql>
<select id="listPageCount" parameterType="java.util.Map" resultType="long">
		select count(1) from 
		wsp_vf_plays
		<where>
		 <include refid="condition_sql"></include>
		 </where>
	</select> 

  

### 定义可复用的带参数 SQL 片段 在 MyBatis 中,可以利用 `<sql>` 标签来定义公共 SQL 片段,并通过 `<include>` 标签引用这些片段以实现代码复用。当涉及到带有参数的 SQL 片段时,除了基本的 `refid` 属性外,还可以使用 `property` 属性传递参数给 SQL 片段。 #### 使用 `<sql>` 和 `<include>` 实现带参数的 SQL 片段复用 对于包含条件判断逻辑的 SQL 片段,可以在 `<sql>` 标签内部编写相应的 `<if>` 判断语句,并设置好测试表达式中的变量名以便后续调用时能够正确替换实际值: ```xml <sql id="conditionalWhereClause"> <where> <if test="title != null">and title like concat('%',#{title},'%')</if> <if test="author != null">and author=#{author}</if> </where> </sql> ``` 上述例子展示了如何创建一个名为 `conditionalWhereClause` 的 SQL 片段,它可以根据输入参数动态构建 WHERE 子句[^3]。 接着,在具体的查询映射中引入此 SQL 片段并为其指定必要的属性值: ```xml <select id="findBooksByTitleOrAuthor" parameterType="map" resultType="Book"> SELECT * FROM books <include refid="conditionalWhereClause"/> </select> ``` 这里假设有一个方法接收两个可能为空的字符串类型的参数 `title` 和 `author` 来查找书籍记录;如果这两个参数都不为空,则会在最终生成的 SQL 查询中加入对应的过滤条件[^4]。 另外,为了更灵活地处理不同场景下的需求变化,MyBatis 支持通过 `${}` 占位符的方式向 SQL 片段注入外部传入的参数。这种方式适用于那些不希望被预编译成 PreparedStatement 参数占位符的位置,例如表名或者列名等固定结构部分。 #### 动态传参实例 下面是一个更加复杂的案例,演示了怎样在一个 SQL 片段里接受来自父级 Select 或 Update 语句的参数,并将其应用于子查询或其他复杂逻辑之中: ```xml <!-- 定义 sql 片段 --> <sql id="dynamicOrderBy"> ORDER BY ${orderByColumn} <choose> <when test="orderDirection == 'asc'">ASC</when> <otherwise>DESC</otherwise> </choose> </sql> <!-- 调用 sql 片段 --> <select id="getSortedRecords" parameterType="java.util.Map" resultType="Record"> SELECT * FROM records r <include refid="dynamicOrderBy"> <property name="orderByColumn" value="r.create_time"/> <property name="orderDirection" value="${sortDir}"/> <!-- 注意这里的写法 --> </include> </select> ``` 在这个例子中,不仅实现了基于传入参数调整排序顺序的功能,还示范了一种安全的方式来处理潜在的安全风险(如 SQL 注入),即尽可能采用 `#{}` 方式的绑定而非直接拼接字符串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值