栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合Mybatis-plus的实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合Mybatis-plus的实现

1.添加pom引用

maven的引用很简单,官方已经给出starter,不需要我们考虑它的依赖关系了,此处使用的是2.3版本。


  com.baomidou
  mybatis-plus-boot-starter
  2.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内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/131512.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号