1. 程式人生 > >mybatis之map.xml檔案的解讀

mybatis之map.xml檔案的解讀

<!-- type:返回的實體bean  id:查詢語句最終的resultMap對應 -->
<resultMap type="com.tvjoy.btop.webapp.entity.User" id="queryUserList">
<!-- id:主鍵    column:表字段的名稱或者別名    property:實體bean類裡面對應的變數名稱     (注:id這個標籤一定需要有,其次才能寫result標籤)-->
<id column="id" property="id"/>
<!-- result:返回結果    column:表字段的名稱或者別名    property:實體bean類裡面對應的變數名稱  jdbcType:資料庫對應的欄位型別  -->
<result column="userName" property="userName" jdbcType="VARCHAR"/>
<result column="age" property="age" jdbcType="VARCHAR"/>
<!--collection:多表查詢時使用     property:實體bean類裡面對應的變數名稱   javaType:定義變數的型別   -->
<collection property="role" javaType="com.tvjoy.btop.webapp.entity.Role">
<id column="roleId" property="id"/>
<result column="roleName" property="roleName" jdbcType="VARCHAR"/>
<result column="userId" property="userId" jdbcType="VARCHAR"/>
</collection>

</resultMap>

 <!-- select :查詢語句   id:對應的maper介面的方法名稱   parameterType:入參的型別     resultMap:返回的resultMap的ID-->  
<select id="findUserRoleById" parameterType="int" resultMap="queryUserList">
select
u.user_id id,u.user_name userName,u.user_age age ,
r.id roleId,r.role_name roleName,r.user_Id userId
from 
t_user u
left join
 t_role r
 on
 u.user_id=r.id
</select>

    <resultMap id="BaseResultMap" type="com.sica.domain.User">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="username" property="username" jdbcType="VARCHAR"/>
        <result column="password" property="password" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
      id, username, password
    </sql>


    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from user
        where id = #{id,jdbcType=VARCHAR}
    </select>


    <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    delete from user
    where id = #{id,jdbcType=VARCHAR}
    </delete>


    <insert id="insert" parameterType="com.sica.domain.User">
    insert into user (id, username, password
      )
    values (#{id,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}
      )
    </insert>


    <insert id="insertSelective" parameterType="com.sica.domain.User">
        insert into user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="username != null">
                username,
            </if>
            <if test="password != null">
                password,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=VARCHAR},
            </if>
            <if test="username != null">
                #{username,jdbcType=VARCHAR},
            </if>
            <if test="password != null">
                #{password,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>


    <update id="updateByPrimaryKeySelective" parameterType="com.sica.domain.User">
        update user
        <set>
            <if test="username != null">
                username = #{username,jdbcType=VARCHAR},
            </if>
            <if test="password != null">
                password = #{password,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{id,jdbcType=VARCHAR}
    </update>


    <update id="updateByPrimaryKey" parameterType="com.sica.domain.User">
    update user
    set username = #{username,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR}
    where id = #{id,jdbcType=VARCHAR}
  </update>