亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

瀏覽:46日期:2023-03-21 16:58:19

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: 8080spring: 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/*.xmlmybatis-plus: mapper-locations: classpath:/mapper/*Mapper.xmllogging: level: com.tuanzi.*: debug

4.首先我們需要寫(xiě)一個(gè)類來(lái)配置分頁(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)類

@Servicepublic 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@Repositorypublic 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 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 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語(yǔ)句:

<select 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 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)值來(lái)看下mybatis-plus的page源碼

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

效果圖:

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

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

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

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

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 自拍视频在线观看视频精品 | 99久久免费看精品国产一区 | 午夜三级在线 | 一级毛片aaa | 精品欧美一区二区精品久久 | 成人高清在线观看播放 | 日韩专区亚洲国产精品 | 91大神大战丝袜美女在线观看 | 久草视频在 | 天堂精品高清1区2区3区 | 欧美日韩视频一区二区在线观看 | 日韩免费高清一级毛片在线 | 欧美性性性性性色大片免费的 | 国产a网站 | 亚洲国产经典 | 亚洲人成网国产最新在线 | 亚洲天堂视频网站 | 国产一区自拍视频 | 国产精品二区三区免费播放心 | 成人影院免费看 | 日本欧美韩国一区二区三区 | 欧美成在线 | 国产一区二区三区高清 | 亚洲成人免费网站 | 亚洲综合免费 | 国产a毛片| 日本一本久道 | u影一族亚洲精品欧美激情 va欧美 | 天干天干天啪啪夜爽爽色 | 91精品国产手机 | 福利视频美女国产精品 | 久草在线免费资源站 | 在线精品国产一区二区 | 草草影院www色极品欧美 | 看真人一一级毛片 | 免费黄色美女视频 | 午夜亚洲国产成人不卡在线 | 精品一区二区久久 | 精品久久久久国产免费 | 盈盈性影院 | 精品视自拍视频在线观看 |