在动态传递参数的时候,需要用到OGNL表达式,不懂的童鞋可以下去百度,这里制作一个简要的介绍
在向XML文件传递参数的时候,需要用到sqlSession.selectList("Message.queryMessageList",message); message就是你要传递的参数。一般来说,这个message是一个对象,因为这里只能传递一个参数,而对象可以将很多参数封装起来。
XML文件接收到参数以后,会动态的执行Sql语句,但是具体要怎么传递参数呢,这就需要用到<if>标签来判断,但是在判断时,就需要用到OGNL表达式。关于OGNL我这里只列出基本的用法,见下图:
tips:OGNL中可以直接调用java中的函数和语法。
直接贴出例子:<if test="command!=null&&!"".equals(command.trim())"> and COMMAND=#{command}</if>
&是&的转义表达,"是 " 的转义表达,#{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