Mybatis Mybatis-plus使用问题全记录

一、XML

1、in语句

  1. 参数是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>
    
  2. 参数是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>标签

  1. 用作判断的条件是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 &lt 20
        	</if>
        	<if test="type=='employment'">
            and age &gt 20
        	</if>
    </select>
    
  2. 用作判断的条件是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 &lt 20
        	</if>
        	<if test="type==1">
            and age &gt 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值