Mybatis Plus

基本应用 

首先,‌需要确保项目中已经添加了MyBatis Plus的依赖。‌这包括spring-boot-starter、‌spring-boot-starter-test、‌mybatis-plus-boot-starter、‌mysql-connector-java以及lombok

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>

Service 

public interface UserService extends IService<User>

ServiceImpl 

public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService 

Mapper

public interface UserMapper extends BaseMapper<User>

 


QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(User::getName, "天天"); // 名称等于"天天"
oaNoticeDepartmentUserMapper.selectList(queryWrapper);

用Mybatis Plus,进行自定义sql,分页展示。

首先进行配置

@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

在Serviceimpl中

Page<User> page = new Page<>(CurrentPage(),PageSize());
IPage<User> UserIPage =userMapper.selectPageList(page, user);

在Mapper

   IPage<User>  selectPageList(Page<User> page, @Param("param2")User user);

xml文件中

 <select id="selectPageList" resultType="com.office.model.User">
        SELECT
        t1.*
        FROM
        user t1
        LEFT JOIN
       notices t2
        ON
        t1.NOTICE_ID = t2.id
        <where>
             t1.IS_DELETE = 0
            <if test="param2.userId != null">
             AND    t1.USER_ID = #{param2.userId}
            </if>

            <if test="param2.noticeTitle != null and param2.noticeTitle != ''">
                AND t2.NOTICE_TITLE LIKE CONCAT('%', #{param2.noticeTitle}, '%')
            </if>
            <if test="param2.startTime != null  and param2. endTime != null">
                AND t2.RELEASE_TIME &gt;= #{param2.startTime}
                AND t2.RELEASE_TIME &lt;=  #{param2.endTime}
            </if>
            <if test="param2.readState != null">
               AND t1.READ_STATE = #{param2.readState}
            </if>
            <if test="param2.isCollect != null">
                AND t1.IS_CollECT = #{param2.isCollect}
            </if>
        </where>
    </select>

注意

  • 大于符号(>):使用实体引用 &gt;
  • 小于符号(<):使用实体引用 &lt;
  • 大于等于符号(>=):使用实体引用 &gt;=
  • 小于等于符号(<=):使用实体引用 &lt;=
  • (&):&amp;
  • ('):&apos;
  • ("):&quot;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值