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.example.springboottest.dao.userDao">
    <!--简单方式级联查询-->
    <resultMap id="sampleSelectMap" type="com.example.springboottest.model.User">
        <id property="id" column="id"></id>
        <result property="userName" column="user_name"></result>
        <result property="userPassword" column="user_password"></result>
        <result property="userType" column="user_type"></result>
        <association property="userProject" column="id" javaType="com.example.springboottest.model.UserProject">
            <result property="stNumber" column="stNumber"></result>
            <result property="project" column="project"></result>
        </association>
        <collection property="userCourseList" ofType="com.example.springboottest.model.UserCourse">
            <result property="courseName" column="courseName"></result>
            <result property="courseScore" column="courseScore"></result>
        </collection>
    </resultMap>
    <select id="sampleSelect" resultMap="sampleSelectMap">
        select a.id,a.user_name,a.user_password,a.user_type,b.stNumber,b.project,c.courseName,c.courseScore from userList a left join userProject b on a.id=b.userId left join userCourse c on a.id=c.userId
    </select>
    <!--级联查询-->
    <resultMap id="getUserOneToOneAssociation" type="com.example.springboottest.model.User">
        <id property="id" column="id"></id>
        <association property="userProject" column="id" select="com.example.springboottest.dao.userProjectDao.findUserProjectByuserId"></association>
        <collection property="userCourseList" column="id" select="com.example.springboottest.dao.userProjectDao.findUserCourseListByuserId"></collection>
    </resultMap>
    <select id="getUserOneToOne" resultMap="getUserOneToOneAssociation">
        select * from userList
    </select>


<!--    <sql id="sqlselect">-->
<!--        id,user_name,user_password,user_type-->
<!--    </sql>-->
<!--    <select id="getAllUser" resultType="com.example.springboottest.model.User">-->
<!--        select <include refid="sqlselect" /> from userList-->
<!--    </select>-->

    <select id="getAllUserWhere" parameterType="string" resultType="com.example.springboottest.model.User">
        select * from userList
        <where>
            <if test="userType !=null and userType !=''">
                and user_type like '%${userType}%'
            </if>
        </where>
    </select>
<!--    <select id="getAllUserWhere" parameterType="string" resultType="com.example.springboottest.model.User">-->
<!--        select * from userList where 1=1-->
<!--        <choose>-->
<!--            <when test="userType!='' and userType!=null">-->
<!--                and user_type like concat('%',#{userType},'%')-->
<!--            </when>-->
<!--            <when test="userName!='' and userName!=null">-->
<!--                and user_name like concat('%',#{userName},'%')-->
<!--            </when>-->
<!--        </choose>-->
<!--    </select>-->

    <select id="getAllUser" resultType="com.example.springboottest.model.User">
        select * from userList
    </select>

    <insert id="insertUser" parameterType="com.example.springboottest.model.User" keyProperty="id" useGeneratedKeys="true">
        insert into userList (user_name,user_password,user_type) values (#{userName},#{userPassword},#{userType})
    </insert>

<!--    <update id="updateUser" parameterType="com.example.springboottest.model.User">-->
<!--        update userList set user_name=#{userName},user_password=#{userPassword},user_type=#{userType} where id=#{id}-->
<!--    </update>-->
    <update id="updateUser" parameterType="com.example.springboottest.model.User">
        update userList
        <set>
            <if test="userName!='' and userName!=null">
                user_name=#{userName},
            </if>
            <if test="userPassword!='' and userPassword!=null">
                user_password=#{userPassword},
            </if>
            <if test="userType!='' and userType!=null">
                user_type=#{userType},
            </if>

        </set>
        where id=#{id}
    </update>

    <delete id="deleteUser" parameterType="long">
        delete from userList where id in
        <foreach item="id" index="index" collection="deleteList" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员阿明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值