1 where
1.0 in查询
非字符串使用null判断,字符串使用’'判断.
- MySQL
select * from infostable where name in("xiaoixao","xiaohua");
- MyBatis
name为姓名列表(List)
<select id="selectDatasFilterByName" resultType="className" parameterType="map">
select * from infostable
<where>
<if test="name != null and name.size()>0">
name in
<foreach item="item" index="index" collection="name" open="(" separator="," close=")">
#{item}
</foreach>
</if>
</where>
</select>
1.2 where查询
- SQL
select * from infostable where name="xiaoxiao";
- MyBatis
name为字符串,使用’'判断
<select id="selectDatasFilterByName" resultType="className" >
select * from infostable
<where>
<if test="name != ''">
name = #{name}
</if>
</where>
</select>
1.3 where查询-组合判断
- Usage
<select id="selectDatasFilterByName" resultType="className" paramterType="map">
select * from infostable
<where>
<if test="name != '' and name != null"></if>
</where>
</select>
2 like查询
- SQL
select * from infostable where name like "%xiao%" or "%da%";
- MyBatis
<select id="countUsers" parameterType="map" resultType="int">
select count(*) from infos
<where>
<if test="userCode != null and userCode.size>0">
and
<foreach item="item" index="index" collection="userCode"
separator="or">
user_code like CONCAT('%',#{item},'%')
</foreach>
</if>
</where>
</select>
3 mybatis一对多映射
- 一
package com.sb.po;
public class OneInfos{
private String name;
private String address;
private String fk;
//Setter & Getter omited.
}
- 多
package com.sb.po;
import com.sb.po.OneInfos;
import java.util.List;
public class ManyInfos{
private String name;
private String address;
private String fk;
private List<OneInfos> oneInfos;
//Setter & Getter omited.
}
- 映射文件
<resultMap id="taskMap" type="com.sb.po.ManyInfos">
<result property="name" column="name"/>
<result property="address" column="address"/>
<collection property="oneInfos" javaType="List"
ofType="com.sb.po.OneInfos"
fetchType="lazy">
<result property="name" column="name"/>
<result property="address" column="address"/>
</collection>
</resultMap>
<select id="selectFromManyInfos" pamameterType="map" resultMap="taskMap">
select * from many_tables mt, one_tables ot
where mt.fk = ot.fk
</select>
序号 | 参数 | 描述 |
---|---|---|
1 | id | 函数名称,调用时使用 |
2 | property | 类中变量名称,必须与数据表的列column同名 |
3 | column | 数据表列名称 |
4 | collection | 集合数据,如List,一对多关系中,一类中的数据 property为类中变量名称,javaType为类中数据类型,ofType为类包,fetchType加载方式,lazy为懒加载,即用时加载 |
4 时间区间查询
使用between A and B语句
其中,A为起始时间字符串,B为结束时间字符串,
注意:
MySQL数据库的时间字段格式为datetime,使用%Y-%m-%d(年-月-日)格式查询时,MySQL默认添加00:00:00时间,因此,
- 问题
出现不能查到结束时间的情况 - 方案
拼接时间为23:59:59
<select id="countWorkorder" parameterType="map" resultType="int">
select count(*) from tb_some_system
<where>
<if test="filterStarttime !=null and filterStarttime != ''
and filterEndtime !=null and filterEndtime != ''">
and DATE_FORMAT(alarm_endtime, '%Y-%m-%d %H:%i:%s') between #{filterStarttime} and #{filterEndtime}
</if>
</where>
</select>
6 附件
6.1 foreach遍历
<if test="name != null">
name in
<foreach item="name" index="index" collection="name" open="(" separator="," close=")">
#{name}
</foreach>
</if>
序号 | 参数 | 描述 |
---|---|---|
1 | item | 迭代对象中的内容,若迭代对象为list,则item为list中的数据,若迭代对象为map,则item为map的键名key |
2 | index | 索引,若迭代对象为list,索引为序号,若迭代对象为map,索引为key |
3 | collection | 迭代对象,类型为list或map |
4 | open | foreach开始符号 |
5 | separator | 元素分割符号,in为逗号 |
6 | close | foreach结束符号 |
- 场景
(1)搜多人信息,选择多个人名,筛选出对应信息,向数据库传入map,key为name,value为nameList;
(2)若输入的人名为空,查询所有人信息,此时需要向数据库输入null,此时才会过滤!null的情况,查询全部信息;
6.2 where if组合查询
and连接多个if
- 变量为单字符串字符串
<select id="selectDatasFilterByNameAddress" resultType="className" parameterType="map">
select * from infostable
<where>
<if test="name != '' and name != null">
and name = #{name}
</if>
<if test="address != '' and address != null">
and address = #{address}
</if>
</where>
</select>
- 变量为组合数据
<select id="selectDatasFilterByNameAddress" resultType="className" parameterType="map">
select * from infostable
<where>
<if test="name != null">
and name in
<foreach item="name" index="index" collection="name" open="(" separator="," close=")">
#{name}
</foreach>
</if>
<if test="address != '' and address != null">
and address = #{address}
</if>
</where>
</select>
7 小结
序号 | 类型 | 描述 |
---|---|---|
1 | 请求体使用Map接收数据 | if中的test使用数据库表中的字段判断数据 |
2 | 请求体使用实体接收数据 | if中的test使用实体类属性进行字段判断 |
- 表
id | order_number |
---|---|
1 | 00000 |
- 实体
public class TestPO{
private Integer id;
private String orderNumber;
}
【参考文献】
[1]https://blog.csdn.net/m0_37897396/article/details/80237920
[2]https://www.cnblogs.com/xyhero/p/2bda5c83b9f218ce82ad3a6ec4653144.html
[3]https://blog.csdn.net/qq_17011423/article/details/69220231
[4]https://blog.csdn.net/snow_love_xia/article/details/82910336?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase
[5]https://blog.csdn.net/A_Runner/article/details/77143566