在mapper.xml中如何添加 SQL语句 start with ...connect by条件判断,如果有参数添加否则不添加
可以使用标签
if 标签
<select id="queryBookByif" resultType="com.zking.mybatis01.model.Book">
select <include refid="Base_Column_List"></include> from t_book where 1=1
<if test="null!=bookType and ''!=bookType">
-- book_type 代表数据库字段
-- bookType代表实体属性不需要类名点
-- #代表取值
and book_type=#{bookType}
</if>
</select>
2 trim mybatis中trim是动态拼接;java中表示去除前后空格
prefix 前缀
suffix后缀
suffixOverrides去除后缀指定字符
prefixOverrides 去除前缀指定字符
<insert id="insertSelective" parameterType="com.zking.mybatis01.model.Book" >
insert into t_book
--trim 动态拼接
--prefix 前缀 suffix后缀 suffixOverrides去除后缀指定字符 prefixOverrides 去除前缀指定字符
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="bookId != null" >
book_id,
</if>
<if test="bookName != null" >
book_name,
</if>
<if test="bookNamePinyin != null" >
book_name_pinyin,
</if>
<if test="bookPrice != null" >
book_price,
</if>
<if test="bookType != null" >
book_type,
</if>
</trim>
3 foreach
collection j有循环的集合
item每次遍历的数据项名称
separator分割符,每次循环后添加,最后一次不添加
open 开始位置 close结束位置
<select id="queryBookByForeach" resultType="com.zking.mybatis01.model.Book">
select <include refid="Base_Column_List"></include> from t_book where 1=1 and book_id in
-- collection j有循环的集合 item每次遍历的数据项名称 separator分割符,每次循环后添加,最后一次不添加 open 开始位置 close结束位置
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</select>
4 choose/set/where
choose(判断参数) - 按顺序将实体类 Book 第一个不为空的属性作为:where条件
判断成功则执行,不成功则不执行
<!-- choose(判断参数) - 按顺序将实体类 Book 第一个不为空的属性作为:where条件 -->
<select id="queryBookByChoose" resultType="com.zking.mybatis01.model.Book">
select <include refid="Base_Column_List"/> from t_book where 1 = 1
<choose>
<when test="bookName != null">
and book_name = #{bookName}
</when>
<when test="bookType != null">
and book_type = #{bookType}
</when>
<otherwise>
and book_id = 100
</otherwise>
</choose>
https://github.com/zhikecore/superblog/tree/main/blog-dao/src/main/java/com/zhike/blogdao/mapper
这是oracle语法