要在Spring Boot中使用MyBatis实现复杂查询和分页功能,通常会用到动态SQL以及分页插件。以下是具体的实现方法:
1. 增加复杂查询功能
使用MyBatis的<script>
标签和<if>
标签,可以实现动态SQL。这里我们以一个条件查询为例,根据ip
、source
、username
进行条件查询。
复杂查询Mapper方法:
import org.apache.ibatis.annotations.*;import java.util.List;@Mapper
public interface AlarmCdnBlockipMapper {// 其他方法保持不变@Select("<script>" +"SELECT * FROM alarm_cdn_blockip " +"WHERE 1=1 " +"<if test='ip != null and !ip.isEmpty()'>" +"AND ip = #{ip} " +"</if>" +"<if test='source != null and !source.isEmpty()'>" +"AND source = #{source} " +"</if>" +"<if test='username != null and !username.isEmpty()'>" +"AND username = #{username} " +"</if>" +"ORDER BY create_time DESC" +"</script>")List<AlarmCdnBlockip> findByConditions(@Param("ip") String ip, @Param("source") String source, @Param("username") String username);
}
- 动态SQL:
<script>
标签包含动态SQL语句,<if>
标签用于条件判断。可以根据传入的参数生成不同的SQL语句。 - 条件查询:这个查询方法会根据
ip
、source
、username
进行条件过滤。如果某个参数未传入,则该条件不会出现在SQL语句中。
2. 增加分页功能
分页功能可以通过MyBatis的插件来实现,常用的分页插件是PageHelper
。首先,需要添加PageHelper
依赖。
Maven依赖:
<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.4.6</version>
</dependency>
配置分页插件:
在application.properties
中配置PageHelper
插件。
# PageHelper配置
pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql
使用分页功能:
在Service层中使用PageHelper.startPage()
方法来设置分页参数。
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class AlarmCdnBlockipService {@Autowiredprivate AlarmCdnBlockipMapper alarmCdnBlockipMapper;// 其他方法保持不变public PageInfo<AlarmCdnBlockip> findByConditionsWithPaging(String ip, String source, String username, int pageNum, int pageSize) {// 开启分页PageHelper.startPage(pageNum, pageSize);// 执行查询List<AlarmCdnBlockip> result = alarmCdnBlockipMapper.findByConditions(ip, source, username);// 封装分页信息return new PageInfo<>(result);}
}
PageHelper.startPage(pageNum, pageSize)
:设置分页的页码和每页的条数。PageInfo
:封装了分页结果及相关的分页信息,如总条数、总页数、当前页等。
3. Controller层增加分页查询接口
Controller层可以增加分页查询的接口,将分页参数传递给Service层。
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/alarm-cdn-blockip")
public class AlarmCdnBlockipController {@Autowiredprivate AlarmCdnBlockipService alarmCdnBlockipService;// 其他方法保持不变@GetMapping("/search")public PageInfo<AlarmCdnBlockip> search(@RequestParam(required = false) String ip,@RequestParam(required = false) String source,@RequestParam(required = false) String username,@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "10") int pageSize) {return alarmCdnBlockipService.findByConditionsWithPaging(ip, source, username, pageNum, pageSize);}
}
@RequestParam(defaultValue = "1") int pageNum
:通过RequestParam
接受页码和每页条数,默认值为1和10。PageInfo
:返回分页信息,包含查询结果和分页元数据。
4. 总结
通过以上实现,已经完成了复杂查询和分页功能:
- 复杂查询:动态SQL实现灵活的条件查询。
- 分页功能:通过
PageHelper
插件进行分页查询,支持大数据量的查询和分页返回。