Juria 2 months ago
parent a0a27529d3
commit 32dd1707df

@ -0,0 +1,121 @@
<?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>
<groupId>com</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>demo</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!--easy excel依赖-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.18</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</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>
<!-- excel 导出工具 -->
<!-- Apache POI的核心库用于处理HSSF格式的Excel.xls文件 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version> <!-- 选择合适的版本 -->
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version> <!-- 选择合适的版本 -->
</dependency>
<!-- Apache Commons IO for file handling (optional but useful) -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version> <!-- 选择合适的版本 -->
</dependency>
<!-- Apache POI的OOXML模式库通常与poi-ooxml一起使用提供了处理OOXML格式文件时所需的XML模式定义 -->
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.demo.DemoApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

@ -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);
}
}

@ -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<Clazz> 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";
}
}

@ -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<Student> students = reader.readAll(Student.class);
studentMapper.batchInsert(students);
}
}

@ -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<Student> getStudent(){
return studentMapper.findAll();
}
@PostMapping("/set")
public String addStudent(@RequestBody Student student){
studentMapper.save(student);
return "success";
}
@GetMapping("/{clazz}")
public List<Student> getStudent1(@PathVariable("clazz") String clazz){
return studentMapper.findByclazz(clazz);
}
@PutMapping("/update")
public String updateStudent(@RequestBody Student student){
studentMapper.updateByname(student);
return "success";
}
}

@ -0,0 +1,9 @@
package com.demo.entity;
import lombok.Data;
@Data
public class Clazz {
private String name;
private int id;
}

@ -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;
}

@ -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<Clazz> 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);
}

@ -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<Student> 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<Student> findByclazz(String clazz);
@Update("update students set point = #{point} where name = #{name}")
@Transactional
void updateByname(Student student);
@Insert({
"<script>",
"INSERT INTO students (id, name, point, clazz) VALUES ",
"<foreach collection='list' item='student' separator=','>",
"(#{student.id}, #{student.name}, #{student.point}, #{student.clazz})",
"</foreach>",
"</script>"
})
void batchInsert(@Param("list") List<Student> list);
}

@ -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

@ -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() {
}
}
Loading…
Cancel
Save