欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot整合mybatis-plus實(shí)現(xiàn)多表分頁(yè)查詢的示例代碼

 更新時(shí)間:2021年03月08日 14:32:47   作者:團(tuán)子.  
這篇文章主要介紹了springboot整合mybatis-plus實(shí)現(xiàn)多表分頁(yè)查詢的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1.新建一個(gè)springboot工程

2.需要導(dǎo)入mybatis和mybatis-plus的依賴文件

<dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>3.1.1</version>
    </dependency>
     <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.0.1</version>
    </dependency>

3.application.yml配置文件

server:
 port: 8080
spring:
 datasource:
  url: jdbc:mysql://localhost:3306/mybatisplus?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=UTC
  username: root
  password: 數(shù)據(jù)庫(kù)密碼
mybatis:
 mapper-locations: classpath*:mapper/*.xml

mybatis-plus:
 mapper-locations: classpath:/mapper/*Mapper.xml
logging:
 level:
  com.tuanzi.*: debug

4.首先我們需要寫一個(gè)類來配置分頁(yè)插件

省略import
@EnableTransactionManagement
@Configuration
@MapperScan("com.tuanzi.*.mapper*")
public class MybatisPlusConfig {

  /**
   * 分頁(yè)插件
   */
  @Bean
  public PaginationInterceptor paginationInterceptor(){
    return new PaginationInterceptor();
  }
}

5.controller類

@RestController
@RequestMapping("/user")
public class UserController {

  @Autowired
  UserService userService;

  /**
   * 多表關(guān)聯(lián),分頁(yè)查詢(1對(duì)1)
   * @param page
   * @return
   */
  @RequestMapping("/findAll")
  public Result<IPage<User>> findAll(@RequestBody Page<User> page){

     return userService.pages(page);

  }

  /**
   * 多表關(guān)聯(lián),分頁(yè)查詢(1對(duì)多)
   * @param page
   * @return
   */
  @RequestMapping("/selectAll")
  public Result<IPage<User>> selectAll(@RequestBody Page<User> page){

    return userService.pageList(page);

  }
}

6.service類

public interface UserService extends IService<User> {

  Result<IPage<User>> pages(Page<User> page);

  Result<IPage<User>> pageList(Page<User> page);
}

7.service實(shí)現(xiàn)類

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

  @Autowired
  UserMapper userMapper;
  @Override
  public Result<IPage<User>> pages(Page<User> page) {
    IPage<User> userIPage = userMapper.Pages(page);
    return Result.getSuccess("分頁(yè)查詢成功",userIPage);
  }

  @Override
  public Result<IPage<User>> pageList(Page<User> page) {
    IPage<User> userIPage = userMapper.pageList(page);
    return Result.getSuccess("分頁(yè)查詢成功",userIPage);
  }
}

8.mapper接口

注意!!: 如果入?yún)⑹怯卸鄠€(gè),需要加注解指定參數(shù)名才能在xml中取值

@Mapper
@Repository
public interface UserMapper extends BaseMapper<User> {

  IPage<User> Pages(@Param("page") Page<User> page);

  IPage<User> pageList(@Param("page") Page<User> page);
}

9.xml文件

一對(duì)一關(guān)聯(lián)

 <!-- 一對(duì)一 通用查詢映射結(jié)果 -->
  <resultMap id="BaseResultMap1" type="com.tuanzi.user.entity.User">
    <result column="id" property="id" />
    <result column="name" property="name" />
    <result column="age" property="age" />
    <result column="email" property="email" />
    <!--assocication  一對(duì)一關(guān)聯(lián)查詢
        可以指定聯(lián)合的JavaBean對(duì)象
        property="work"指定哪個(gè)屬性是聯(lián)合的對(duì)象
        javaType:指定這個(gè)屬性對(duì)象的類型
      -->
    <association property="work" javaType="com.tuanzi.user.entity.Work">
      <result column="id" property="id" />
      <result column="position" property="position" />
      <result column="user_id" property="userId" />
    </association>
  </resultMap>

一對(duì)多關(guān)聯(lián)

<!-- 一對(duì)多 通用查詢映射結(jié)果 -->
  <resultMap id="BaseResultMap2" type="com.tuanzi.user.entity.User">
    <result column="id" property="id" />
    <result column="name" property="name" />
    <result column="age" property="age" />
    <result column="email" property="email" />
    <!--
		collection定義關(guān)聯(lián)結(jié)合類型的屬性的封裝規(guī)則
		property="workList"指定哪個(gè)屬性是聯(lián)合的對(duì)象
		ofType:指定集合里面元素的類型
		-->
    <collection property="workList" ofType="com.tuanzi.user.entity.Work">
      <result column="id" property="id" />
      <result column="position" property="position" />
      <result column="user_id" property="userId" />
    </collection>
  </resultMap>

SQL語句:

<select id="Pages" resultMap="BaseResultMap1">
    select a.id id,a.name name,a.age age,a.email email,b.position position,b.user_id user_id from user a LEFT JOIN work b on a.id=b.user_id
  </select>
  <select id="pageList" resultMap="BaseResultMap2">
    select a.id id,a.name name,a.age age,a.email email,b.position position,b.user_id user_id from user a LEFT JOIN work b on a.id=b.user_id
  </select>

10.這樣就基本完成了!我這里省略了實(shí)體類

我們運(yùn)行一下,用postman測(cè)試一下結(jié)果
這里我們需要傳2個(gè)參數(shù),當(dāng)然我們也可以不用傳,因?yàn)閙ybatis-plus有默認(rèn)值
來看下mybatis-plus的page源碼

在這里插入圖片描述

效果圖:

在這里插入圖片描述

在這里插入圖片描述

最后附贈(zèng)源碼地址:demo

到此這篇關(guān)于springboot整合mybatis-plus實(shí)現(xiàn)多表分頁(yè)查詢的示例代碼的文章就介紹到這了,更多相關(guān)springboot整合mybatis-plus多表分頁(yè)查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論