• MyBatis Mapper 文件例子


    转载: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>

  • 相关阅读:
    scanf与scanf_s的区别
    C语言输出时的各种%
    Windows下配置OpenGL环境
    C#高级进阶--重写函数
    Linux下安装国际版QQ (转)
    Linux Vim不明原因卡死解决办法
    iCamera App Kit 使用说明
    usb2.0高速视频采集之68013A寄存器配置说明
    iSensor APP 之 摄像头调试 OV5642 续集2
    iSensor APP 之 摄像头调试 OV9655 测试之二
  • 原文地址:https://www.cnblogs.com/wenlj/p/4641991.html
Copyright © 2020-2023  润新知