mybatis的mapper.xml标签使用

mybatis的mapper.xml标签使用


//namespace命名空间,对应一个mapper类

<mapper namespace="com.blog.mapper.BlogsMapper">

//resultMap 一个基础返回对象,column是数据库的表字段名,property是对应的实体类字段名,jdbcType是字段的数据库类型
//也可以做一个复杂的返回对象,通过包装
    <resultMap id="resultBaseMap" type="com.blog.mapper.BlogsMapper">
        <id column="blogid" property="blogid" jdbcType="VARCHAR"/>
        <result column="userid" property="userid" jdbcType="VARCHAR"/>
        <result column="title" property="title" jdbcType="VARCHAR"/>
        <result column="blog_content" property="blog_content" jdbcType="VARCHAR"/>
        <result column="createtime" property="createtime" jdbcType="DATETIME"/>
        <result column="updatetime" property="updatetime" jdbcType="DATETIME"/>
    </resultMap>

//复杂返回对象
    <resultMap id="resultBaseMap" type="com.blog.pojo.Blogs">
        <id column="blogid" property="blogid" jdbcType="VARCHAR"/>
        <result column="userid" property="userid" jdbcType="VARCHAR"/>
        <result column="title" property="title" jdbcType="VARCHAR"/>
        <result column="blog_content" property="blog_content" jdbcType="VARCHAR"/>
        <result column="createtime" property="createtime" jdbcType="DATETIME"/>
        <result column="updatetime" property="updatetime" jdbcType="DATETIME"/>
        //association 插入一个对象
        //collection  这个用法跟上面的差不多,只不过这个是list
        <association property="commentContent" resultMap="commentContent"/>
    </resultMap>

//-----------------------------------------------

    <resultMap id="commentContent" type="com.blog.pojo.CommentContent">
        <id column="commentid" property="commentid" jdbcType="VARCHAR"/>
        <result column="blogid" property="blogid" jdbcType="VARCHAR"/>
        <result column="userid" property="userid" jdbcType="VARCHAR"/>
        <result column="commentContent" property="commentContent" jdbcType="VARCHAR"/>
        <result column="createtime" property="createtime" jdbcType="DATETIME"/>
    </resultMap>


    <insert id="add" >
        insert into blogs(blogid,userid,title,blog_content,createtime,updatetime)
        values (#{blogid},#{userid},#{title},#{blog_content},#{createtime},#{updatetime})
    </insert>

    <select id="findAll" resultType="com.blog.pojo.Blogs" parameterType="java.lang.String">
        select blogid,userid,title,blog_content,createtime,updatetime from blogs
    </select>

    <select id="findOneById" resultType="com.blog.pojo.Blogs" parameterType="java.lang.String">
        select blogid,userid,title,blog_content,createtime,updatetime from blogs where blogid = #{blogid}
    </select>

    <update id="blogUpdate" >
        update blogs set title=#{title},blog_content=#{blog_content},updatetime=#{updatetime} where blogid=#{blogid}
    </update>

    <delete id="blogDelete">
        delete from blogs where blogid=#{blogid}
    </delete>

    <insert id="blogCommentAdd" >
        insert into blog_users(commentid,userid,blogid,commentContent,createtime) values (#{commentid},#{userid},#{blogid},#{commentContent},#{createtime})
    </insert>

    <select id="findAllComment" resultType="com.blog.pojo.Comments" parameterType="java.lang.String">
        select commentid,userid,blogid,commentContent,createtime from blog_users where blogid = #{blogid}
    </select>

    <delete id="blogCommentDelete">
        delete from blog_users where commentid=#{commentid}
    </delete>

    <select id="blogSearch" resultType="com.blog.pojo.Blogs" parameterType="java.lang.String">
        select blogid,userid,title,blog_content,createtime from blogs where title like "%"#{search}"%" or blog_content like "%"#{search}"%"
    </select>

    <select id="blogSearch2" resultType="com.blog.pojo.Blogs" parameterType="java.lang.String">
        select blogid,userid,title,blog_content,createtime from blogs where title like #{search} or blog_content like #{search}
    </select>


</mapper>

附数据库的表
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值