UserMapper.xml 4.7 KB
Newer Older
1 2
<?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">
爱吃血肠's avatar
爱吃血肠 已提交
3
<mapper namespace="com.yingjun.ssm.dao.UserDao">
4
    <!-- TUser的resultMap,column是给数据库列起的别名,它对应property类的属性-->
爱吃血肠's avatar
爱吃血肠 已提交
5
    <resultMap id="result_TUser_Map" type="com.yingjun.ssm.entity.User">
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
        <id column="id" property="id" />
        <result column="password" property="password" />
        <result column="name" property="name" />
        <result column="email" property="email" />
    </resultMap>

    <!-- 数据库中表名为:t_user的列名,as前是数据库的列明,as后是列的别名用于映射成实体类中的属性,需要注意的是别名必须与resultMap中的column别名一致 -->
    <sql id="t_user_Column">
        t_user.id as id
        ,t_user.password as password
        ,t_user.name as name
        ,t_user.email as email
    </sql>

    <!--获得类名为:TUser对应的数据库表的数据总行数 -->
    <select id="getTUserRowCount" resultType="java.lang.Long">
        select count(id) from t_user
    </select>
    <!-- 获得类名为:TUser对应数据库中表的数据集合 -->
    <select id="selectTUser" resultMap="result_TUser_Map">
        select 
        <include refid="t_user_Column" /> 
        from t_user
    </select> 

    <!-- 获得一个TUser对象,以参数TUser对象中不为空的属性作为条件进行查询-->
爱吃血肠's avatar
爱吃血肠 已提交
32
    <select id="selectTUserByObj" parameterType="com.yingjun.ssm.entity.User" resultMap="result_TUser_Map">
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
        select 
            <include refid="t_user_Column" /> 
        from t_user
        <where>
            <if test="email != null "> and t_user.email = #{email}</if>
            <if test="name != null "> and t_user.name = #{name}</if>
            <if test="password != null "> and t_user.password = #{password}</if>
            <if test="id != null "> and t_user.id = #{id}</if>
        </where>
    </select> 

    <!-- 通过TUser的id获得对应数据库中表的数据对象-->
    <select id="selectTUserById" parameterType="java.lang.Long" resultMap="result_TUser_Map">
        select 
            <include refid="t_user_Column" /> 
        from t_user
        where t_user.id = #{id}
    </select> 

    <!-- 将TUser插入到对应数据库的表中,包括属性值为null的数据-->

爱吃血肠's avatar
爱吃血肠 已提交
54
    <insert id="insertTUser" parameterType="com.yingjun.ssm.entity.User">
55 56 57 58 59
        insert into t_user(id,password,name,email) 
        values(#{id},#{password},#{name},#{email})
    </insert>

    <!-- 将TUser中属性值不为null的数据,插入到对应数据库的表中-->
爱吃血肠's avatar
爱吃血肠 已提交
60
    <insert id="insertNonEmptyTUser" parameterType="com.yingjun.ssm.entity.User">
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        insert into t_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="password != null">password,</if>
            <if test="name != null">name,</if>
            <if test="email != null">email,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null"> #{id},</if>
            <if test="password != null"> #{password},</if>
            <if test="name != null"> #{name},</if>
            <if test="email != null"> #{email},</if>
        </trim>
    </insert>

    <!-- 将TUser批量插入到对应数据库的表中-->
    <insert id="insertTUserByBatch" parameterType="ArrayList">
        insert into t_user(id,password,name,email) values
        <foreach collection="list" item="item" index="index" separator="," >
            (#{item.id},#{item.password},#{item.name},#{item.email})
        </foreach>
    </insert>

    <!-- 通过TUser的id将数据库表中对应的数据删除-->
    <delete id="deleteTUserById" parameterType="java.lang.Long">
        delete from t_user
        where id = #{id}
    </delete>

    <!-- 通过TUser的id将TUser的数据更新到数据库中对应的表,包括值null的数据-->
爱吃血肠's avatar
爱吃血肠 已提交
91
    <update id="updateTUserById" parameterType="com.yingjun.ssm.entity.User">
92 93 94 95 96 97 98 99
        update t_user set
            password=#{password}
            ,name=#{name}
            ,email=#{email}
        where id=#{id}
    </update>

    <!-- 通过TUser的id将TUser中属性值不为null的数据更新到数据库对应的表中-->
爱吃血肠's avatar
爱吃血肠 已提交
100
    <update id="updateNonEmptyTUserById" parameterType="com.yingjun.ssm.entity.User">
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        update t_user
        <set>
            <if test="password != null">
                password=#{password},
            </if>
            <if test="name != null">
                name=#{name},
            </if>
            <if test="email != null">
                email=#{email},
            </if>
        </set>
        where id=#{id}
    </update>

</mapper>