动态SQL
2015-07-08 00:40
316 查看
<select id="queryMessageList" resultMap="messageResult" parameterMap="messageParameter"> SELECT id,command,description,content FROM message <where> <if test="command != null and !!"".equals(command.trim())"> AND command = #{command} </if> <if test="description != null and !"".equals(description.trim())"> AND description LIKE CONCAT('%', #{description}, '%') </if> </where> </select>
<!--删除--> <delete id="deleteMessage" parameterType="int"> DELETE FROM message WHERE id = #{_parameter} </delete> <delete id="deleteMessages" parameterType="java.util.List"> DELETE FROM message WHERE id IN ( <foreach collection="list" item="item" separator=","> #{item} </foreach> ) </delete>
include的使用
<sql id="messageColumns"> id,command,description,content </sql> <!--查询--> <select id="queryMessageList" resultMap="messageResult" parameterMap="messageParameter"> SELECT <include refid="messageColumns"/> FROM message <where> <if test="command != null and !!"".equals(command.trim())"> AND command = #{command} </if> <if test="description != null and !"".equals(description.trim())"> AND description LIKE CONCAT('%', #{description}, '%') </if> </where> </select>
一对多关系
package com.imooc.bean; import java.util.List; /** * 与指令表对应的实体类 */ public class Command { /** * 主键 */ private String id; /** * 指令名称 */ private String name; /** * 描述 */ private String description; /** * 一条指令对应的自动回复内容列表 */ private List<CommandContent> contentList; ...... }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="Command"> <resultMap type="com.imooc.bean.Command" id="Command"> <id column="C_ID" jdbcType="INTEGER" property="id"/> <result column="NAME" jdbcType="VARCHAR" property="name"/> <result column="DESCRIPTION" jdbcType="VARCHAR" property="description"/> <collection property="contentList" resultMap="CommandContent.Content"/> </resultMap> <select id="queryCommandList" parameterType="com.imooc.bean.Command" resultMap="Command"> select a.ID C_ID,a.NAME,a.DESCRIPTION,b.ID,b.CONTENT,b.COMMAND_ID from COMMAND a left join COMMAND_CONTENT b on a.ID=b.COMMAND_ID <where> <if test="name != null and !"".equals(name.trim())"> and a.NAME=#{name} </if> <if test="description != null and !"".equals(description.trim())"> and a.DESCRIPTION like '%' #{description} '%' </if> </where> </select> </mapper>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="CommandContent"> <resultMap type="com.imooc.bean.CommandContent" id="Content"> <id column="ID" jdbcType="INTEGER" property="id"/> <result column="CONTENT" jdbcType="VARCHAR" property="content"/> <result column="COMMAND_ID" jdbcType="VARCHAR" property="commandId"/> </resultMap> </mapper>
其他常用标签
定义SQL语句 insert、delete、update、select
配置java与结果集映射 resultMap
控制SQL拼接 foreach、if、choose
格式化输出 where、set、trim
配置关联关系 collection、association
定义常量 sql
引用常量 include
相关文章推荐
- mongoDB学习之旅(一)
- Memcached-session-manager原理
- MongoDB 那些坑
- Servlet手把手教你做一个初级程序
- php-redis中的sort排序函数总结
- Mysql存储过程中游标的用法实例
- MySQL的mysqldump工具用法详解
- MySQL下PID文件丢失的相关错误的解决方法
- MySQL中数据类型相关的优化办法
- 检测MySQL的表的故障的方法
- MySQL的一些常用的SQL语句整理
- MySQL基本操作语句小结
- SQL Server中通配符的使用示例
- SQL Server中使用Trigger监控存储过程更改脚本实例
- SQL Server查询数据库中表使用空间信息实现脚本
- SQL Server根据分区表名查找所在的文件及文件组实现脚本
- SQL Server把某个字段的数据用一条语句转换成字符串
- 关于oracle对null排序的问题
- 完全卸载Oracle实现方法
- windows下Oracle常用创建命令