
可参考官网:mybatis – MyBatis 3 | XML 映射器
- <dependency>
- <groupId>com.github.pagehelpergroupId>
- <artifactId>pagehelperartifactId>
- <version>${pagehelper.version}version>
- dependency>
-
- <dependency>
- <groupId>com.github.pagehelpergroupId>
- <artifactId>pagehelper-spring-boot-autoconfigureartifactId>
- <version>${pagehelper-spring-boot-autoconfigure.version}version>
- dependency>
-
- <dependency>
- <groupId>com.github.pagehelpergroupId>
- <artifactId>pagehelper-spring-boot-starterartifactId>
- <version>${pagehelper-spring-boot-starter.version}version>
- dependency>
核心类就是PageHelper和PageInfo
- PageHelper.startPage(pageNum,pageSize);//设置分页属性pageNum(第几页)和pageSize(每页显示的条数)
- List
list = userMapper.getUserList();//查询总数 - PageInfo
pageInfo = new PageInfo<>(list);//把查询到的结果封装到PageInfo类中
可参考该博文:Spring Boot 整合 Mybatis 实践教程(干货) - 知乎 (zhihu.com)[目前公司微服务应用整合Mybatis过程和该博文介绍基本一致]
- <insert id="batchSave" parameterType="java.util.List">
- insert into User(title,content) values
- <foreach collection="list" item="item" index="index" separator=",">
- (#{item.title},#{item.content})
- </foreach>
- </insert>
- <!-- 参数类型是List
,实体集合 -->
- <update id="batchUpdate" parameterType="java.util.List">
- <foreach collection="list" item="item" index="index" open="" close="" separator=";">
- update User
- <set>
- title = #{item.title}, content = #{item.content}
- </set>
- where id = #{item.id}
- </foreach>
- </update>
- <!-- 参数类型是List
,实体集合 -->
- <delete id="batchDel" parameterType="java.util.List">
- delete from User where id in
- <foreach collection="list" index="index" item="item" open="(" close=")" separator=",">
- #{item}
- </foreach>
- </delete>
- <!-- 参数类型是List
,id集合 -->
- @RequestMapping("add")
- public void add(HttpServletRequest request) throws Exception {
- List
cList = testService.queryByName("key");//查询 - System.out.println(Thread.currentThread().getName() + "clist.size:" + cList.size());
- if (cList.size() == 0) {
- if (redisUtil.setnx("name_" + "key", "key") == 1) {//如果数据存在则返回0,不存在返回1
- Course course = new Course();
- course.setCname("key");
- try {
- testService.add(course);//插入
- } catch (Exception e) {
- redisUtil.del("name_" + "key");//插入出异常则删除
- throw e;
- }
- System.out.println(Thread.currentThread().getName() + "success");
- redisUtil.expire("name_" + "key", 3);//防止业务处理过程出现异常无法释放锁所以设置锁失效时间3秒
- } else {
- System.out.println(Thread.currentThread().getName() + "exists");
- }
- } else {
- System.out.println(Thread.currentThread().getName() + "false");
- }
- }
