三分钟搞定springboot+mybatis分页

原创
2018/06/27 20:32
阅读数 1.5W

创建一个空的springboot项目,添加web,mysql依赖

  • 导入项目到IDE

  • pom.xml中添加依赖

<dependency>
    <groupId>net.oschina.durcframework</groupId>
    <artifactId>fastmybatis-spring-boot-starter</artifactId>
    <version>1.0.3</version>
</dependency>
  • application.properties中配置数据库连接
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/stu?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=root
  • 添加实体类,Mapper
// 实体类
@Table(name = "t_user")
public class TUser {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    /** ID, 数据库字段:id */
    private Integer id;

    /** 用户名, 数据库字段:username */
    private String username;

    /** 状态, 数据库字段:state */
    private Byte state;

    /** 是否删除, 数据库字段:isdel */
    @com.gitee.fastmybatis.core.annotation.LogicDelete
    private Boolean isdel;

    /** 备注, 数据库字段:remark */
    private String remark;

    /** 添加时间, 数据库字段:add_time */
    private Date addTime;

    /** 金额, 数据库字段:money */
    private BigDecimal money;

    /** 剩下的钱, 数据库字段:left_money */
    private Float leftMoney;
	// 省略get set
}

// Mapper
public interface TUserMapper extends CrudMapper<TUser, Integer> {

}
  • 新建Controller
@RestController
public class TUserController {
	
    @Autowired
    private TUserMapper tUserMapper;
    
	@GetMapping("/")
	public Object home(@RequestParam(defaultValue = "1") int pageIndex, @RequestParam(defaultValue = "10")int pageSize) {
	    Query query = new Query().page(pageIndex, pageSize);
	    PageInfo<TUser> pageInfo = MapperUtil.query(this.tUserMapper, query);
		return pageInfo;
	}
	
}

返回结果:

{
    "list": [
        {
            "id": 13,
            "username": "username1",
            "state": 0,
            "isdel": false,
            "remark": "remark1",
            "addTime": "2018-06-20T02:59:40.000+0000",
            "money": 1,
            "leftMoney": 200
        }
    ],
    "total": 11,
    "start": 10,
    "pageIndex": 2,
    "pageSize": 10,
    "pageCount": 2,
    "currentPageIndex": 2,
    "firstPageIndex": 1,
    "prePageIndex": 1,
    "lastPageIndex": 2,
    "nextPageIndex": 2
}

demo地址:https://gitee.com/durcframework/dev-codes/tree/master/demo-web

更多参考

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
4 收藏
0
分享
返回顶部
顶部