基本应用
首先,需要确保项目中已经添加了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 >= #{param2.startTime}
AND t2.RELEASE_TIME <= #{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>
注意
- 大于符号(>):使用实体引用
>
- 小于符号(<):使用实体引用
<
- 大于等于符号(>=):使用实体引用
>=
- 小于等于符号(<=):使用实体引用
<=
(&):&
- ('):'
- ("):"