您的位置:首页 > 数据库

动态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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: