1. 程式人生 > >MyBatis 向Sql語句中動態傳引數·動態SQL拼接

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下載。這篇文章是承接 : 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