三分鐘搞定springboot+mybatis分頁

建立一個空的springboot項目,添加web,mysql依賴mysql

  • 導入項目到IDEgit

  • pom.xml中添加依賴web

<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;
	}
	
}

返回結果:spring

{
    "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-websql

更多參考數據庫

相關文章
相關標籤/搜索