MyBatis-Plus自定义sql分页、if操作、自定义返回字段

分页查询 - 注解方式

    @Select(" select * from Test where keyword = #{keyword }")
    IPage<Test > pageQuery(IPage<Test > page, String keyword );

分页查询 - XML方式

如果使用的是MybatisPlus , XML不需要写resultMap结果集, 不需要写

    <!-- mybatisplus 不需要写这个-->
    <resultMap type="com.entity.Test" id="baseMap">
    </resultMap>

Java Mapper接口

    IPage<Test > pageQuery(IPage<Test > page, String keyword );

Mapper.xml

    <select id="pageQuery" parameterType="java.lang.String" resultType="com.entity.Test">
        SELECT * FROM Test where keyword = #{keyword }
	</select>

MyBatis-PLUS if操作

在sql 前后加上<script> SQL语句</script>

示例一

@Select(" <script> select * from test where 1 = 1 <when test = 'keyword != null'> and keyword = #{keyword} </when> </script>)
IPage<Test> pageQuery(IPage<Test > page, String keyword );

等价于

    <select id="pageQuery" parameterType="java.lang.String" resultType="com.entity.Test">
        SELECT * FROM Test where 1 = 1
        <if test = 'keyword  != null'>  and keyword = #{keyword } </if>
	</select>

有时候多表查询,只想返回部分字段,又不想写VO,可以使用Map

1、第一种 注解方式
    @Select(" select a as 'A' from Test ")
    IPage<Map<String, Object> pageQuery(IPage<Test > page);
2、第二种 使用 mybatis-plus 的 QueryWrapper
QueryWrapper query = new QueryWrapper<Test>();
query.select("a as A");
// 在使用 mybatis-plus 的 mapper类或者 serviceimpl 调用 selectMaps方法 
this.baseMapper.selectMaps(query);
2、第三种 在 mapper.xml里面使用
// MapKey 指定一个字段作为 唯一值(主键)
@MapKey("id")
IPage<Map<String, Object>> adminPageQuery(IPage<Test> iPage);

xml

    <select id="adminPageQuery" resultType="java.util.Map" >
		select a as 'A' from Test
    </select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值