一、XML
1、in语句
-
参数是List类型,且只有一个参数时,不管是否使用@Param指定参数名,在xml中,默认参数名为list
Mapper:List<Person> getPersons(@Param("ids") List<Long> ids);
在xml中,使用foreach标签
<select id="getPersons" resultType="com.base.info.entity"> select * from base_person where id in <foreach collection="list" item="personId" index="index" open="(" separator="," close=")"> #{personId} </foreach> </select>
-
参数是List类型,且有多个参数时,不需要用@Param指定参数名,默认在mapper中是什么名,在xml中就用什么名
Mapper:List<Person> getPersons(List<Long> ids,String type);
xml:
<select id="getPersons" resultType="com.base.info.entity"> select * from base_person where id in <foreach collection="ids" item="personId" index="index" open="(" separator="," close=")"> #{personId} </foreach> </select>
2、<if>标签
-
用作判断的条件是String类型时
Service:List<Person> persons = mapper.getPersons(ids, "unemployment"); List<Person> persons1= mapper.getPersons(ids, "employment");
Mapper:
List<Person> getPersons(List<Long> ids,String type);
xml:
<select id="getPersons" resultType="com.base.info.entity"> select * from base_person where id in <foreach collection="ids" item="personId" index="index" open="(" separator="," close=")"> #{personId} </foreach> <if test="type=='unemployment'"> and age < 20 </if> <if test="type=='employment'"> and age > 20 </if> </select>
-
用作判断的条件是int类型时
Service:List<Person> persons = mapper.getPersons(ids, 0); List<Person> persons1= mapper.getPersons(ids, 1);
Mapper:
List<Person> getPersons(List<Long> ids,int type);
xml:
<select id="getPersons" resultType="com.base.info.entity"> select * from base_person where id in <foreach collection="ids" item="personId" index="index" open="(" separator="," close=")"> #{personId} </foreach> <if test="type==0"> and age < 20 </if> <if test="type==1"> and age > 20 </if> </select>
3、使用if函数报错的解决办法
在xml中使用如下if函数
<select id="getPersons" resultType="com.base.info.entity">
select sum(if(name like '李%',1,0)) as liCount from base_person
</select>
报错如下
Cause: com.baomidou.mybatisplus.exceptions.MybatisPlusException: Failed to process, please exclude the tableName or statementId
Encountered unexpected token: "(" "("
解决办法
如果Mybatis-plus的版本是3.1.1以上,在mapper层的函数上加上@SqlParser(filter=true)
@SqlParser(filter=true)
List<Person> getPersons();
如果Mybatis-plus的版本是3.1.1以下,在配置文件中添加如下配置
mybatis-plus:
global-config:
sql-parser-cache: true
4、like语句
<select id="getByParams" resultType="com.base.info.entity">
select * from base_person where department like concat('%',#{keyWord},'%')
</select>
二、分页
1、IPage分页
1.1、传入Page参数,xml进行sql拼装、返回分页数据
Service
Page<Person> page = new Page<Person>(pageNo, pageSize);//分页对象 pageNo:当前页码 pageSize:每页数据数量
Ipage<Person> pageList = mapper.getByParams(page,String department,String company);
Mapper
IPage<Person> getByParams(Page<Person> page, String department, String company);
xml中不需要操作page参数,Mybatis会自动拼装到sql中
<select id="getByParams" resultType="com.base.info.entity">
select * from base_person where department = #{department} and company = #{company}
</select>
1.2、将结果集合List转为IPage,返回分页数据
List<String> resultList = new ArrayList<>();
IPage<String> page = new Page<>(pageNo, pageSize);
page.setRecords(resultList);
page.setTotal(resultList.size());
return page