mybatis使用的一些细节

  1. 返回数值类型的数据
Mybatis返回的Map是这样的一种格式:Map<字段名称,字段值> 一个Map<字段名,字段值>对象代表一行数据
mybatis 返回的是对象 如果查不到结果就是null,如果能查到才会转换成对应类型
所以如果想返回int,并且不是count() 函数 最好是Integer类型,方便进行判断
count(*) 如果查不到是0不是 null 
如如查询一个用户的id(字段类型为int),  查不到就是null
  1. now()
	now() 表示当前系统时间
	如果赋值给  date类型的字段,则只保留到日 如:2022-09-19
	如果赋值给  datetime类型的字段,则保留到秒:2020-09-19 12:46:38
	now(6) 表示精确到秒后六位: 2020-09-19 12:46:38.222562
  1. JavaType和ofTyp
	JavaType和ofType都是用来指定对象类型的,
	但是JavaType是用来指定实体类中属性的类型,
	而ofType指定的是映射到list集合属性中pojo的类型。
@Getter
@Setter
public class Scientic {
    String text;
    List<String> images;
}
    <resultMap id="scientic" type="Scientic">
        <result property="text" column="scientific_knowledge"/>
          <collection property="images" ofType="String" javaType="list">
              <result column="image" />
          </collection>
    </resultMap>
  1. 一对多映射 自己封装的实体类 List< Tolelines > lines;
例子1
@Getter
@Setter
public class Tole {
    String title;
    String audio;
    List<Tolelines> lines;
}
@Getter
@Setter
public class Tolelines {
    String src;
    String text;
}
  <resultMap id="tole" type="Tole">
        <result property="title" column="title"/>
        <result property="audio" column="audio"/>

        <collection property="lines" ofType="Tolelines">
            <result property="src" column="src"/>
            <result property="text" column="text"/>
        </collection>
    </resultMap>
   <select id="getTole" resultType="Tole" resultMap="tole" parameterType="int">
      select A.title,A.audio,B.src,B.text 
      from(api_tole as A left join api_toleline as B on A.id = B.tole_id) where A.id=#{toleId}
    </select>
  1. 一对多映射List< String >
例子2
@Getter
@Setter
public class Scientic {
    String text;
    List<String> images;
}
    <resultMap id="scientic" type="Scientic">
        <result property="text" column="scientific_knowledge"/>

          <collection property="images" ofType="String" javaType="list">
              <result column="image" />
          </collection>
    </resultMap>
    <select id="getScientic" resultMap="scientic" parameterType="int">
        select A.scientific_knowledge,B.image from (api_card as A left join api_cartoonimage as B on
         (A.id = B.card_id)) where A.id = #{cardId}
    </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值