fix:添加转换工具

上级 3b31b76d
此差异已折叠。
此差异已折叠。
......@@ -18,6 +18,7 @@
<!-- 与1.8配置在一起,设置编码集-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<mapstruct.version>1.4.1.Final</mapstruct.version>
</properties>
<dependencies>
<dependency>
......@@ -175,7 +176,11 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
......
//package com.kwan.springbootkwan.config;
//
//import com.kwan.springbootkwan.entity.Person;
//import org.springframework.batch.core.Job;
//import org.springframework.batch.core.Step;
//import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
//import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
//import org.springframework.batch.core.configuration.annotation.StepScope;
//import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
//import org.springframework.batch.item.database.JdbcBatchItemWriter;
//import org.springframework.batch.item.file.FlatFileItemReader;
//import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
//import org.springframework.batch.item.file.mapping.DefaultLineMapper;
//import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.io.ClassPathResource;
//
//import javax.sql.DataSource;
//
//@Configuration
//public class CsvBatchJobConfig {
//
//
// @Autowired
// JobBuilderFactory jobBuilderFactory;
// @Autowired
// StepBuilderFactory stepBuilderFactory;
// @Autowired
// DataSource dataSource;
//
//
// @Bean
// @StepScope
// FlatFileItemReader<Person> itemReader() {
// FlatFileItemReader<Person> reader = new FlatFileItemReader<>();
// reader.setLinesToSkip(1);
// reader.setResource(new ClassPathResource("data.csv"));
// reader.setLineMapper(new DefaultLineMapper<Person>() {{
// setLineTokenizer(new DelimitedLineTokenizer() {
// {
// setNames("id", "username", "address", "gender");
// //配置列与列之间的间隔符(将通过间隔符对每一行的数据进行切分),最后设置要映射的实体类属性即可
// setDelimiter(",");
// }
// });
// setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {
// {
// setTargetType(Person.class);
// }
// });
//
// }});
// return reader;
// }
//
// @Bean
// public JdbcBatchItemWriter jdbcBatchItemWriter() {
// JdbcBatchItemWriter writer = new JdbcBatchItemWriter();
// writer.setDataSource(dataSource);
// writer.setSql("insert into person(id, username,address,gender)" + "values (:id, :username, :address, :gender)");
// writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
// return writer;
// }
//
// @Bean
// Step csvStep() {
// return stepBuilderFactory.get("csvStep")
// .<Person, Person>chunk(2)
// .reader(itemReader())
// .writer(jdbcBatchItemWriter())
// .build();
// }
//
// @Bean
// Job csvJob() {
// return jobBuilderFactory.get("csvJob")
// .start(csvStep())
// .build();
//
// }
//}
......@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kwan.springbootkwan.entity.Chatbot;
import com.kwan.springbootkwan.entity.Result;
import com.kwan.springbootkwan.entity.dto.ChatbotDTO;
import com.kwan.springbootkwan.service.ChatbotService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
......@@ -67,12 +68,12 @@ public class ChatbotController {
pageParm.setCurrent(page);
pageParm.setSize(pageSize);
QueryWrapper<Chatbot> wrapper = new QueryWrapper<>();
wrapper.orderByDesc("id"); // 按照 id 字段降序排列
wrapper.eq("is_delete", 0); // 按照 id 字段降序排列
wrapper.orderByDesc("id");
wrapper.eq("is_delete", 0);
if (StringUtils.isNotEmpty(question)) {
wrapper.like("question", question);
}
return Result.ok(this.chatbotService.page(pageParm, wrapper));
return Result.ok(ChatbotDTO.Converter.INSTANCE.from(this.chatbotService.page(pageParm, wrapper)));
}
/**
......
......@@ -13,7 +13,6 @@ import java.util.Date;
* @date : 2023/7/11 18:03
*/
@Data
@SuppressWarnings("serial")
public class Chatbot extends Model<Chatbot> {
private Integer id;
private String question;
......
package com.kwan.springbootkwan.entity;
import com.baomidou.mybatisplus.core.metadata.IPage;
import java.io.Serializable;
import java.util.List;
public class PageBean<T> implements Serializable {
private static final long serialVersionUID = 8656597559014685635L;
private long totalElements;
private List<T> content;
private long number;
private long size;
private long totalPages;
private long numberOfElements;
public PageBean() {
}
public PageBean(List<T> list) {
if (list != null) {
this.number = 1L;
this.size = (long) list.size();
this.totalElements = (long) list.size();
this.totalPages = 1L;
this.content = list;
this.numberOfElements = (long) list.size();
}
}
public PageBean(IPage<T> page) {
if (page != null) {
this.number = page.getCurrent();
this.size = page.getSize();
this.totalElements = page.getTotal();
this.totalPages = page.getPages();
this.content = page.getRecords();
if (this.content != null && !this.content.isEmpty()) {
this.numberOfElements = (long) this.content.size();
} else {
this.numberOfElements = 0L;
}
}
}
public List<T> getContent() {
return this.content;
}
public void setContent(List<T> content) {
this.content = content;
}
public long getSize() {
return this.size;
}
public void setSize(long size) {
this.size = size;
}
public long getTotalElements() {
return this.totalElements;
}
public void setTotalElements(long totalElements) {
this.totalElements = totalElements;
}
public long getNumber() {
return this.number;
}
public void setNumber(long number) {
this.number = number;
}
public long getTotalPages() {
return this.totalPages;
}
public void setTotalPages(long totalPages) {
this.totalPages = totalPages;
}
public long getNumberOfElements() {
return this.numberOfElements;
}
public void setNumberOfElements(long numberOfElements) {
this.numberOfElements = numberOfElements;
}
}
\ No newline at end of file
package com.kwan.springbootkwan.entity.dto;
import com.kwan.springbootkwan.entity.Chatbot;
import com.kwan.springbootkwan.mapstruct.FromConverter;
import lombok.Data;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;
import java.util.Date;
@Data
public class ChatbotDTO {
private Integer id;
private String question;
private String response;
private Date createTime;
private Integer isDelete;
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface Converter extends FromConverter<ChatbotDTO, Chatbot> {
Converter INSTANCE = Mappers.getMapper(Converter.class);
}
}
\ No newline at end of file
package com.kwan.springbootkwan.mapstruct;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.kwan.springbootkwan.entity.PageBean;
import java.util.List;
public interface FromConverter<T, FROM> {
T from(FROM from);
List<T> from(List<FROM> list);
default PageBean<T> from(PageBean<FROM> page) {
PageBean<T> result = new PageBean<>();
result.setTotalElements(page.getTotalElements());
result.setNumber(page.getNumber());
result.setSize(page.getSize());
result.setTotalPages(page.getTotalPages());
result.setNumberOfElements(page.getNumberOfElements());
result.setContent(this.from(page.getContent()));
return result;
}
default PageBean<T> from(IPage<FROM> page) {
PageBean<T> result = new PageBean<>();
result.setTotalElements((int) page.getTotal());
result.setNumber((int) page.getCurrent());
result.setSize((int) page.getSize());
result.setTotalPages((int) page.getPages());
result.setNumberOfElements(page.getRecords().size());
result.setContent(this.from(page.getRecords()));
return result;
}
}
\ No newline at end of file
package com.kwan.springbootkwan.mapstruct;
import org.mapstruct.MappingTarget;
public interface PropertyCopier<TARGET, T> {
void copy(@MappingTarget TARGET target, T t);
}
\ No newline at end of file
package com.kwan.springbootkwan.mapstruct;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.kwan.springbootkwan.entity.PageBean;
import java.util.List;
public interface ToConverter<T, TO> {
TO to(T t);
List<TO> to(List<T> list);
default PageBean<TO> to(PageBean<T> page) {
PageBean<TO> result = new PageBean<>();
result.setTotalElements(page.getTotalElements());
result.setNumber(page.getNumber());
result.setSize(page.getSize());
result.setTotalPages(page.getTotalPages());
result.setNumberOfElements(page.getNumberOfElements());
result.setContent(this.to(page.getContent()));
return result;
}
default PageBean<TO> to(IPage<T> page) {
PageBean<TO> result = new PageBean<>();
result.setTotalElements((int) page.getTotal());
result.setNumber((int) page.getCurrent());
result.setSize((int) page.getSize());
result.setTotalPages((int) page.getPages());
result.setNumberOfElements(page.getRecords().size());
result.setContent(this.to(page.getRecords()));
return result;
}
}
\ No newline at end of file
package com.kwan.springbootkwan.mapstruct;
public interface TwoWayConverter<SRC, DEST> extends ToConverter<SRC, DEST>, FromConverter<SRC, DEST> {
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册