This commit is contained in:
rsgltzyd 2024-08-03 22:44:47 +08:00
parent 00f8620424
commit 10d774df62
8 changed files with 60 additions and 26 deletions

View File

@ -0,0 +1,26 @@
package com.example.demo.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
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;
@Configuration
@MapperScan(basePackages = {"com.example.**.mapper"})
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseGeneratedShortKey(false);
}
}

View File

@ -1,7 +1,7 @@
package com.example.demo.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.News;
import com.example.demo.entity.User;
import com.example.demo.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/news")
public class NewsController {
private final NewsService newsService;
@ -17,9 +18,11 @@ public class NewsController {
this.newsService = newsService;
}
@GetMapping("/news")
public List<News> getAllNews() {
return newsService.getAllNews();
@GetMapping("/list")
public Page<News> getAllNews(@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "10") int size) {
Page<News> newsPage = new Page<>(page, size);
return newsService.getAllNews(newsPage);
}
@PostMapping("/insertNews")

View File

@ -17,20 +17,20 @@ public class News {
@TableField(value = "EXCERPT")
private String excerpt;
@TableField(value = "CREATE_TIME", fill = FieldFill.UPDATE)
private Timestamp create_time;
@TableField(value = "CREATE_TIME", fill = FieldFill.INSERT)
private Timestamp createTime;
@TableField(value = "UPDATE_TIME", fill = FieldFill.INSERT)
private Timestamp update_time;
@TableField(value = "UPDATE_TIME", fill = FieldFill.UPDATE)
private Timestamp updateTime;
@TableField(value = "IS_DELETE")
private Boolean is_delete;
private Boolean isDelete;
@TableField(value = "EXT")
private String ext;
@TableField(value = "BATCH_ID")
private String batch_id;
private String batchId;
public News() {
@ -40,14 +40,4 @@ public class News {
this.title = title;
this.excerpt = excerpt;
}
public News(int id, String title, String excerpt, Timestamp create_time, Timestamp update_time, Boolean is_delete, String ext) {
this.id = id;
this.title = title;
this.excerpt = excerpt;
this.create_time = create_time;
this.update_time = update_time;
this.is_delete = is_delete;
this.ext = ext;
}
}

View File

@ -2,6 +2,8 @@ package com.example.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.News;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface NewsMapper extends BaseMapper<News> {
}

View File

@ -2,6 +2,8 @@ package com.example.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}

View File

@ -1,10 +1,9 @@
package com.example.demo.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.entity.News;
import java.util.List;
public interface NewsService extends IService<News> {
List<News> getAllNews();
Page<News> getAllNews(Page<News> page);
News getNewsById(Integer id);
}

View File

@ -1,5 +1,6 @@
package com.example.demo.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.News;
import com.example.demo.mapper.NewsMapper;
@ -12,9 +13,15 @@ import java.util.UUID;
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {
private final NewsMapper newsMapper;
public NewsServiceImpl(NewsMapper newsMapper) {
this.newsMapper = newsMapper;
}
@Override
public List<News> getAllNews() {
return list();
public Page<News> getAllNews(Page<News> page) {
return newsMapper.selectPage(page, null);
}
@Override
@ -26,7 +33,7 @@ public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements Ne
public boolean saveBatch(Collection<News> newsList) {
String batchId = UUID.randomUUID().toString();
for (News news : newsList) {
news.setBatch_id(batchId);
news.setBatchId(batchId);
}
return super.saveBatch(newsList);
}

View File

@ -3,5 +3,10 @@ spring.datasource.url=jdbc:p6spy:mysql://47.106.93.245:9003/service
spring.datasource.username=root
spring.datasource.password=qingyu23.
spring.datasource.driver-class-name = com.p6spy.engine.spy.P6SpyDriver
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.global-config.db-config.id-type=auto
logging.level.com.baomidou.mybatisplus=DEBUG
logging.level.org.apache.ibatis=DEBUG