Mybatis操作数据库--通用

选中.xml 右键 Junite 测试的时候,@Test位于这个包中 import org.junit.jupiter.api.Test;

根据条件进行查询总记录数  条件如果涉及到两个表的内容  你也就只写一个表里面的
select
		count(1)
		from
		smbms_bill 
		<trim prefix="where" prefixOverrides="and | or">
			<if test="productName!=null and productName!=''">
				and productName like CONCAT('%',#{productName},'%')
			</if>
			<if test="providerId!=null and providerId>0">
				and providerId=#{providerId}
			</if>
			<if test="isPayment!=null and isPayment>0">
				and isPayment=#{isPayment}
			</if>
		</trim>


根据条件查询(分页)集合(虽然没有说按什么排序 但是你在最后需要写出来 order By )
select
		b.id,b.billCode,b.productName,b.productDesc,b.productUnit,
		b.productCount,b.totalPrice,b.isPayment,b.providerId,
		b.createdBy,b.creationDate,b.modifyBy,b.modifyDate,p.proName as providerName
		from
		smbms_bill b,smbms_provider p
		where
<trim prefixOverrides="and | or">
	b.providerId=p.id
		<if test="productName!=null and productName!=''">
			and productName like CONCAT('%',#{productName},'%')
		</if>
		<if test="providerId!=null and providerId>0">
			and providerId=#{providerId}
		</if>
		<if test="isPayment!=null and isPayment>0">
			and isPayment=#{isPayment}
		</if>
		order by creationDate DESC,modifyDate DESC
		<if test="startIndex!=null and pageSize!=null">
			limit #{startIndex},#{pageSize}
		</if>
	</trim>
当个参数可以加上注解
查看  通过id 查询返回值Provider
<!-- Provider getById(@Param("id")Integer id) -->
	<select id="getById"  resultType="Provider">
	SELECT
		id,proCode,proName,
		proDesc,proContact,
		proPhone,proAddress,
		proFax,createdBy,
		creationDate,
		modifyDate,modifyBy
	FROM
		smbms_provider
	where
		u.userRole=r.id
		id=#{id}
	</select>
	
        
 删除 (增删改默认返回是int<!-- int delById(@Param("id")Integer id) -->
	<delete id="delById">
	delete from smbms_provider where id=#{id}
	</delete>


添加 
<!-- int add(Provider provider) -->
	<insert id="add" parameterType="Provider">
	insert into smbms_provider(proCode,proName,
		proDesc,proContact,
		proPhone,proAddress,
		proFax,createdBy,
		creationDate,
		modifyDate,modifyBy)
	values(#{proCode},#{proName},#{proDesc},#{proContact},#{proPhone},#{proAddress},
           #{proFax},#{createdBy},#{creationDate},#{modifyDate},#{modifyBy})
	</insert>
	
 修改(虽然没有说按什么修改 但是你应该知道是根据id进行修改)
 <!-- int update(Provider provider) -->
	<update id="update" parameterType="Provider">
	update smbms_provider
		set
		<trim suffixOverrides=",">
			<if test="proCode != null">
				proCode = #{proCode},
			</if>
			<if test="proName != null">
				proName = #{proName},
			</if>
			<if test="proDesc != null">
				proDesc = #{proDesc},
			</if>
			<if test="proContact !=null">
				proContact = #{proContact},
			</if>
			<if test="proPhone != null">
				proPhone = #{proPhone},
			</if>
			<if test="proAddress != null">
				proAddress = #{proAddress},
			</if>
			<if test="proFax != null">
				proFax = #{proFax},
			</if>
			<if test="userRole != null and userRole > 0">
				userRole = #{userRole},
			</if>
			<if test="modifyBy != null">
				modifyBy = #{modifyBy},
			</if>
			<if test="modifyDate != null">
				modifyDate = #{modifyDate},
			</if>
			<if test="idPicPath != null">
				idPicPath = #{idPicPath}
			</if>
		</trim>
		where id = #{id}
	</update>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值