Java框架搭建(mybatis plus)

导读:本篇文章讲解 Java框架搭建(mybatis plus),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

mybatis plus

  1. 添加依赖
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>

2.删掉application.yml中的mybatis,加上mybatis plus

# mybatis-plus配置
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  typeAliasesPackage: com.qcby.demo.entity
  mapperLocations: classpath:mapper/*.xml
#  全局配置id自增
  global-config:
    db-config:
      id-type: auto

3.在demo下建立config并建立MybatisPlusConfig类

package com.qcby.demo.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//启动的移过来
@MapperScan("com.qcby.demo.mapper")
@Configuration
public class MybatisPlusConfig {
    // 最新版
    @Bean  // <bean id=""/>
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

4.测试跟分页

package com.qcby.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.demo.entity.User;
import com.qcby.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("listAll")
    public List<User> listAll(){
        return userService.listAll();
    }

    @RequestMapping("listAll1")
    public  List<User> listAll1(){
        return userService.list();
    }
    @RequestMapping("listPage")
    public IPage listPage(int pageNo,int pageSize,User user){
        Page<User> page = new Page<>(pageNo,pageSize);


        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(user.getUsername())){
                queryWrapper.like("username",user.getUsername());
        }
        IPage<User> pageInfo = userService.page(page);
        return pageInfo;
    }


}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/115402.html

(0)
Java光头强的头像Java光头强

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!