MyBatis 向Sql语句中动态传参数·动态SQL拼接

在动态传递参数的时候,需要用到OGNL表达式,不懂的童鞋可以下去百度,这里制作一个简要的介绍

在向XML文件传递参数的时候,需要用到sqlSession.selectList("Message.queryMessageList",message); message就是你要传递的参数。一般来说,这个message是一个对象,因为这里只能传递一个参数,而对象可以将很多参数封装起来。

XML文件接收到参数以后,会动态的执行Sql语句,但是具体要怎么传递参数呢,这就需要用到<if>标签来判断,但是在判断时,就需要用到OGNL表达式。关于OGNL我这里只列出基本的用法,见下图:

tips:OGNL中可以直接调用java中的函数和语法。

直接贴出例子:<if test="command!=null&amp;&amp;!&quot;&quot;.equals(command.trim())"> and COMMAND=#{command}</if>

&amp;是&的转义表达,&quot;是 " 的转义表达,#{command}表示这个值由Bean中的command表示。

这个表达式用java写出来就是:if (command != null && !"".equals(command.trim())) { * sql.append(" and COMMAND=?"); } ?表示被代替的位置。

 具体xml:

<?xml version="1.0" encoding="UTF-8"?>
<!--

       Copyright 2009-2016 the original author or authors.

       Licensed under the Apache License, Version 2.0 (the "License");
       you may not use this file except in compliance with the License.
       You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing, software
       distributed under the License is distributed on an "AS IS" BASIS,
       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       See the License for the specific language governing permissions and
       limitations under the License.

-->
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="Message">

  <resultMap type="com.daley.bean.Message" id="MessageResult">
    <id column="ID" jdbcType="INTEGER" property="id"/>
    <result column="COMMAND" jdbcType="VARCHAR" property="command"/>
    <result column="DESCRIPTION" jdbcType="VARCHAR" property="description"/>
    <result column="CONTENT" jdbcType="VARCHAR" property="content"/>
  </resultMap>

  <select id="queryMessageList" parameterType="com.daley.bean.Message" resultMap="MessageResult">
    select ID,COMMAND,DESCRIPTION,CONTENT from message where 1=1
    <if test="command!=null&&!"".equals(command.trim())"> and COMMAND=#{command}</if>
    <if test="description!=null&&!"".equals(description.trim())"> and DESCRIPTION like '%' #{description} '%'</if>
  </select>

</mapper>

Bean文件

package com.daley.bean;
/**
 * @author Daley
 * Date 2016-11-28
 * project_name MicroMessage
 * 与消息表对应的实体类
 */
public class Message {
	private String id;
	private String command;
	private String description;
	private String content;
	public Message(){
		
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getCommand() {
		return command;
	}
	public void setCommand(String command) {
		this.command = command;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
}


这是一个简单的学习项目,如果想看具体的文件,可以访问我的github下载。这篇文章是承接 : 

MyBatis 学习入门·基本配置·项目实例 

http://blog.csdn.net/sinat_32873711/article/details/53397594

XML标签

GitHub DemoZip地址https://github.com/DaleyChao/MicroMessage/archive/7008032e9381cea8d197dabff9625bb339713d1c.zip

GitHub项目地址:(SQL语句拼接功能完善https://github.com/DaleyChao/MicroMessage/tree/7008032e9381cea8d197dabff9625bb339713d1c

欢迎关注公众号获取学习资料(后台留言):后端开发技术

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

西海幼鸟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值