Mybatis SQL参数、条件、日志

参数:

通常情况下,Mybatis的mapper文件中Select、Inser、Update和Delete只接收一个参数,并用parameterType指定。

要指定多个参数,可以将多个参数加入到一个hashmap,然后将hashmap作为参数传入。

另外的方法是使用@Param注解标注多个参数,例如:

List<Contact> selectQuery(@Param(value="contact") Contact contact,@Param(value="start") int start,@Param("size") int size);
其中参数contact类型为Contact,mapper对于的xml配置为:

<select id="selectQuery" resultType="Contact">
    	select * from contacts 
    	<where>
    		<if test="contact.name!=null">
    			name like concat('%',#{contact.name},'%')
    		</if>
    		<if test="contact.email!=null">
    			and email like concat('%',#{contact.email},'%')
    		</if>
    	</where>
    	limit #{start},#{size}
    </select>
因为多个参数的存在,引用name属性必须指定contact.name。

条件:

if:条件判断

官方文档代码片段:

<select id="findActiveBlogWithTitleLike" parameterType="Blog"
		resultType="Blog">
	SELECT * FROM BLOG
	WHERE state = ‘ACTIVE’
	<if test="title != null">
		AND title like #{title}
	</if>
</select>

choose:多个条件判断,包含when otherwise

官方文档代码片段:

<select id="findActiveBlogLike" parameterType="Blog" resultType="Blog">
	SELECT * FROM BLOG WHERE state = ‘ACTIVE’
	<choose>
		<when test="title != null">
			AND title like #{title}
		</when>
		<when test="author != null and author.name != null">
			AND author_name like #{author.name}
		</when>
		<otherwise>
			AND featured = 1
		</otherwise>
	</choose>
</select>

where:构造where条件片段

官方文档代码片段:

<select id="findActiveBlogLike" parameterType="Blog" resultType="Blog">
	SELECT * FROM BLOG
	<where>
		<if test="state != null">
			state = #{state}
		</if>
		<if test="title != null">
			AND title like #{title}
		</if>
		<if test="author != null and author.name != null">
			AND author_name like #{author.name}
		</if>
	</where>
</select>
会这3个基本上就好了。。


日志:

log4j.rootLogger=DEBUG, stdout   
   
log4j.appender.stdout=org.apache.log4j.ConsoleAppender   
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout   
log4j.appender.stdout.layout.ConversionPattern=%c{1} - %m%n   
   
log4j.logger.java.sql.PreparedStatement=DEBUG  






  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dyyaries

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值