您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)如何在springboot中使用mybatis-plus實現(xiàn)一個多表分頁查詢功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
1.新建一個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ù)庫密碼 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; /** * 多表關(guān)聯(lián),分頁查詢(1對1) * @param page * @return */ @RequestMapping("/findAll") public Result<IPage<User>> findAll(@RequestBody Page<User> page){ return userService.pages(page); } /** * 多表關(guān)聯(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文件
一對一關(guān)聯(lián)
<!-- 一對一 通用查詢映射結(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 一對一關(guān)聯(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>
一對多關(guān)聯(lián)
<!-- 一對多 通用查詢映射結(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"指定哪個屬性是聯(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測試一下結(jié)果
這里我們需要傳2個參數(shù),當(dāng)然我們也可以不用傳,因為mybatis-plus有默認(rèn)值
來看下mybatis-plus的page源碼
效果圖:
關(guān)于如何在springboot中使用mybatis-plus實現(xiàn)一個多表分頁查詢功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。