idea+spring boot+mybatis分页查询
写一套idea+spring boot+mybatis分页查询的例子,这里的分页使用的是mybatis的分页插件PagehHelper
首先看下目录结构
开发
第1.0.0.步:在pom.xml添加mybatis插件依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
第1.0.1步:在.properties配置文件中加入以下配置
server:
port: 8081
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/smbms?useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password: root
mybatis:
mapper-locations: classpath:mybatis/*.xml
type-aliases-package: com.yx.pojo
# PageHelper分页插件
pagehelper:
helperDialect: mysql
supportMethodsArguments: true
params: count=countSql
reasonable: true
第2.0.0步: 编写启动类的代码
//在main方法启动下面加就可以
//配置mybatis的分页插件pageHelper
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("offsetAsPageNum","true");
properties.setProperty("rowBoundsWithCount","true");
properties.setProperty("reasonable","true");
properties.setProperty("dialect","mysql"); //配置mysql数据库的方言
pageHelper.setProperties(properties);
return pageHelper;
}
第2.0.1步: 编写实体类 PaySeq
@Data
public class PaySeq extends BaseEntity {
//为了省空间就把注释省了
private int accId;
private String accName;
private String userId;
private String appId;
private String openId;
private int orderId;
}
2.0.2步:编写controller层
@RestController
@RequestMapping("/payment")
public class PayController {
@Autowired
private PayBiz payBiz;
@RequestMapping("/findAll")
@ResponseBody
public Object findPayAll(int pageNum,int pageSize) {
ModelMap map = new ModelMap();
//pageNum:当前第几页 pageSize:每页的数量
PageHelper.startPage(pageNum, pageSize);
try {
List<PaySeq> PayAll1 = payBiz.findPayAll();
//分页的信息
PageInfo<PaySeq> pageInfo1 = new PageInfo<>(PayAll1);
map.put("msg", "查询所有成功");
map.put("data", pageInfo1);
map.put("code", 0);
} catch (Exception e) {
e.getStackTrace();
map.put("msg", "查询所有失败");
map.put("code", 1);
}
return map;
}
}
2.0.3步:编写Biz层(此层可以做业务判断)
@Component//这个注释不能忘
public class PayBiz {
@Autowired
private PayService payService;
public List<PaySeq> findPayAll() {
return payService.findPayAll();
}
}
2.0.4步:编写Service层
public interface PayService {
List<PaySeq> findPayAll();
}
2.0.5步:编写ServiceImpl
@Service
public class PayServiceImpl implements PayService {
@Autowired
private PaySeqDao paySeqDao;
@Override
public List<PaySeq> findPayAll() {
return paySeqDao.findPayAll();
}
}
2.0.6步:编写Dao
@Repository
public interface PaySeqDao {
List<PaySeq> findPayAll();
}
2.0.7步:编写mapper文件