1.mybatis的if判断
String 和 int两种
<if test "order != null and order != '' ">
<if test "aa != 0 and aa != 5">
2.mybatis 的 like
使用concat多层拼接(一层就一个)
name like concat("%",#{name},"%")
name like concat(concat("%",#{name}),"%")
3.大于小于,大于等于,小于等于
大于 > 大于等于 >=
小于 < 小于等于 <=
4.接口中@Param("")指定参数名称的使用,
//既有实体类参数,又有其他参数
List<AA> test1(@Param("aa") AA aa, @Param("startTime")Date startTime,
@Param("endTime")Date endTime)
//xml
<if test="aa.name != null and aa.name != ''"> balabala like concat("%",#{aa.name},"%")
<if test="startTime != null"> balabala DT_CREATE_TIME >= #{startTime}
//当接口中只有实体类的时候
//一.指定参数名称
List<AA> test2(@Param("aa" AA aa))
//xml 这里取参数的时候必须要写上param的别名
<if test="aa.name != null and aa.name != '' "> balabala like concat("%",#{aa.name},"%")
//二.不指定参数名称
List<AA> test2(AA aa))
//xml 这里取参数的时候可以直接写属性名称(AA类的属性)
<if test="name != null and name != '' "> balabala like concat("%",#{name},"%")
//三.参数不是实体类的时候,指定别名就使用别名,不指定可以直接使用形参的名称默认一致
List<AA> test3(Sting id)
List<AA> test4(@Param("iid" String id))
5.返回结果处理
//1.返回结果到实体类中
//xml中需要写对应的resultMap(一般数据库的字段命名是和实体类属性名称不一样的,如果一样不用写)
<resultMap id="testMap" type="com.test.Test">
<id column="S_ID" jdbcType="VARCHAR" property="id">
<id column="S_NAME" jdbcType="VARCHAR" property="name">
<id column="S_PHONE" jdbcType="VARCHAR" property="phone">
<id column="DT_CREATE_TIME" jdbcType="TIMESTAMP" property="createTime">
</resultMap>
<select id="test1" resultMap="testMap"> //List或者一个对象直接就可以对应上
select * from TAB_TEST
</select>
<select id="test2" resultMap="testMap">
select S_ID ,S_NAME ,S_PHONE ,DT_CREATE_TIME from TAB_TEST
</select>
<select id="test3" resultType="com.test.Test">
select S_ID id,S_NAME name,S_PHONE phone,DT_CREATE_TIME createTime from TAB_TEST
</select>
//2.返回结果到List<Map<String,Object中>>
<select id="test1" resultMap="map"> //这个map是java.util.Map
select * from TAB_TEST
</select>
//这里可以选择让List<Map>直接返回或者将其从Map转换为实体类对象
//转换参考https://blog.csdn.net/liuyunyihao/article/details/86597002
报错:invalid bound statement(not found)
解决:方法的名称和xml的id名称没对应 或者 配置文件配置没有扫描到
错误:查询结果List 有长度,但是List里面每一个元素都为null
解决:查询结果中的字段名称和实体类属性名称有不一致
select 标签 resultType为int 这种基本类型也是需要写的,不能省略
否则会报 A query was run and no Result Maps were found for the Mapped Statement ‘com.test.TestMapper.test1’. It’s likely that nethier a Result Type nor a Result Map was specified
报错:元素类型为 “mapper” 的内容必须匹配 "(cache-ref|cache|resultMap*|parameterMap*|sql*|insert*|update*|delete*|select
解决:是因为xml文件有错误,我是把方法的id放到xml文件中单独行了没有在标签中,xml没有编辑器直接的检验,只有在编译的时候才能判定xml是否正确