application
spring:
datasource:
url: jdbc:mysql://localhost:3306/test11
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:com/southwind/mapper/xml/*.xml
server:
port: 8181
CrosConfiguration
package com.southwind.configuration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class CrosConfiguration implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
.allowCredentials(true)
.maxAge(3600)
.allowedHeaders("*");
}
}
FruitController
package com.southwind.controller;
import com.southwind.entity.Fruit;
import com.southwind.service.FruitService;
import com.southwind.vo.BarVO;
import com.southwind.vo.PieVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import java.util.List;
/**
* <p>
* 前端控制器
* </p>
*
* @author admin
* @since 2021-03-31
*/
@RestController
@RequestMapping("//fruit")
public class FruitController {
@Autowired
private FruitService fruitService;
@GetMapping("/list")
public List<Fruit> list(){
return this.fruitService.list();
}
@DeleteMapping("/delete/{id}")
public boolean delete(@PathVariable("id") Integer id){
return this.fruitService.removeById(id);
}
@GetMapping("/find/{id}")
public Fruit find(@PathVariable("id") Integer id){
return this.fruitService.getById(id);
}
@PutMapping("/update")
public boolean update(@RequestBody Fruit fruit){
return this.fruitService.updateById(fruit);
}
@PostMapping("/add")
public boolean add(@RequestBody Fruit fruit){
return this.fruitService.save(fruit);
}
@GetMapping("/barVO")
public BarVO barVO(){
return this.fruitService.barVOList();
}
@GetMapping("/pieVO")
public List<PieVO> pieVO(){
return this.fruitService.pieVOList();
}
}
entity
package com.southwind.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* <p>
*
* </p>
*
* @author admin
* @since 2021-03-31
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class Fruit implements Serializable {
private static final long serialVersionUID=1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String name;
private Integer sale;
private String icon;
}
FruitMapper
<?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.southwind.mapper.FruitMapper">
</mapper>
FruitMapper
package com.southwind.mapper;
import com.southwind.entity.Fruit;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* <p>
* Mapper 接口
* </p>
*
* @author admin
* @since 2021-03-31
*/
public interface FruitMapper extends BaseMapper<Fruit> {
}
FruitServiceImpl
package com.southwind.mapper;
import com.southwind.entity.Fruit;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* <p>
* Mapper 接口
* </p>
*
* @author admin
* @since 2021-03-31
*/
public interface FruitMapper extends BaseMapper<Fruit> {
}
FruitService
package com.southwind.service;
import com.southwind.entity.Fruit;
import com.baomidou.mybatisplus.extension.service.IService;
import com.southwind.vo.BarVO;
import com.southwind.vo.PieVO;
import java.util.List;
/**
* <p>
* 服务类
* </p>
*
* @author admin
* @since 2021-03-31
*/
public interface FruitService extends IService<Fruit> {
public BarVO barVOList();
public List<PieVO> pieVOList();
}
DataUtil
package com.southwind.util;
import java.util.HashMap;
import java.util.Map;
public class DataUtil {
public static Map<String,String> createItemStyle(Integer sale){
String color = "";
if(sale<60){
color = "#96dee8";
}
if(sale>60 && sale<160){
color = "#c4ebad";
}
if(sale>160 && sale<300){
color = "#6be6c1";
}
if(sale>300 && sale<500){
color = "#3fb1e3";
}
if(sale>500){
color = "#a0a7e6";
}
Map<String,String> map = new HashMap<>();
map.put("color", color);
return map;
}
}
BarVO
package com.southwind.vo;
import lombok.Data;
import java.util.List;
@Data
public class BarVO {
private List<String> names;
private List<DataVO> values;
}
DataVO
package com.southwind.vo;
import lombok.Data;
import java.util.Map;
@Data
public class DataVO {
private Integer value;
private Map<String,String> itemStyle;
}
PieVO
package com.southwind.vo;
import lombok.Data;
import java.util.Map;
@Data
public class PieVO {
private Integer value;
private Map<String,String> itemStyle;
private String name;
}
FruitApplication
package com.southwind;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.southwind.mapper")
public class FruitApplication {
public static void main(String[] args) {
SpringApplication.run(FruitApplication.class, args);
}
}