转载:http://blog.csdn.net/ppby2002/article/details/20611737

<?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="com.lee.UserMapper">

<!--返回单一对象-->
  <select id="selectName" resultType="com.lee.User">
    <![CDATA[ 
      select user_name userName from user_table where user_id = #{userId}     
    ]]>
  </select>

<!--返回结果格式 Map<字段名称,字段值>-->
  <select id="selectByName" resultType="hashMap" parameterType="string">
      <![CDATA[
          SELECT * from user_table where user_name=#{userName}
     ]]>      
  </select>
  
  <!--调用存储过程-->
  <select id="selectUserFromStoreProcedure" statementType="CALLABLE">
    <![CDATA[
       {call my_pack.my_proc(
           #{userId,mode=IN,jdbcType=VARCHAR,javaType=string},
           #{userName,mode=OUT,jdbcType=FLOAT,javaType=string})}
       ]]>
  </select>

<!--返回List<User>-->
  <select id="selectUsers" resultType="com.lee.User">   
        <![CDATA[ 
      select user_id userId, user_name userName from user_table
     ]]>
  </select>
  
  <!--重用sql-->
  <sql id="subQuery">   
      <![CDATA[  
         WITH MY_SUB_QUERY as (
        select 'lee' as user_name, '1' as user_id from dual 
        union select 'lee1' ,'2' from dual
      )
    ]]>
  </sql>
  
  <!--动态sql-->
  <sql id="selectOther">
    <include refid="subQuery" />        
        <![CDATA[ 
        SELECT t.other_id otherId, t.other_name otherName, t.other_flag otherFlag FROM OtherTable t
            INNER JOIN MY_SUB_QUERY mapper ON mapper.user_id = t.user_id 
     ]]>
    <if test="filterFlag==true">  
            <![CDATA[
              and t.other_flag = 'Y'
            ]]>
    </if>
    <!--
    另一个if段
    <if test="flag1==true">  
      ...
    </if>
    -->
<!--
使用choose语句,flag1是从mapper方法中传递的参数,如 @Param("flag1") String flag1
    <choose>
      <when test="flag1 == 'Y'">
        t1.flag1 AS flag
        FROM table1 t1
      </when>
      <otherwise>
        t2.flag2 AS flag
        FROM table2 t2
      </otherwise>
    </choose>  
  -->
  </sql>
  
  <!--返回数字-->
  <select id="selectCount" resultType="java.lang.Long">       
        <![CDATA[   
          SELECT count(*) FROM user_table
        ]]>
  </select>
  
  <!--Map参数, 格式Map<参数名称,参数值>-->
  <select id="selectUser"  parameterType="java.util.HashMap" resultType="com.lee.User">
    <![CDATA[     
            SELECT user_id userId, user_name userName from user_table
          where user_id=#{userId} and user_name = #{userName}
        ]]>
  </select>
</mapper>

--------------------------------------------------这个分割线的作用是要显示下边的Java对象例子--------------------------------------------------
public class User {
  private int userId;
  private String userName;
  public String getUserId() {return userId}
  public void setUserId(int userId) {this.userId=userId}
  public String getUserName() {return userName}
  public void setUserName(String userName) {this.userName=userName}
}
--------------------------------------------------这个分割线的作用是要显示下边的Mapper对象例子--------------------------------------------------
@Repository
public interface UserMapper {  
  public User selectName(@Param("userId") String userId);
  public List<Map<String,Object>> selectByName(@Param("userName")String userName);
  <!--Map<字段名称,字段值>-->
  public void selectUserFromStoreProcedure(Map<String,Object> map);
  public List<User> selectUsers();
  public OtherUser selectOther();
  public int selectCount();
  public User selectUser(Map<String,Object> map);
}
--------------------------------------------------这个分割线的作用是要显示另一些配置例子--------------------------------------------------
<!--用映射配置查询sql-->
<resultMap id="UserMap" type="com.lee.User">
  <result column="user_id" property="userId"/>
  <result column="user_name" property="userName"/>
</resultMap>
<select id="selectName" resultMap="UserMap">
  <![CDATA[ 
    select user_name from user_table where user_id = #{userId}     
  ]]>
</select>

<!--重用映射配置并连接到其它结果集查询-->
<resultMap id="OtherUserMap" type="com.lee.OtherUser" extends="UserMap">
    <!--多个查询条件用逗号隔开,如userId=user_id,userName=user_name-->
    <collection property="ownedItems" select="selectItems" column="userId=user_id"/> 
</resultMap>
<select id="selectItems" resultType="com.lee.UserItem">   
  SELECT * FROM user_item_table WHERE user_id = #{userId}
</select>

public class OtherUser extends User {
  private List<UserItem> ownedItems;
  public List<UserItem> getOwnedItems() {return ownedItems}
  public void setOwnedItems(List<UserItem> userId) {this.ownedItems=ownedItems}
}
--------------------------------------------------这个分割线的作用是要显示另一个重用子查询配置例子--------------------------------------------------
<mapper namespace="mapper.namespace">
  <sql id="selectTable1">
    <![CDATA[       
      select f1, f2, f3 from table1 where 1=1
    ]]> 
  </sql>
  <select id="getStandardAgents" resultMap="StandardAgent">   
     <include refid="mapper.namespace.selectTable1"/>
     <![CDATA[             
      and f1 = 'abc'
     ]]>      
  </select>
</mapper>
--------------------------------------------------这个分割线的作用是要显示insert/update/delete配置例子--------------------------------------------------
<!--从Oracle序列中产生user_id, jdbcType=VARCHAR用于插入空值-->
<insert id="insertUser" parameterType="com.lee.User">
  <selectKey keyProperty="user_id" resultType="string" order="BEFORE">
    select db_seq.nextval as user_id from dual
  </selectKey>
  INSERT INTO 
    user_table(
      user_id,
      user_name,
    ) VALUES(
      #{user_id},
      #{user_name,jdbcType=VARCHAR}
    )
</insert>

<update id="updateUser" parameterType="com.lee.User">
  UPDATE user_table
    SET user_name = #{userName,jdbcType=VARCHAR},
  WHERE
    user_id = #{userId}
</update>

<delete id="deleteUser" parameterType="com.lee.User">
  DELETE user_table WHERE user_id = #{userId} 
</delete>

MyBatis Mapper 文件例子的更多相关文章

  1. MyBatis mapper文件中的变量引用方式#{}与${}的差别

    MyBatis mapper文件中的变量引用方式#{}与${}的差别 #{},和 ${}传参的区别如下:使用#传入参数是,sql语句解析是会加上"",当成字符串来解析,这样相比于$ ...

  2. [DB][mybatis]MyBatis mapper文件引用变量#{}与${}差异

    MyBatis mapper文件引用变量#{}与${}差异 默认,使用#{}语法,MyBatis会产生PreparedStatement中.而且安全的设置PreparedStatement參数,这个过 ...

  3. intellij idea 插件开发--快速定位到mybatis mapper文件中的sql

    intellij idea 提供了openApi,通过openApi我们可以自己开发插件,提高工作效率.这边直接贴个链接,可以搭个入门的demo:http://www.jianshu.com/p/24 ...

  4. mybatis mapper文件sql语句传入hashmap参数

    1.怎样在mybatis mapper文件sql语句传入hashmap参数? 答:直接这样写map就可以 <select id="selectTeacher" paramet ...

  5. ][mybatis]MyBatis mapper文件中的变量引用方式#{}与${}的差别

    转自https://blog.csdn.net/szwangdf/article/details/26714603 MyBatis mapper文件中的变量引用方式#{}与${}的差别 默认情况下,使 ...

  6. MyBatis mapper文件中使用常量

    MyBatis mapper文件中使用常量 Java 开发中会经常写一些静态常量和静态方法,但是我们在写sql语句的时候会经常用到判断是否等于 //静态类 public class CommonCod ...

  7. Mybatis mapper文件占位符设置默认值

    如果要设置占位符默认值的话:需要进行 设置 org.apache.ibatis.parsing.PropertyParser.enable-default-value 属性为true启用占位符默认值处 ...

  8. 自己挖的坑自己填--Mybatis mapper文件if标签中number类型及String类型的坑

    1.现象描述 (1)使用 Mybatis 在进行数据更新时,大部分时候update语句都需要通过动态SQL进行拼接.在其中,if标签中经常会有 xxx !='' 这种判断,若 number 类型的字段 ...

  9. Mybatis mapper文件中的转义方法

    在mybatis中的sql文件中对于大于等于或小于等于是不能直接写?=或者<=的,需要进行转义,目前有两种方式: 1.通过符号转义: 转义字符       <     <   小于号 ...

随机推荐

  1. FastDFS4 + Ubuntu12安装及部署

    1. 安装libevent 如果需要使用http进行下载,需要安装这个组件. 2. 安装fastdfs 3. 配置fastdfs 4. 安装nginx 问题4.1:安装nginx启动后访问原先资源UR ...

  2. AngularJS(15)-依赖注入

    AngularJS 依赖注入 什么是依赖注入 wiki 上的解释是:依赖注入(Dependency Injection,简称DI)是一种软件设计模式,在这种模式下,一个或更多的依赖(或服务)被注入(或 ...

  3. jquery实现密码框显示提示文字

    jquery实现密码框提示文字的功能. 代码:    <html>  <head>   3 <title>登录-jquery实现密码框显示文字-www.jbxue. ...

  4. select标签用法

    <select name="type" class="textarea" onchange='bbbb(this.value)' > <opt ...

  5. 使用VisualSVN Server自动发布站点

    使用VisualSVN Server自动发布站点可以节省大量的发布时间. 适用于项目测试版本 通常一个项目在测试阶段会遇到以下问题 1.开发人员修改缺陷要实时反应到测试环境 2.项目经理想让客户及时看 ...

  6. 深入剖析——float之个人见解

    浮动的原本作用仅仅是为了实现文字的环绕效果. 以下分别是html与css代码,显示效果如下图.因为两个div使用了float浮动属性,所以脱离了标准文档流.让父元素撑开高度,我们需要清除浮动. < ...

  7. session 测试用例详解

    http协议是WEB服务器与客户端(浏览器)相互通信的协议,它是一种无状态协议.所谓无状态,指的是不会维护http请求数据,http请求是独立的,非持久的.而越来越复杂的WEB应用,需要保存一些用户状 ...

  8. [css]兼容性

    div +input 输入框 , 在微信中  有问题 块级元素 行内元素

  9. HTML & XML 转义字符

    HTML & XML 转义字符 HTML中<, >,&等有特殊含义,(前两个字符用于链接签,&用于转义),不能直接使用.使用这三个字符时,应使用它们的转义序列,如下 ...

  10. 模仿开发H5游戏,看你有多色

    开发记录 前言 之前跟着慕课网学习开发H5小游戏开心鱼,勾起我的兴趣. 在写代码的过程中,不怎么会遇到问题.虽然代码是亲手敲出来的,但是由于并没有对游戏的整体思路,所以并不知道开发与优化的过程. 为了 ...