mybatis 动态sql详解

  1. 内容转自<a target=_blank href="http://haohaoxuexi.iteye.com/blog/1333271">转到原网页</a>
  2. <mapper namespace="com.tiantian.mybatis.model.BlogMapper">  
    <!--  新增记录  -->  
        <insert id="insertBlog" parameterType="Blog">  
            insert into t_blog(title,content,owner) values(#{title},#{content},#{owner})  
        </insert>  
    <!--  查询单条记录 -->  
        <select id="selectBlog" parameterType="int" resultType="Blog">  
            select * from t_blog where id = #{id}  
        </select>  
    <!--  修改记录   -->  
        <update id="updateBlog" parameterType="Blog">  
            update t_blog set title = #{title},content = #{content},owner = #{owner} where id = #{id}  
        </update>  
    <!--  查询所有记录,查询多条记录即返回结果是一个集合的时候,resultType不是集合类型,而是集合所包含的类型 -->  
        <select id="selectAll" resultType="Blog">  
            select * from t_blog  
        </select>  
    <!--  模糊查询   -->  
        <select id="fuzzyQuery" resultType="Blog" parameterType="java.lang.String">  
            select * from t_blog where title like "%"#{title}"%"  
        </select>  
    <!--  删除记录   -->  
        <delete id="deleteBlog" parameterType="int">  
            delete from t_blog where id = #{id}  
        </delete>  
    </mapper> 
    SQL映射语句中一些应该注意的问题:
    1.  resultType的值应该是返回类型的完全名或别名,当返回的结果是一个集合的时候,resultType应为集合中所包含的类型,而不是集合类型,如上面的Blog
    2. resultType和resultMap都是表示指定返回结果的,但两者不能同时使用
    3. 对于Insert映射语句有一个useGeneratedKeys属性,该属性的默认值为false,当该属性的值为true时,在进行插入操作时,mybatis会取到当前正在插入的记录在数据库中的自动递增的主键值,并把它设置给指定的实体的属性,这就需要设置一个keyProperty属性,用于指定实体中表示主键的属性


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值