亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

1.新建一個springboot工程

2.需要導入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ù)庫密碼
mybatis:
 mapper-locations: classpath*:mapper/*.xml

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

4.首先我們需要寫一個類來配置分頁插件

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

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

5.controller類

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

  @Autowired
  UserService userService;

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

     return userService.pages(page);

  }

  /**
   * 多表關聯(lián),分頁查詢(1對多)
   * @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實現(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("分頁查詢成功",userIPage);
  }

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

8.mapper接口

注意!!: 如果入?yún)⑹怯卸鄠€,需要加注解指定參數(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文件

一對一關聯(lián)

 <!-- 一對一 通用查詢映射結果 -->
  <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  一對一關聯(lián)查詢
        可以指定聯(lián)合的JavaBean對象
        property="work"指定哪個屬性是聯(lián)合的對象
        javaType:指定這個屬性對象的類型
      -->
    <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>

一對多關聯(lián)

<!-- 一對多 通用查詢映射結果 -->
  <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定義關聯(lián)結合類型的屬性的封裝規(guī)則
		property="workList"指定哪個屬性是聯(lián)合的對象
		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.這樣就基本完成了!我這里省略了實體類

我們運行一下,用postman測試一下結果
這里我們需要傳2個參數(shù),當然我們也可以不用傳,因為mybatis-plus有默認值
來看下mybatis-plus的page源碼

在這里插入圖片描述

效果圖:

在這里插入圖片描述

在這里插入圖片描述

最后附贈源碼地址:demo

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

相關文章

最新評論