diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..d13c284 --- /dev/null +++ b/pom.xml @@ -0,0 +1,121 @@ + + + 4.0.0 + com + demo + 0.0.1-SNAPSHOT + demo + demo + + 1.8 + UTF-8 + UTF-8 + 2.6.13 + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.2.2 + + + + cn.hutool + hutool-all + 5.8.18 + + + org.apache.poi + poi-ooxml + 5.2.3 + + + + com.mysql + mysql-connector-j + runtime + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-web + + + + + org.apache.poi + poi + 5.2.3 + + + + org.apache.poi + poi-ooxml + 5.2.3 + + + + + commons-io + commons-io + 2.11.0 + + + + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + UTF-8 + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring-boot.version} + + com.demo.DemoApplication + true + + + + repackage + + repackage + + + + + + + + diff --git a/src/main/java/com/demo/DemoApplication.java b/src/main/java/com/demo/DemoApplication.java new file mode 100644 index 0000000..0b8bb6d --- /dev/null +++ b/src/main/java/com/demo/DemoApplication.java @@ -0,0 +1,16 @@ +package com.demo; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +@MapperScan("com.demo.mapper") + +public class DemoApplication { + + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } + +} diff --git a/src/main/java/com/demo/controller/ClazzController.java b/src/main/java/com/demo/controller/ClazzController.java new file mode 100644 index 0000000..099e04a --- /dev/null +++ b/src/main/java/com/demo/controller/ClazzController.java @@ -0,0 +1,40 @@ +package com.demo.controller; + +import com.demo.entity.Clazz; +import com.demo.entity.Student; +import com.demo.mapper.ClazzMapper; +import org.apache.ibatis.annotations.Delete; +import org.apache.ibatis.annotations.Update; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.List; + +@RestController +@RequestMapping("/clazz") +public class ClazzController { + @Resource + ClazzMapper clazzMapper; + + @PostMapping("/save") + public String addClazz(@RequestBody Clazz clazz){ + clazzMapper.save(clazz); + return "success"; + } + @GetMapping("/list") + public List getClazz(){ + return clazzMapper.findAll(); + } + @DeleteMapping("/{name}") + public String deleteClazz(@PathVariable("name") String name){ + clazzMapper.deleteByname(name); + return "success"; + } + @PutMapping("/update") + public String updateClazz(@RequestBody Clazz clazz){ + clazzMapper.updateById(clazz); + return "success"; + } + +} diff --git a/src/main/java/com/demo/controller/ExcelController.java b/src/main/java/com/demo/controller/ExcelController.java new file mode 100644 index 0000000..4e076fc --- /dev/null +++ b/src/main/java/com/demo/controller/ExcelController.java @@ -0,0 +1,28 @@ +package com.demo.controller; + +import cn.hutool.poi.excel.ExcelReader; +import cn.hutool.poi.excel.ExcelUtil; +import com.demo.entity.Student; +import com.demo.mapper.StudentMapper; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; +import java.io.InputStream; +import java.nio.channels.MulticastChannel; +import java.util.List; + +@RestController +@RequestMapping("/excel") +@CrossOrigin(origins = "*") +public class ExcelController { + @Resource + private StudentMapper studentMapper; + @PostMapping("/import") + public void imp(@RequestBody MultipartFile file)throws Exception{ + InputStream inputStream = file.getInputStream(); + ExcelReader reader = ExcelUtil.getReader(inputStream); + List students = reader.readAll(Student.class); + studentMapper.batchInsert(students); + } +} diff --git a/src/main/java/com/demo/controller/StudentController.java b/src/main/java/com/demo/controller/StudentController.java new file mode 100644 index 0000000..6e74bcb --- /dev/null +++ b/src/main/java/com/demo/controller/StudentController.java @@ -0,0 +1,40 @@ +package com.demo.controller; + +import com.demo.entity.Clazz; +import com.demo.entity.Student; +import com.demo.mapper.StudentMapper; +import lombok.SneakyThrows; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; +import java.util.List; + +@RestController +@RequestMapping("/student") +public class StudentController { + @Resource + StudentMapper studentMapper; + @GetMapping + public List getStudent(){ + return studentMapper.findAll(); + } + @PostMapping("/set") + public String addStudent(@RequestBody Student student){ + studentMapper.save(student); + return "success"; + } + @GetMapping("/{clazz}") + public List getStudent1(@PathVariable("clazz") String clazz){ + + return studentMapper.findByclazz(clazz); + } + @PutMapping("/update") + public String updateStudent(@RequestBody Student student){ + studentMapper.updateByname(student); + return "success"; + } + + + +} diff --git a/src/main/java/com/demo/entity/Clazz.java b/src/main/java/com/demo/entity/Clazz.java new file mode 100644 index 0000000..10ec305 --- /dev/null +++ b/src/main/java/com/demo/entity/Clazz.java @@ -0,0 +1,9 @@ +package com.demo.entity; + +import lombok.Data; + +@Data +public class Clazz { + private String name; + private int id; +} diff --git a/src/main/java/com/demo/entity/Student.java b/src/main/java/com/demo/entity/Student.java new file mode 100644 index 0000000..5889cba --- /dev/null +++ b/src/main/java/com/demo/entity/Student.java @@ -0,0 +1,16 @@ +package com.demo.entity; + + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class Student { + private String id; + private String name; + private int point; + private String clazz; +} diff --git a/src/main/java/com/demo/mapper/ClazzMapper.java b/src/main/java/com/demo/mapper/ClazzMapper.java new file mode 100644 index 0000000..ed1e5de --- /dev/null +++ b/src/main/java/com/demo/mapper/ClazzMapper.java @@ -0,0 +1,25 @@ +package com.demo.mapper; + +import com.demo.entity.Clazz; +import com.demo.entity.Student; +import org.apache.ibatis.annotations.Delete; +import org.apache.ibatis.annotations.Insert; +import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.Update; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.PathVariable; + +import java.util.List; + +public interface ClazzMapper { + @Select("select * from classes") + List findAll(); + @Insert("INSERT INTO classes (name) VALUES (#{name});") + @Transactional + void save(Clazz clazz); + @Delete("delete from classes where name = #{name}") + void deleteByname(String name); + @Update("update classes set name = #{name} where id = #{id}") + @Transactional + void updateById(Clazz clazz); +} diff --git a/src/main/java/com/demo/mapper/StudentMapper.java b/src/main/java/com/demo/mapper/StudentMapper.java new file mode 100644 index 0000000..3e1fd72 --- /dev/null +++ b/src/main/java/com/demo/mapper/StudentMapper.java @@ -0,0 +1,38 @@ +package com.demo.mapper; + +import com.demo.entity.Clazz; +import com.demo.entity.Student; +import org.apache.ibatis.annotations.Insert; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.Update; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +public interface StudentMapper { + @Select("select * from students") + List findAll(); + + @Insert("INSERT INTO students (`id`, `name`, `point`, `clazz`) VALUES (#{id},#{name},#{point},#{clazz});") + @Transactional + void save(Student student); + + @Select("select * from students where clazz = #{clazz}") + List findByclazz(String clazz); + + @Update("update students set point = #{point} where name = #{name}") + @Transactional + void updateByname(Student student); + + @Insert({ + "" + }) + void batchInsert(@Param("list") List list); + +} \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..a32cc6e --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,8 @@ +server: + port: 8090 +spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/demo?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false + username: root + password: 123456 \ No newline at end of file diff --git a/src/test/java/com/demo/DemoApplicationTests.java b/src/test/java/com/demo/DemoApplicationTests.java new file mode 100644 index 0000000..72afc81 --- /dev/null +++ b/src/test/java/com/demo/DemoApplicationTests.java @@ -0,0 +1,13 @@ +package com.demo; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class DemoApplicationTests { + + @Test + void contextLoads() { + } + +}