Browse Source

树形数据优雅组装

pull/2/head
马府强 1 year ago
parent
commit
95d133296b
  1. 8
      treenodedata/.idea/.gitignore
  2. 1
      treenodedata/.idea/.name
  3. 18
      treenodedata/.idea/compiler.xml
  4. 6
      treenodedata/.idea/encodings.xml
  5. 25
      treenodedata/.idea/jarRepositories.xml
  6. 11
      treenodedata/.idea/misc.xml
  7. 9
      treenodedata/.idea/modules.xml
  8. 9
      treenodedata/.idea/mybatisPlus.iml
  9. 124
      treenodedata/.idea/uiDesigner.xml
  10. 8
      treenodedata/mp-demo/mp-demo.iml
  11. 110
      treenodedata/mp-demo/pom.xml
  12. 21
      treenodedata/mp-demo/src/main/java/com/woniu/mp/MpDemoApplication.java
  13. 30
      treenodedata/mp-demo/src/main/java/com/woniu/mp/controller/CategoryController.java
  14. 71
      treenodedata/mp-demo/src/main/java/com/woniu/mp/entity/CategoryEntity.java
  15. 12
      treenodedata/mp-demo/src/main/java/com/woniu/mp/mapper/CategoryDao.java
  16. 10
      treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/first/Source.java
  17. 24
      treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/first/SourceTargetMapper.java
  18. 17
      treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/first/Target.java
  19. 10
      treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/four/SimpleService.java
  20. 9
      treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/four/Student.java
  21. 8
      treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/four/StudentDto.java
  22. 20
      treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/four/StudentMapper.java
  23. 10
      treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/second/Customer.java
  24. 15
      treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/second/CustomerDto.java
  25. 21
      treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/second/CustomerMapper.java
  26. 16
      treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/second/TimeAndFormat.java
  27. 22
      treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/third/Bike.java
  28. 21
      treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/third/BikeDto.java
  29. 17
      treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/third/Car.java
  30. 17
      treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/third/CarDto.java
  31. 16
      treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/third/ToEntity.java
  32. 20
      treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/third/TransportationMapper.java
  33. 18
      treenodedata/mp-demo/src/main/java/com/woniu/mp/service/CategoryService.java
  34. 52
      treenodedata/mp-demo/src/main/java/com/woniu/mp/service/impl/CategoryServiceImpl.java
  35. 24
      treenodedata/mp-demo/src/main/java/com/woniu/mp/util/MergeUtil.java
  36. 56
      treenodedata/mp-demo/src/main/java/com/woniu/mp/util/R.java
  37. 16
      treenodedata/mp-demo/src/main/resources/application.yaml
  38. 5
      treenodedata/mp-demo/src/main/resources/mapper/UserMapper.xml
  39. 12
      treenodedata/mp-demo/src/test/java/com/woniu/mp/MpDemoApplicationTests.java
  40. 69
      treenodedata/mp-demo/src/test/java/com/woniu/mp/mapper/TestTransactionTest.java

8
treenodedata/.idea/.gitignore

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/../../../:\mybatisPlus\.idea/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

1
treenodedata/.idea/.name

@ -0,0 +1 @@
mp-demo

18
treenodedata/.idea/compiler.xml

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="mp-demo" />
</profile>
</annotationProcessing>
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="mp-demo" options="-parameters" />
</option>
</component>
</project>

6
treenodedata/.idea/encodings.xml

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/mp-demo/src/main/java" charset="UTF-8" />
</component>
</project>

25
treenodedata/.idea/jarRepositories.xml

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="http://maven.aliyun.com/nexus/content/groups/public" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>

11
treenodedata/.idea/misc.xml

@ -0,0 +1,11 @@
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/mp-demo/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK" />
</project>

9
treenodedata/.idea/modules.xml

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/mp-demo/mp-demo.iml" filepath="$PROJECT_DIR$/mp-demo/mp-demo.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/mybatisPlus.iml" filepath="$PROJECT_DIR$/.idea/mybatisPlus.iml" />
</modules>
</component>
</project>

9
treenodedata/.idea/mybatisPlus.iml

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

124
treenodedata/.idea/uiDesigner.xml

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

8
treenodedata/mp-demo/mp-demo.iml

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="FacetManager">
<facet type="Spring" name="Spring">
<configuration />
</facet>
</component>
</module>

110
treenodedata/mp-demo/pom.xml

@ -0,0 +1,110 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.woniu.mp</groupId>
<artifactId>mp-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mp-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<org.mapstruct.version>1.5.5.Final</org.mapstruct.version>
<org.projectlombok.version>1.18.30</org.projectlombok.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<!--Swagger 依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!--Swagger-ui 依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.11</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

21
treenodedata/mp-demo/src/main/java/com/woniu/mp/MpDemoApplication.java

@ -0,0 +1,21 @@
package com.woniu.mp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* <p>
* 基于mybatisPlus的代码生成器插件
* </p>
* @author woniu
* @since 2023-09-24
*/
@SpringBootApplication
public class MpDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MpDemoApplication.class, args);
}
}

30
treenodedata/mp-demo/src/main/java/com/woniu/mp/controller/CategoryController.java

@ -0,0 +1,30 @@
package com.woniu.mp.controller;
import cn.hutool.core.lang.tree.Tree;
import com.woniu.mp.service.CategoryService;
import com.woniu.mp.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("product/category")
public class CategoryController {
@Autowired
private CategoryService categoryService;
/**
* 查询出所有分类以及子分类以树形结构组装起来列表
*/
@RequestMapping("/list/tree")
public R list(){
List<Tree<Long>> trees = categoryService.listWithTree();
return R.ok().put("data", trees);
}
}

71
treenodedata/mp-demo/src/main/java/com/woniu/mp/entity/CategoryEntity.java

@ -0,0 +1,71 @@
package com.woniu.mp.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
* 商品三级分类
*
* @author 夏沫止水
* @email HeJieLin@gulimall.com
* @date 2020-05-22 19:00:18
*/
@Data
@TableName("pms_category")
public class CategoryEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 分类id
*/
@TableId
private Long catId;
/**
* 分类名称
*/
private String name;
/**
* 父分类id
*/
private Long parentCid;
/**
* 层级
*/
private Integer catLevel;
/**
* 是否显示[0-不显示1显示]
*/
@TableLogic(value = "1",delval = "0")
private Integer showStatus;
/**
* 排序
*/
private Integer sort;
/**
* 图标地址
*/
private String icon;
/**
* 计量单位
*/
private String productUnit;
/**
* 商品数量
*/
private Integer productCount;
/**
* 所有子分类
*/
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@TableField(exist = false)
private List<CategoryEntity> children;
}

12
treenodedata/mp-demo/src/main/java/com/woniu/mp/mapper/CategoryDao.java

@ -0,0 +1,12 @@
package com.woniu.mp.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniu.mp.entity.CategoryEntity;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface CategoryDao extends BaseMapper<CategoryEntity> {
}

10
treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/first/Source.java

@ -0,0 +1,10 @@
package com.woniu.mp.mapstruct.first;
import lombok.Data;
@Data
public class Source {
private String stringProp;
private Long longProp;
}

24
treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/first/SourceTargetMapper.java

@ -0,0 +1,24 @@
package com.woniu.mp.mapstruct.first;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;
/**
* mapstruct这么用同事也纷纷模仿
* mapstruct几个常用小技巧
*/
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface SourceTargetMapper {
/**
* 常量转换
*/
@Mapping(target = "stringProperty", source = "stringProp", defaultValue = "undefined")
@Mapping(target = "longProperty", source = "longProp", defaultValue = "-1l")
@Mapping(target = "stringConstant", constant = "Constant Value")
@Mapping(target = "integerConstant", constant = "14")
@Mapping(target = "longWrapperConstant", constant = "3001L")
@Mapping(target = "dateConstant", dateFormat = "yyyy-MM-dd", constant = "2023-09-01-")
Target sourceToTarget(Source s);
}

17
treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/first/Target.java

@ -0,0 +1,17 @@
package com.woniu.mp.mapstruct.first;
import lombok.Data;
import java.util.Date;
@Data
public class Target {
private String stringProperty;
private long longProperty;
private String stringConstant;
private Integer integerConstant;
private Long longWrapperConstant;
private Date dateConstant;
}

10
treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/four/SimpleService.java

@ -0,0 +1,10 @@
package com.woniu.mp.mapstruct.four;
import org.springframework.stereotype.Component;
@Component
public class SimpleService {
public String formatName(String name) {
return "您的名字是:" + name;
}
}

9
treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/four/Student.java

@ -0,0 +1,9 @@
package com.woniu.mp.mapstruct.four;
import lombok.Data;
@Data
public class Student {
private String name;
}

8
treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/four/StudentDto.java

@ -0,0 +1,8 @@
package com.woniu.mp.mapstruct.four;
import lombok.Data;
@Data
public class StudentDto {
private String name;
}

20
treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/four/StudentMapper.java

@ -0,0 +1,20 @@
package com.woniu.mp.mapstruct.four;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;
import org.springframework.beans.factory.annotation.Autowired;
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public abstract class StudentMapper {
@Autowired
protected SimpleService simpleService;
/**
* 转换的时候如何注入springBean
*/
@Mapping(target = "name", expression = "java(simpleService.formatName(source.getName()))")
public abstract Student map(StudentDto source);
}

10
treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/second/Customer.java

@ -0,0 +1,10 @@
package com.woniu.mp.mapstruct.second;
import lombok.Data;
@Data
public class Customer {
private String id;
private String name;
private TimeAndFormat timeAndFormat;
}

15
treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/second/CustomerDto.java

@ -0,0 +1,15 @@
package com.woniu.mp.mapstruct.second;
import lombok.Data;
import java.util.Date;
@Data
public class CustomerDto {
public Long id;
public String customerName;
private String format;
private Date time;
}

21
treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/second/CustomerMapper.java

@ -0,0 +1,21 @@
package com.woniu.mp.mapstruct.second;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;
import java.util.UUID;
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING, imports = UUID.class)
public interface CustomerMapper {
/**
* 场景二转换中调用表达式
*/
@Mapping(target = "timeAndFormat",
expression = "java( new TimeAndFormat( s.getTime(), s.getFormat() ) )")
@Mapping(target = "id", source = "id", defaultExpression = "java( UUID.randomUUID().toString() )")
Customer toCustomer(CustomerDto s);
}

16
treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/second/TimeAndFormat.java

@ -0,0 +1,16 @@
package com.woniu.mp.mapstruct.second;
import lombok.Data;
import java.util.Date;
@Data
public class TimeAndFormat {
private Date time;
private String format;
public TimeAndFormat(Date time, String format) {
this.time = time;
this.format = format;
}
}

22
treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/third/Bike.java

@ -0,0 +1,22 @@
package com.woniu.mp.mapstruct.third;
import lombok.Data;
import java.util.Date;
@Data
public class Bike {
/**
* 唯一id
*/
private String id;
private Date creationDate;
/**
* 品牌
*/
private String brandName;
}

21
treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/third/BikeDto.java

@ -0,0 +1,21 @@
package com.woniu.mp.mapstruct.third;
import lombok.Data;
import java.util.Date;
@Data
public class BikeDto {
/**
* 唯一id
*/
private String id;
private Date creationDate;
/**
* 品牌
*/
private String brand;
}

17
treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/third/Car.java

@ -0,0 +1,17 @@
package com.woniu.mp.mapstruct.third;
import lombok.Data;
import java.util.Date;
@Data
public class Car {
/**
* 唯一id
*/
private String id;
private Date creationDate;
private String otherName;
}

17
treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/third/CarDto.java

@ -0,0 +1,17 @@
package com.woniu.mp.mapstruct.third;
import lombok.Data;
import java.util.Date;
@Data
public class CarDto {
/**
* 唯一id
*/
private String id;
private Date creationDate;
private String other;
}

16
treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/third/ToEntity.java

@ -0,0 +1,16 @@
package com.woniu.mp.mapstruct.third;
import org.mapstruct.Mapping;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
//通用注解
@Retention(RetentionPolicy.CLASS)
//自动生成当前日期
@Mapping(target = "creationDate", expression = "java(new java.util.Date())")
//忽略id
@Mapping(target = "id", ignore = true)
public @interface ToEntity {
}

20
treenodedata/mp-demo/src/main/java/com/woniu/mp/mapstruct/third/TransportationMapper.java

@ -0,0 +1,20 @@
package com.woniu.mp.mapstruct.third;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface TransportationMapper {
/**
* 类共用属性如何复用
*/
@ToEntity
@Mapping( target = "brandName", source = "brand")
Bike map(BikeDto source);
@ToEntity
@Mapping( target = "otherName", source = "other")
Car map(CarDto source);
}

18
treenodedata/mp-demo/src/main/java/com/woniu/mp/service/CategoryService.java

@ -0,0 +1,18 @@
package com.woniu.mp.service;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.service.IService;
import com.woniu.mp.entity.CategoryEntity;
import java.util.List;
public interface CategoryService extends IService<CategoryEntity> {
List<Tree<Long>> listWithTree();
}

52
treenodedata/mp-demo/src/main/java/com/woniu/mp/service/impl/CategoryServiceImpl.java

@ -0,0 +1,52 @@
package com.woniu.mp.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.mp.entity.CategoryEntity;
import com.woniu.mp.mapper.CategoryDao;
import com.woniu.mp.service.CategoryService;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
/**
* 树形数据还可以这样组装真优雅
*/
public List<Tree<Long>> listWithTree() {
List<CategoryEntity> entities = baseMapper.selectList(null);
if (!CollectionUtil.isEmpty(entities)) {
List<TreeNode<Long>> list = entities.
stream().
map(CategoryServiceImpl::getLongTreeNode).
collect(Collectors.toList());
return TreeUtil.build(list, 0L);
}
return Collections.emptyList();
}
private static TreeNode<Long> getLongTreeNode(CategoryEntity item) {
TreeNode<Long> treeNode = new TreeNode<>();
treeNode.setId(item.getCatId());
treeNode.setParentId(item.getParentCid());
treeNode.setName(item.getName());
treeNode.setWeight(item.getSort());
Map<String, Object> extra = new HashMap<>();
extra.put("showStatus", item.getShowStatus());
extra.put("catLevel", item.getCatLevel());
extra.put("icon", item.getIcon());
extra.put("productUnit", item.getProductUnit());
extra.put("productCount", item.getProductCount());
treeNode.setExtra(extra);
return treeNode;
}
}

24
treenodedata/mp-demo/src/main/java/com/woniu/mp/util/MergeUtil.java

@ -0,0 +1,24 @@
package com.woniu.mp.util;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
public class MergeUtil {
private static final CopyOptions options = CopyOptions.create().setIgnoreNullValue(true).setOverride(false);
private static final CopyOptions optionsAllowOverride = CopyOptions.create().setIgnoreNullValue(true).setOverride(true);
//将sourceBean中的属性合并到tagetBean,忽略Null值,非Null值不允许覆盖
public static Object merge(Object sourceBean, Object targetBean) {
BeanUtil.copyProperties(sourceBean, targetBean, options);
return targetBean;
}
//将sourceBean中的属性合并到tagetBean,忽略Null值,非Null值允许覆盖
public static Object mergeAllowOverride(Object sourceBean, Object targetBean) {
BeanUtil.copyProperties(sourceBean, targetBean, optionsAllowOverride);
return targetBean;
}
}

56
treenodedata/mp-demo/src/main/java/com/woniu/mp/util/R.java

@ -0,0 +1,56 @@
package com.woniu.mp.util;
import java.util.HashMap;
import java.util.Map;
public class R extends HashMap<String, Object> {
private static final long serialVersionUID = 1L;
public R setData(Object data) {
put("data",data);
return this;
}
public R() {
put("code", 0);
put("msg", "success");
}
public static R error(int code, String msg) {
R r = new R();
r.put("code", code);
r.put("msg", msg);
return r;
}
public static R ok(String msg) {
R r = new R();
r.put("msg", msg);
return r;
}
public static R ok(Map<String, Object> map) {
R r = new R();
r.putAll(map);
return r;
}
public static R ok() {
return new R();
}
public R put(String key, Object value) {
super.put(key, value);
return this;
}
public Integer getCode() {
return (Integer) this.get("code");
}
}

16
treenodedata/mp-demo/src/main/resources/application.yaml

@ -0,0 +1,16 @@
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
hikari:
#最大连接数量,默认10
maximum-pool-size: 1
logging:
level:
com.itheima: debug
pattern:
dateformat: HH:mm:ss
mybatis:
mapper-locations: classpath*:mapper/*.xml

5
treenodedata/mp-demo/src/main/resources/mapper/UserMapper.xml

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.woniu.mp.mapper.UserMapper">
</mapper>

12
treenodedata/mp-demo/src/test/java/com/woniu/mp/MpDemoApplicationTests.java

@ -0,0 +1,12 @@
package com.woniu.mp;
import org.junit.jupiter.api.Test;
class MpDemoApplicationTests {
@Test
void contextLoads() {
}
}

69
treenodedata/mp-demo/src/test/java/com/woniu/mp/mapper/TestTransactionTest.java

@ -0,0 +1,69 @@
package com.woniu.mp.mapper;
import com.woniu.mp.mapstruct.first.Source;
import com.woniu.mp.mapstruct.first.SourceTargetMapper;
import com.woniu.mp.mapstruct.first.Target;
import com.woniu.mp.mapstruct.second.Customer;
import com.woniu.mp.mapstruct.second.CustomerDto;
import com.woniu.mp.mapstruct.second.CustomerMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.Date;
@SpringBootTest
class TestTransactionTest {
@Resource
TestTransaction testTransaction;
@Resource
SourceTargetMapper targetMapper;
@Resource
CustomerMapper customerMapper;
/**
* 常量转换
*/
@Test
void testFirst() {
Source source = new Source();
source.setLongProp(1l);
source.setStringProp("woniu");
Target target = targetMapper.sourceToTarget(source);
System.out.println(target);
}
/**
* 转换中调用表达式
*/
@Test
void testSecond() {
CustomerDto customerDto = new CustomerDto();
customerDto.setId(1L);
customerDto.setTime(new Date());
customerDto.setCustomerName("woniu");
customerDto.setFormat("yyyy-MM");
Customer customer = customerMapper.toCustomer(customerDto);
System.out.println(customer);
}
@Test
void testThird() {
CustomerDto customerDto = new CustomerDto();
customerDto.setId(1L);
customerDto.setTime(new Date());
customerDto.setCustomerName("woniu");
customerDto.setFormat("yyyy-MM");
Customer customer = customerMapper.toCustomer(customerDto);
System.out.println(customer);
}
}
Loading…
Cancel
Save