mybatis多条件查询及动态sql

看代码,项目见码云


test是条件判断,当传入的参数不是null时才可以

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.xdl.dao.XdlUserMapper">


	<!--sql片段,作用是被引用-->
	<sql id="xdlUser">select * from user1</sql>
	<select id="findAll" resultType="com.xdl.entity.XdlUser">
	/*	使用include标签引用*/
		<include refid="xdlUser"></include>
	</select>

	<select id="findByCondit" resultType="com.xdl.entity.XdlUser">
		select * from user1
		where
		<choose>
			<when test="id != null">
				id = #{id}
			</when>
			<when test="username != null and username!= ''">
				username = #{username}
			</when>
			<otherwise>
				passwd = #{passwd}
			</otherwise>
		</choose>

		<!--<trim prefix="where" suffixOverrides="and">
			<if test="id != null">
				 id = #{id} and
			</if>

			<if test="username != null">
				username = #{username} and
			</if>

			<if test="passwd != null">
				passwd = #{passwd}  and
			</if>
		</trim>-->

	</select>

	<update id="update" parameterType="com.xdl.entity.XdlUser">
		update user1
		<trim prefix="set" suffixOverrides=",">

			<if test="username != null">
				username = #{username} ,
			</if>

			<if test="passwd != null">
				passwd = #{passwd}
			</if>
		</trim>
		<where>
			<if test="id != null">
				id = #{id}
			</if>
		</where>
	</update>

	<insert id="add" parameterType="com.xdl.entity.XdlUser" useGeneratedKeys="true" keyProperty="id">
		insert into user1(username,passwd) values (
        <choose>
			<when test="passwd == '333'">'man'</when>
			<when test="passwd == '222'">'女'</when>
			<otherwise>'not all'</otherwise>
		</choose>
		)
	</insert>

	<delete id="delMore" >
		delete from user1 where id in (${value })
	</delete>

	<select id="findByList" resultType="com.xdl.entity.XdlUser">
		select * from user1 where
		/*如果传入是集合则collection里添list,数组则array*/
		<foreach collection="array" item="id" open="(" close=")" separator="or">
			id = #{id}
		</foreach>
	</select>

	<delete id="delByList">
		delete from user1 where id in
		<foreach collection="array" item="id" open="(" close=")" separator=",">
			#{id}
		</foreach>
	</delete>

	<insert id="insertUser">
		insert into user1 values
		<foreach collection="array" item="user" separator=",">
			(null,#{user.username},#{user.passwd})
		</foreach>
	</insert>

	<update id="updateMore">
		<foreach collection="array" item="user">
			/*注意update操作时,一定要在jdbc后面加allowMultiQueries=true,即jdbc:mysql://localhost:3306/test?allowMultiQueries=true*/
			update user1 set username = #{user.username},passwd = #{user.passwd} where id = #{user.id};
		</foreach>
	</update>
</mapper>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值