XXXMapper.xml写法

后续用到的还会继续补充

<?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">
<!--namespace :绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.eiji.dao.UserMapper">
    <!--select 查询语句-->
    <resultMap id="UserMap" type="user">
        <result column="id" property="id"></result>
        <result column="name" property="userName"></result>
        <result column="psw" property="password"></result>
    </resultMap>

    <select id="getUserList" resultMap="UserMap">
        select * from mybatis.user
    </select>

    <!--分页查询-->
    <select id="getUserListByLimit" parameterType="map" resultMap="UserMap">
        select * from mybatis.user limit #{startNum},#{pageSize}
    </select>

    <select id="getUserById" parameterType="int" resultMap="UserMap">
        select * from mybatis.user where id =#{id}
    </select>

    <insert id="insertUser" parameterType="user">
        insert into mybatis.user(id,psw) values (#{id},#{psw})
    </insert>


    <update id="updateUser" parameterType="user">
        update mybatis.user set psw = #{psw} where id =#{id}
    </update>

    <delete id="deleteUser" parameterType="int">
        delete from mybatis.user where id=#{id}
    </delete>





    <!--多对一的两种实现方式(详细的项目在mybatis 笔记1中)-->
    <!--方式一-->
    <resultMap id="studentMap" type="student">
        <result  property="id" column="id"></result>
        <result property="name" column="name"></result>
        <association property="teacher" column="tid" javaType="teacher" select="getTeacher"></association>
    </resultMap>

    <select id="getInfos" resultMap="studentMap">
        select * from mybatis.student
    </select>
    <select id="getTeacher" resultType="teacher">
        select * from mybatis.teacher where id=#{id}
    </select>

    <!--方式二-->
    <resultMap id="StudentMap" type="student">
        <result property="id" column="sid"></result>
        <result property="name" column="sname"/>
        <association property="teacher" javaType="teacher">
            <result property="name" column="tname"></result>
            <result property="id" column="id"/>
        </association>
    </resultMap>
    <select id="getInfos2" resultMap="StudentMap">
        select s.id sid,s.name sname,t.name tname
        from mybatis.student s,mybatis.teacher t
        where s.tid = t.id
    </select>




    <!--一对多的两种实现方式-->
    <!--方式一-->
    <resultMap id="TeacherStu" type="teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <collection property="students" ofType="student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>
    <select id="getTeachers" resultMap="TeacherStu">
        select s.id sid,s.name sname,t.id tid,t.name tname
        from mybatis.teacher t,mybatis.student s
        where t.id = s.tid and t.id = #{tid}
    </select>

    <!--方式二-->
    <resultMap id="TeacherStu2" type="teacher">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <collection property="students" javaType="ArrayList" ofType="student" select="getStu" column="id"/>
    </resultMap>
    <select id="getTeachers2" resultMap="TeacherStu2">
        select *
        from mybatis.teacher
        where id = #{id}
    </select>

    <select id="getStu" resultType="student">
        select * from mybatis.student where tid = #{tid}
    </select>







    <!--动态sql-->
    <select id="getBlobsIF" parameterType="map" resultType="blog">
        select * from mybatis.blog where 1=1
        <if test="title != null">
            and title = #{title}
        </if>
        <if test="author != null">
            and author = #{author}
        </if>
    </select>

    <select id="getBlogsWhere" parameterType="map" resultType="blog">
        select * from mybatis.blog
        <where>
            <choose>
                <when test="title != null">
                    title = #{title}
                </when>
                <when test="author != null">
                    author = #{author}
                </when>
                <otherwise>
                    views = #{views}
                </otherwise>
            </choose>
        </where>
    </select>

    <update id="updateBlog" parameterType="map">
        update mybatis.blog
        <set>
            <if test="title != null">
                title = #{title},
            </if>
            <if test="author != null">
                author = #{author},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="views != null">
                views = #{views},
            </if>
        </set>
        where id= #{id}
    </update>

    <sql id="foreachS">
        <if test="title != null">
            title = #{title}
        </if>
        <if test="author != null">
            and author = #{author}
        </if>
    </sql>
    <select id="getBlogByForeach01" parameterType="map" resultType="Blog">
        select * from mybatis.blog
        <where>
            <include refid="foreachS"></include>
        </where>
    </select>
    
    <select id="getBlogByForeach02" parameterType="map" resultType="Blog">
        select * from mybatis.blog
        <where>
            <foreach collection="ids" item="id" open="and (" separator="or" close=")">
                id = #{id}
            </foreach>
        </where>
    </select>
</mapper>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值