mybatis的XML文件格式

仅供自己参考,用于复制粘贴

基础mybatis的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">

<mapper namespace="com.itheima.mapper.ClassMapper">
    <resultMap id="class" type="com.itheima.pojo.ClassInfo">
        <result column="id" property="cid"/>
        <result column="cname" property="cname"/>
    </resultMap>

    <resultMap id="stu" type="com.itheima.pojo.Student">
        <result column="sid" property="id"/>
        <result column="sname" property="name"/>
        <result column="age" property="birthday"/>
        <result column="sex" property="sex"/>
        <result column="cid" property="cid"/>
    </resultMap>

    <insert id="clsAdd">
        insert into cls(cname) value (#{cname});
    </insert>

    <select id="findById" resultMap="class">
        select *
        from cls
        where id = #{id};
    </select>

    <select id="findAllCls" resultMap="class">
        select *
        from cls;
    </select>

    <select id="findStus" resultMap="stu">
        SELECT student.sid, student.sname, student.age, student.sex
        FROM student
        WHERE student.cid = #{cid};
    </select>
</mapper>



动态sql,懒加载的使用

<?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.itheima.mapper.StudentMapper">
    <resultMap id="stu" type="com.itheima.pojo.Student">
        <result column="sid" property="id"/>
        <result column="sname" property="name"/>
        <result column="age" property="birthday"/>
        <result column="sex" property="sex"/>
        <result column="cid" property="cid"/>

        <association property="classInfo"
                     javaType="com.itheima.pojo.ClassInfo"
                     fetchType="lazy"
                     column="cid"
                     select="com.itheima.mapper.ClassMapper.findById"/>

        <collection property="course"
                    javaType="java.util.List"
                    fetchType="lazy"
                    column="sid"
                    select="com.itheima.mapper.CourseMapper.findCourseWithScore"/>
        <!--                     select="SELECT sc.courseid from student, cls, sc where student.cid = cls.id and student.sid = sc.studentid"/>-->

        <!--        <collection property="course"-->
        <!--                    javaType="java.util.List"-->
        <!--                    fetchType="lazy"-->
        <!--                    column="courseid"-->
        <!--                    select="com.itheima.mapper.CourseMapper"/>-->
    </resultMap>

    <update id="updateStu">
        update student
        <set>
            <if test="name != null and name != ''">
                sname = #{name},
            </if>
            <if test="birthday != null">
                age = #{birthday},
            </if>
            <if test="sex != null and sex != ''">
                sex = #{sex},
            </if>
            <if test="cid != 0">
                cid = #{cid},
            </if>
        </set>
        where sid = #{id};
    </update>


    <delete id="deleteById">
        delete
        from student
        where sid = #{id};
    </delete>

    <delete id="deleteByIds">
        delete from student where sid in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </delete>

    <select id="findAllStuWithClass" resultMap="stu">
        select *
        from student,
             cls
        where student.cid = cls.id;
    </select>

    <select id="findStuById" resultMap="stu">
        select *
        from student,
             cls
        where sid = #{id}
          and student.cid = cls.id;
    </select>

    <select id="findStuByIds" resultMap="stu">
        select *
        from student,
        cls
        where student.cid = cls.id and
        sid in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </select>

    <select id="findAllStuWithClassCourse" resultMap="stu">
        select *
        from student;
    </select>

    <select id="selectInfoByStr" resultMap="stu">
        select * from student
        <where>
            <if test="str != null">or sid like #{str}</if>
            <if test="str != null and str != ''">or sname like #{str}</if>
            <if test="str != null and str != ''">or sex like #{str}</if>
<!--            <if test="str != null">or age like #{str}</if>-->
        </where>
    </select>

    <select id="selectByPageAndCondition" resultMap="stu">
        select *
        from student
        <where>
            <if test="student!= null and student.name != null and student.name != ''">
                sname like #{student.name}
            </if>
            <if test="student!= null and student.sex != null and student.sex != ''">
                and sex like #{student.sex}
            </if>
        </where>
        limit #{begin}, #{size};
    </select>
    <select id="findStusByCoId" resultMap="stu">
        SELECT *
        FROM (SELECT * FROM student, sc WHERE student.sid = sc.studentid) t1, course
        WHERE t1.courseid = course.id AND t1.courseid = #{id}
    </select>

    <insert id="addStuInfo" useGeneratedKeys="true" keyProperty="id">
        insert into student(sname, age, sex, cid)
        values (#{name}, #{birthday}, #{sex}, #{cid})
    </insert>
</mapper>

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值