1. Create the project
The code structure
2. Add dependencies
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> < artifactId > mybatis - spring - the boot - starter < / artifactId > < version > 2.2.0 < / version > < / dependency > < the dependency > <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.alibaba</groupId> </artifactId> </artifactId> <version>2.1.1</version> </dependency>
3, Application. Properties configuration
URL = JDBC: MySQL :/// The name of the database? ServerTimezone = GMT % 2 b8 & characterEncoding = # utf8 database password is spring. The datasource. The username = account spring. The datasource. Password = password # MyBatis configuration myBatis. Mapper-Locations =classpath:/mapper/*.xml logging.level.com.zj=debug
4. Write the POJO layer
@Data public class User implements Serializable { private static final long serialVersionUID = 619270056917451517L; // @datetimeformat (" YYYY MM MM DD MM HH MM MINUTES SS MM ") // @ColumnWidth(20) @ExcelProperty(value = "",index = 0) private Integer ID; @ExcelProperty(value = "Name ",index = 1) private String Name; }
5. Write the Mapper layer
@Mapper
public interface UserMapper {
int insert(User user);
}
6. Write usermap.xml
<? The 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.zj.mapper.UserMapper"> <insert id="insert"> insert into user01(id,name)values (#{id},#{name}); </insert> </mapper>
7. Write UserService
public interface UserService {
int insert(User user);
}
8. Write the implementation class
@Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public int insert(User user) { return userMapper.insert(user); }}
9. Overwrite listeners (not managed by Spring)
public class UserListener extends AnalysisEventListener<User> { private UserService userService; public UserListener(UserService userService){ this.userService=userService; } public void invoke(User User, Override) public void invoke(User User, Override) public void invoke(User User, Override) AnalysisContext) {System.out.println(" Start executing "); saveData(user); } private void saveData(User user) { userService.insert(user); System.out.println(" Save successfully "); } @Override public void invokeHeadMap(Map<Integer, String> HeadMap, String>) AnalysisContext Context) {System.out.println(" header: "+headMap); } public void doAfterAllAnalysed(analysisContext analysisContext) {System.out.println(" out ");} public void doAfterAllAnalysed(analysisContext analysisContext); }}
10. Controller layer writing
@RestController public class UserController { @Autowired private UserService userService; @PostMapping("upload") public String upload(MultipartFile file) throws IOException { EasyExcel.read(file.getInputStream(), User.class,new UserListener(userService)).sheet().doRead(); // sheet() default is 0 return "success"; }}
Postman test
The IDEA of the background
The database
Upload data successfully