返回数值类型的数据
Mybatis返回的Map是这样的一种格式:Map< 字段名称,字段值> 一个Map< 字段名,字段值> 对象代表一行数据
mybatis 返回的是对象 如果查不到结果就是null,如果能查到才会转换成对应类型
所以如果想返回int ,并且不是count ( ) 函数 最好是Integer类型,方便进行判断
count ( * ) 如果查不到是0 不是 null
如如查询一个用户的id ( 字段类型为int ) , 查不到就是null
now()
now ( ) 表示当前系统时间
如果赋值给 date类型的字段,则只保留到日 如: 2022 - 09 - 19
如果赋值给 datetime类型的字段,则保留到秒: 2020 - 09 - 19 12 : 46 : 38
now ( 6 ) 表示精确到秒后六位: 2020 - 09 - 19 12 : 46 : 38.222562
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>
一对多映射 自己封装的实体类 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>
一对多映射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>