Mybatis使用记录

Mybatis使用记录

mybatis是j2ee中一个重要的orm,mybatis有自动化生成工具,更加方便使用

继承

mybatis支持继承,其中Mapper映射文件会自动合并,如下,两个xml中列出的方法是可以映射到同一个TestDAO.java中的,这样可以方便的将自定义的sql放在Ex.xml中,自动生成的放在第一个xml中,便于数据库变化后自动生成
TestMapper.xml

<mapper namespace="com.rjw.j2ee.dal.dao.TestDAO" >
……
</mapper>

TestExMapper.xml

<mapper namespace="com.rjw.j2ee.dal.dao.TestDAO" >
……
</mapper>

数据库字段最好加上表名,方便Join查询,同时尽量不要使用sql的关键字作为表名。

mybatis参数

mybatis可以通过三种方式传入参数,#{0}占位符,#{param1}占位符,@Param注解标记参数。动态SQL中test语句可以解析param1和Param注解。使用前两种占位符,需要设置 parameterType=”map”

mybatis嵌套

可以使用association嵌套,分为两种,一种子查询,一种联合查询,子查询时在ResultMap中配置select,其中select对应的语句有配置相应的ResultMap:

<association property="property" column="column" javaType="com.rjw.j2ee.test.TestModel" select="ccom.rjw.j2ee.test.dal.dao.TestDao.selectByPrimaryKey"/>

联合查询时,在ResultMap中嵌套ResultMap

<resultMap id="blogResult" type="Blog">
  <id property="id" column="blog_id" />
  <result property="title" column="blog_title"/>
  <association property="author" column="blog_author_id" javaType="Author" resultMap="authorResult"/>
</resultMap>

<resultMap id="authorResult" type="Author">
  <id property="id" column="author_id"/>
  <result property="username" column="author_username"/>
  <result property="password" column="author_password"/>
  <result property="email" column="author_email"/>
  <result property="bio" column="author_bio"/>
</resultMap>

<select id="selectBlog" resultMap="blogResult">
  select
    B.id            as blog_id,
    B.title         as blog_title,
    B.author_id     as blog_author_id,
    A.id            as author_id,
    A.username      as author_username,
    A.password      as author_password,
    A.email         as author_email,
    A.bio           as author_bio
  from Blog B left outer join Author A on B.author_id = A.id
  where B.id = #{id}
</select>

多记录插入,其中foreach中collection的值只有list和collection

<insert id="selectPostIn" resultType="domain.blog.Post">
  insert into employees
    (
            <include refid="Base_Column_List" />
    )
values
<trim suffix="" suffixOverrides=",">
  <foreach item="item" index="index" collection="list"
      open="(" separator="," close=")">
      uuid, 
      #{item.id},
      #{item.name},
      #{item.value}
  </foreach>
 </trim>
</insert>

插入时主键的处理

批量插入时使用selectKey时所有记录的主键都是一样的,故下面方式会报错主键重复错

<insert id="selectPostIn" resultType="domain.blog.Post">
<selectKey keyProperty="key_uid" resultType="String" order="BEFORE">
    select uuid()
  </selectKey>
  insert into employees
    (
            <include refid="Base_Column_List" />
    )
values
<trim suffix="" suffixOverrides=",">
  <foreach item="item" index="index" collection="list"
      open="(" separator="," close=")">
      #{key_uid}, 
      #{item.id},
      #{item.name},
      #{item.value}
  </foreach>
 </trim>
</insert>

mybatis简单分页

直接在DAO.java的接口中声明重载方法,加入参数RowBounds即可

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值