当前位置: 首页 > news >正文

MyBatis 向Sql语句中动态传参数#183;动态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

相关文章:

  • zabbix图形中文乱码的问题
  • 业务人员自助BI分析不够用,还要自助数据准备?
  • finnal 评论 II
  • MySQL字符串转日期类型
  • 行业动态
  • 新浪微博客户端(52)-长按或滑动显示表情
  • 基本概念学习(9005)---位段
  • 常用python机器学习库总结
  • 下拉框
  • AOP注解不起作用的debug结果
  • Unity应用架构设计(3)——构建View和ViewModel的生命周期
  • 动态链接库相关
  • java集合类
  • Autodesk Vault: 获取授权失败
  • 017,idea集成svn
  • CODING 缺陷管理功能正式开始公测
  • css系列之关于字体的事
  • C语言笔记(第一章:C语言编程)
  • Django 博客开发教程 16 - 统计文章阅读量
  • gf框架之分页模块(五) - 自定义分页
  • iOS | NSProxy
  • Java基本数据类型之Number
  • python学习笔记-类对象的信息
  • select2 取值 遍历 设置默认值
  • Vue 动态创建 component
  • 案例分享〡三拾众筹持续交付开发流程支撑创新业务
  • 开源地图数据可视化库——mapnik
  • 数据库写操作弃用“SELECT ... FOR UPDATE”解决方案
  • 小程序滚动组件,左边导航栏与右边内容联动效果实现
  • 积累各种好的链接
  • 树莓派用上kodexplorer也能玩成私有网盘
  • #Linux(Source Insight安装及工程建立)
  • #微信小程序(布局、渲染层基础知识)
  • %@ page import=%的用法
  • (C语言)fgets与fputs函数详解
  • (C语言)共用体union的用法举例
  • (libusb) usb口自动刷新
  • (pytorch进阶之路)扩散概率模型
  • (附源码)spring boot基于Java的电影院售票与管理系统毕业设计 011449
  • (规划)24届春招和25届暑假实习路线准备规划
  • (免费领源码)python#django#mysql校园校园宿舍管理系统84831-计算机毕业设计项目选题推荐
  • (南京观海微电子)——I3C协议介绍
  • (牛客腾讯思维编程题)编码编码分组打印下标题目分析
  • (转)MVC3 类型“System.Web.Mvc.ModelClientValidationRule”同时存在
  • (转)使用VMware vSphere标准交换机设置网络连接
  • (转)原始图像数据和PDF中的图像数据
  • .apk文件,IIS不支持下载解决
  • .gitignore文件设置了忽略但不生效
  • .mysql secret在哪_MYSQL基本操作(上)
  • .net core webapi 部署iis_一键部署VS插件:让.NET开发者更幸福
  • .net core使用ef 6
  • .NetCore部署微服务(二)
  • @JSONField或@JsonProperty注解使用
  • @基于大模型的旅游路线推荐方案
  • [AI]ChatGPT4 与 ChatGPT3.5 区别有多大