1.添加pom引用
maven的引用很简单,官方已经给出starter,不需要我们考虑它的依赖关系了,此处使用的是2.3版本。
com.baomidou mybatis-plus-boot-starter2.3
2.配置
server.port=8080
#mysql
spring.datasource.url=jdbc:mysql://localhost:3306/ease-run?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#mybatis-plus
mybatis-plus.mapper-locations=classpath:com/mht/springbootmybatisplus/mapper/xml
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("offsetAsPageNum","true");
properties.setProperty("rowBoundsWithCount","true");
properties.setProperty("reasonable","true");
//配置mysql数据库的方言
properties.setProperty("dialect","mysql");
pageHelper.setProperties(properties);
return pageHelper;
}
}
Mapper:
public interface UserMapper extends baseMapper{ @Select("selectUserList") List selectUserList(Pagination page,String state); }
新建UserMapper配置文件:
id, name, age
4.新建service层类UserService:
@Service public class UserService extends ServiceImpl{ public Page selectUserPage(Page page, String state) { page.setRecords(baseMapper.selectUserList(page,state)); return page; } }
UserService继承了ServiceImpl类,mybatis-plus通过这种方式为我们注入了UserMapper,这样可以使用service层默认为我们提供的很多方法,也可以调用我们自己在dao层编写的操作数据库的方法.Page类是mybatis-plus提供分页功能的一个model,继承了Pagination,这样我们也不需要自己再编写一个Page类,直接使用即可.
5,新建controller层UserController:
@Controller
public class UserController extends baseController {
@Autowired
private IUserService userService;
@ResponseBody
@RequestMapping("/page")
public Object selectPage(Model model){
Page page=new Page(1,10); //1表示当前页,而10表示每页的显示显示的条目数
page = userService.selectUserPage(page, "NORMAL");
return page;
}
到此这篇关于springboot整合Mybatis-plus的实现的文章就介绍到这了,更多相关springboot整合Mybatis-plus内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!



