1.pom.xml
略(参照springboot 整合 mybatis-plus)
2.实例化分页组件
/**
* Mybatis-plus 提供 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
3.具体操作
3.1 controller
@PostMapping("/search/{page}/{size}")
public Result pageQuery(@PathVariable("page") Integer page,@PathVariable("size") Integer size, @RequestBody Label label){
Page<Label> labelPage = labelService.selectByPage(new Page<Label>(page,size),label);
return Result.ok(labelPage);
}
3.2 service
@Override
public Page<Label> selectByPage(Page<Label> labelPage, Label label) {
List<Label> labellist = labelDao.selectByPage(labelPage,label);
labelPage.setRecords(labellist);
return labelPage;
}
3.3 dao
/**
* 注意:这里page 与label 都需要带上注解@Param
* @param page
* @param label
* @return
*/
List<Label> selectByPage(@Param("page") Page<Label> page,@Param("label") Label label);
3.4 xml中动态sql
<select id="selectByPage" resultType="com.temsquare.base.pojo.Label">
select * from tb_label
<where>
<if test="label != null and label.labelname !=null">
and labelname like CONCAT('%', #{label.labelname}, '%')
</if>
<if test="label != null and label.state !=null">
and state = #{label.state}
</if>
<if test="label != null and label.count !=null">
and count = #{label.count}
</if>
<if test="label != null and label.recommend !=null">
and recommend = #{label.recommend}
</if>
<if test="label != null and label.fans !=null">
and fans = #{label.fans}
</if>
</where>
</select>
4. 把玩时间
5.分析总结
5.1:mybatis 在dao中是通过反射获取参数的 ,,如果用@Param声明参数
例如
List<Label> selectByPage(
@Param
(
"page"
) Page<Label> page,
@Param
(
"label"
) Label label);
mybatis 底层会用一个map来收集参数
大致做这样的操作 map.put("page",page).put("label",label);
如果不用@Param注解则会 map.put("arg1",page).put("arg2",label);
具体细节,参见笔记Mybatis Mapper注解开发中的详细Debug 分析
5.2 对service中如下代码的分析
@Override
public Page<Label> selectByPage(Page<Label> labelPage, Label label) {
List<Label> labellist = labelDao.selectByPage(labelPage,label);
labelPage.setRecords(labellist);
return labelPage;
}
labelPage 是作为参数从controller 传过来的,,在执行sql 语句之前 进入拦截器处理之后,就已经对labelPage做了处理
包括总条数,总页数,,结合之前的当前页,每页书,,,其实
labelPage 除了差需要执行sql语句查询出来的labellist,其他需要
的属性都有了,
所以在执行完sql之后获取到
labellist,然后
labelPage.setRecords(labellist);
这个时候labelPage 就饱满了 变成了下面这个可爱的样子
其实,mybatis-plus 封装好的返回分页对象的操作,也是类似的
跟进去
官方的操作也是如出一辙,只不过封装得太狠了,这样对单表操作,张口就来,都不用自己写sql
方便的同时,也滋生了局限性..
最后,提一嘴为什么,可以这样操作,
每一个从controller进来 执行完这个过程,,都不会出现线程安全问题 ,,
因为labelPage 是作为方法的参数进来了,,这个数据 存在于虚拟机
栈中
里面的数据 随着方法的生命周期 压栈--->弹栈 ---> 销毁,对于每一个线程都是独立的
这也是springmvc 比 struts2 牛逼的地方之一 struts2 的参数是在成员变量中的,,会有线程安全问题,,
所以必须要在配置中告诉 spring,将其设置为多例的 也就是 在bean中声明action 的时候必须要设置
scope="potoltype" ,,否则就会有灭顶之灾 ,,就出现了 耳熟能详的 多个线程对同一个共享资源做写操作.
所以一旦访问量上来了,而struts2的action 又是多例的 就会吃掉很多内存资源,,所以性能是不好的, 而springmvc
可以是多例的,也可以是单例的,默认是单例的..
<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">