Commit 6a69d6d744cfd70dcedb5b96553d471db9a0ec35
0 parents
用户信息保存、获取、Excel下载
Showing
9 changed files
with
448 additions
and
0 deletions
pom.xml
0 → 100644
| 1 | +++ a/pom.xml | |
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | |
| 2 | +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| 3 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
| 4 | + <modelVersion>4.0.0</modelVersion> | |
| 5 | + | |
| 6 | + <groupId>zzsy</groupId> | |
| 7 | + <artifactId>cnlive_zzsy</artifactId> | |
| 8 | + <version>0.0.1-SNAPSHOT</version> | |
| 9 | + <packaging>jar</packaging> | |
| 10 | + | |
| 11 | + <name>cnlive_zzsy</name> | |
| 12 | + <description>Demo project for Spring Boot</description> | |
| 13 | + | |
| 14 | + <parent> | |
| 15 | + <groupId>org.springframework.boot</groupId> | |
| 16 | + <artifactId>spring-boot-starter-parent</artifactId> | |
| 17 | + <version>2.0.5.RELEASE</version> | |
| 18 | + <relativePath/> <!-- lookup parent from repository --> | |
| 19 | + </parent> | |
| 20 | + | |
| 21 | + <properties> | |
| 22 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
| 23 | + <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |
| 24 | + <java.version>1.8</java.version> | |
| 25 | + </properties> | |
| 26 | + | |
| 27 | + <dependencies> | |
| 28 | + <dependency> | |
| 29 | + <groupId>org.springframework.boot</groupId> | |
| 30 | + <artifactId>spring-boot-starter</artifactId> | |
| 31 | + </dependency> | |
| 32 | + <dependency> | |
| 33 | + <groupId>mysql</groupId> | |
| 34 | + <artifactId>mysql-connector-java</artifactId> | |
| 35 | + </dependency> | |
| 36 | + <dependency> | |
| 37 | + <groupId>org.springframework.boot</groupId> | |
| 38 | + <artifactId>spring-boot-starter-test</artifactId> | |
| 39 | + <scope>test</scope> | |
| 40 | + </dependency> | |
| 41 | + <dependency> | |
| 42 | + <groupId>org.springframework.boot</groupId> | |
| 43 | + <artifactId>spring-boot-starter-data-jpa</artifactId> | |
| 44 | + <version>RELEASE</version> | |
| 45 | + <scope>compile</scope> | |
| 46 | + </dependency> | |
| 47 | + <dependency> | |
| 48 | + <groupId>org.springframework.boot</groupId> | |
| 49 | + <artifactId>spring-boot-starter-web</artifactId> | |
| 50 | + <version>RELEASE</version> | |
| 51 | + <scope>compile</scope> | |
| 52 | + </dependency> | |
| 53 | + <dependency> | |
| 54 | + <groupId>org.springframework.boot</groupId> | |
| 55 | + <artifactId>spring-boot-starter-web</artifactId> | |
| 56 | + <version>RELEASE</version> | |
| 57 | + <scope>compile</scope> | |
| 58 | + </dependency> | |
| 59 | + | |
| 60 | + <dependency> | |
| 61 | + <groupId>org.apache.poi</groupId> | |
| 62 | + <artifactId>poi-scratchpad</artifactId> | |
| 63 | + <version>3.16</version> | |
| 64 | + </dependency> | |
| 65 | + <dependency> | |
| 66 | + <groupId>org.apache.poi</groupId> | |
| 67 | + <artifactId>poi-ooxml</artifactId> | |
| 68 | + <version>3.16</version> | |
| 69 | + </dependency> | |
| 70 | + </dependencies> | |
| 71 | + | |
| 72 | + <build> | |
| 73 | + <plugins> | |
| 74 | + <plugin> | |
| 75 | + <groupId>org.springframework.boot</groupId> | |
| 76 | + <artifactId>spring-boot-maven-plugin</artifactId> | |
| 77 | + </plugin> | |
| 78 | + </plugins> | |
| 79 | + </build> | |
| 80 | + | |
| 81 | + | |
| 82 | +</project> | ... | ... |
src/main/java/zzsy/CnliveZzsyApplication.java
0 → 100644
| 1 | +++ a/src/main/java/zzsy/CnliveZzsyApplication.java | |
| 1 | +package zzsy; | |
| 2 | + | |
| 3 | +import org.springframework.boot.SpringApplication; | |
| 4 | +import org.springframework.boot.autoconfigure.SpringBootApplication; | |
| 5 | + | |
| 6 | +@SpringBootApplication | |
| 7 | +public class CnliveZzsyApplication { | |
| 8 | + | |
| 9 | + public static void main(String[] args) { | |
| 10 | + SpringApplication.run(CnliveZzsyApplication.class, args); | |
| 11 | + } | |
| 12 | +} | ... | ... |
src/main/java/zzsy/controller/UserMessengeController.java
0 → 100644
| 1 | +++ a/src/main/java/zzsy/controller/UserMessengeController.java | |
| 1 | +package zzsy.controller; | |
| 2 | +/** | |
| 3 | + * Created by 周伟鹏 on 2018/10/16. | |
| 4 | + */ | |
| 5 | + | |
| 6 | +import org.apache.poi.xssf.usermodel.XSSFCell; | |
| 7 | +import org.apache.poi.xssf.usermodel.XSSFRow; | |
| 8 | +import org.apache.poi.xssf.usermodel.XSSFSheet; | |
| 9 | +import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 11 | +import org.springframework.dao.DataIntegrityViolationException; | |
| 12 | +import org.springframework.web.bind.annotation.*; | |
| 13 | +import zzsy.dao.UserMessengeRepository; | |
| 14 | +import zzsy.entity.UserMessenge; | |
| 15 | +import zzsy.utils.ErrorEnum; | |
| 16 | +import zzsy.utils.ResponseBean; | |
| 17 | + | |
| 18 | +import javax.servlet.ServletOutputStream; | |
| 19 | +import javax.servlet.http.HttpServletResponse; | |
| 20 | +import java.io.*; | |
| 21 | +import java.util.List; | |
| 22 | +import java.util.regex.Pattern; | |
| 23 | + | |
| 24 | +/** | |
| 25 | + * @Auther: Administrator | |
| 26 | + * @Date: 2018/10/16 16:19 | |
| 27 | + * @Description: | |
| 28 | + */ | |
| 29 | +@RestController | |
| 30 | +@RequestMapping(value = "/userApi") | |
| 31 | +public class UserMessengeController { | |
| 32 | + @Autowired | |
| 33 | + UserMessengeRepository userMessengeService; | |
| 34 | + | |
| 35 | + @PostMapping("save") | |
| 36 | + public ResponseBean save(@RequestParam(name = "name") String name, @RequestParam(name = "phone")String phone, @RequestParam(name = "company")String company) { | |
| 37 | + ResponseBean responseBean = new ResponseBean(); | |
| 38 | + try { | |
| 39 | + String REGEX_PHONE = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$"; | |
| 40 | + boolean isPhone = Pattern.matches(REGEX_PHONE, phone); | |
| 41 | + if(isPhone){ | |
| 42 | + UserMessenge userMessenge = new UserMessenge(); | |
| 43 | + userMessenge.setName(name); | |
| 44 | + userMessenge.setCompany(company); | |
| 45 | + userMessenge.setPhone(phone); | |
| 46 | + userMessengeService.save(userMessenge); | |
| 47 | + }else{ | |
| 48 | + responseBean = new ResponseBean(ErrorEnum.PHONE_ERROR.getErrorCode(),ErrorEnum.PHONE_ERROR.getErrorMessage()); | |
| 49 | + } | |
| 50 | + | |
| 51 | + }catch (DataIntegrityViolationException e){ | |
| 52 | + responseBean = new ResponseBean(ErrorEnum.PHONE_REPEAT.getErrorCode(),ErrorEnum.PHONE_REPEAT.getErrorMessage()); | |
| 53 | + } | |
| 54 | + return responseBean; | |
| 55 | + } | |
| 56 | + | |
| 57 | + | |
| 58 | + @RequestMapping("messenge") | |
| 59 | + public ResponseBean findAll() { | |
| 60 | + ResponseBean responseBean = new ResponseBean(); | |
| 61 | + responseBean.setData(userMessengeService.findAll()); | |
| 62 | + return responseBean; | |
| 63 | + } | |
| 64 | + | |
| 65 | + @RequestMapping("excel") | |
| 66 | + public void getExcel(HttpServletResponse response) { | |
| 67 | + List<UserMessenge> userMessengeList = userMessengeService.findAll(); | |
| 68 | + String fileName = "正在上演用户信息表"; | |
| 69 | + XSSFWorkbook wb = new XSSFWorkbook(); | |
| 70 | + ServletOutputStream out = null; | |
| 71 | + BufferedInputStream bis = null; | |
| 72 | + BufferedOutputStream bos = null; | |
| 73 | + try { | |
| 74 | + XSSFSheet sheetlist = wb.createSheet(fileName); | |
| 75 | + XSSFRow row = sheetlist.createRow(0);//行 | |
| 76 | + XSSFCell cell = row.createCell(0);//列 | |
| 77 | + cell.setCellValue("姓名"); | |
| 78 | + cell = row.createCell(1);//列 | |
| 79 | + cell.setCellValue("电话"); | |
| 80 | + cell = row.createCell(2);//列 | |
| 81 | + cell.setCellValue("公司"); | |
| 82 | + | |
| 83 | + for (int i = 0; i < userMessengeList.size(); i++) { | |
| 84 | + row = sheetlist.createRow((short) (i + 1));//行 | |
| 85 | + cell = row.createCell(0);//列 | |
| 86 | + cell.setCellValue(userMessengeList.get(i).getName()); | |
| 87 | + cell = row.createCell(1);//列 | |
| 88 | + cell.setCellValue(userMessengeList.get(i).getPhone()); | |
| 89 | + cell = row.createCell(2);//列 | |
| 90 | + cell.setCellValue(userMessengeList.get(i).getCompany()); | |
| 91 | + } | |
| 92 | + | |
| 93 | + ByteArrayOutputStream os = new ByteArrayOutputStream(); | |
| 94 | + wb.write(os); | |
| 95 | + byte[] content = os.toByteArray(); | |
| 96 | + InputStream is = new ByteArrayInputStream(content); | |
| 97 | + response.setContentType("application/vnd.ms-excel;charset=utf-8"); | |
| 98 | + response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xlsx").getBytes(), "iso-8859-1")); | |
| 99 | + out = response.getOutputStream(); | |
| 100 | + bis = new BufferedInputStream(is); | |
| 101 | + bos = new BufferedOutputStream(out); | |
| 102 | + byte[] buff = new byte[2048]; | |
| 103 | + int bytesRead; | |
| 104 | + while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { | |
| 105 | + bos.write(buff, 0, bytesRead); | |
| 106 | + } | |
| 107 | + } catch (final Exception e) { | |
| 108 | + e.printStackTrace(); | |
| 109 | + try { | |
| 110 | + if (bis != null) | |
| 111 | + bis.close(); | |
| 112 | + if (bos != null) | |
| 113 | + bos.close(); | |
| 114 | + } catch (Exception e1) { | |
| 115 | + e1.printStackTrace(); | |
| 116 | + } | |
| 117 | + } finally { | |
| 118 | + try { | |
| 119 | + if (bis != null) | |
| 120 | + bis.close(); | |
| 121 | + if (bos != null) | |
| 122 | + bos.close(); | |
| 123 | + } catch (Exception e2) { | |
| 124 | + e2.printStackTrace(); | |
| 125 | + } | |
| 126 | + } | |
| 127 | + } | |
| 128 | +} | ... | ... |
src/main/java/zzsy/dao/UserMessengeRepository.java
0 → 100644
| 1 | +++ a/src/main/java/zzsy/dao/UserMessengeRepository.java | |
| 1 | +package zzsy.dao; | |
| 2 | + | |
| 3 | +import org.springframework.data.jpa.repository.JpaRepository; | |
| 4 | +import zzsy.entity.UserMessenge; | |
| 5 | + | |
| 6 | +/** | |
| 7 | + * Created by 周伟鹏 on 2018/10/16. | |
| 8 | + */ | |
| 9 | +public interface UserMessengeRepository extends JpaRepository<UserMessenge, Integer> { | |
| 10 | +} | ... | ... |
src/main/java/zzsy/entity/UserMessenge.java
0 → 100644
| 1 | +++ a/src/main/java/zzsy/entity/UserMessenge.java | |
| 1 | +package zzsy.entity; | |
| 2 | +/** | |
| 3 | + * Created by 周伟鹏 on 2018/10/16. | |
| 4 | + */ | |
| 5 | + | |
| 6 | +import javax.persistence.*; | |
| 7 | +import java.io.Serializable; | |
| 8 | +import java.sql.Timestamp; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * @Auther: Administrator | |
| 12 | + * @Date: 2018/10/16 15:18 | |
| 13 | + * @Description: | |
| 14 | + */ | |
| 15 | +@Entity | |
| 16 | +@Table(name = "USER_MESSENGE") | |
| 17 | +public class UserMessenge implements Serializable { | |
| 18 | + | |
| 19 | + private static final long serialVersionUID = -3061930907822467009L; | |
| 20 | + | |
| 21 | + @Id | |
| 22 | + @GeneratedValue(strategy = GenerationType.AUTO) | |
| 23 | + private Integer id; | |
| 24 | + | |
| 25 | + private String name; | |
| 26 | + | |
| 27 | + private String phone; | |
| 28 | + | |
| 29 | + private String company; | |
| 30 | + | |
| 31 | + @Column(name = "INSERT_DATE") | |
| 32 | + private Timestamp insertDate; | |
| 33 | + | |
| 34 | + public Integer getId() { | |
| 35 | + return id; | |
| 36 | + } | |
| 37 | + | |
| 38 | + public void setId(Integer id) { | |
| 39 | + this.id = id; | |
| 40 | + } | |
| 41 | + | |
| 42 | + public String getName() { | |
| 43 | + return name; | |
| 44 | + } | |
| 45 | + | |
| 46 | + public void setName(String name) { | |
| 47 | + this.name = name; | |
| 48 | + } | |
| 49 | + | |
| 50 | + public String getPhone() { | |
| 51 | + return phone; | |
| 52 | + } | |
| 53 | + | |
| 54 | + public void setPhone(String phone) { | |
| 55 | + this.phone = phone; | |
| 56 | + } | |
| 57 | + | |
| 58 | + public String getCompany() { | |
| 59 | + return company; | |
| 60 | + } | |
| 61 | + | |
| 62 | + public void setCompany(String company) { | |
| 63 | + this.company = company; | |
| 64 | + } | |
| 65 | + | |
| 66 | + public Timestamp getInsertDate() { | |
| 67 | + return insertDate; | |
| 68 | + } | |
| 69 | + | |
| 70 | + public void setInsertDate(Timestamp insertDate) { | |
| 71 | + this.insertDate = insertDate; | |
| 72 | + } | |
| 73 | +} | ... | ... |
src/main/java/zzsy/utils/ErrorEnum.java
0 → 100644
| 1 | +++ a/src/main/java/zzsy/utils/ErrorEnum.java | |
| 1 | +package zzsy.utils; | |
| 2 | + | |
| 3 | +public enum ErrorEnum { | |
| 4 | + | |
| 5 | + SUCCESS(0, "success"), | |
| 6 | + ERROR(500, "服务异常"), | |
| 7 | + PHONE_ERROR(1, "手机号无效"), | |
| 8 | + PHONE_REPEAT(2, "手机号重复"); | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + private Integer errorCode; | |
| 13 | + private String errorMessage; | |
| 14 | + | |
| 15 | + ErrorEnum(Integer errorCode, String errorMessage) { | |
| 16 | + this.errorCode = errorCode; | |
| 17 | + this.errorMessage = errorMessage; | |
| 18 | + } | |
| 19 | + | |
| 20 | + public Integer getErrorCode() { | |
| 21 | + return errorCode; | |
| 22 | + } | |
| 23 | + | |
| 24 | + public void setErrorCode(Integer errorCode) { | |
| 25 | + this.errorCode = errorCode; | |
| 26 | + } | |
| 27 | + | |
| 28 | + public String getErrorMessage() { | |
| 29 | + return errorMessage; | |
| 30 | + } | |
| 31 | + | |
| 32 | + public void setErrorMessage(String errorMessage) { | |
| 33 | + this.errorMessage = errorMessage; | |
| 34 | + } | |
| 35 | +} | ... | ... |
src/main/java/zzsy/utils/ResponseBean.java
0 → 100644
| 1 | +++ a/src/main/java/zzsy/utils/ResponseBean.java | |
| 1 | +package zzsy.utils; | |
| 2 | + | |
| 3 | +import java.io.Serializable; | |
| 4 | + | |
| 5 | +/** | |
| 6 | + * 通用API 请求返回实体包装类 | |
| 7 | + * | |
| 8 | + */ | |
| 9 | +public class ResponseBean implements Serializable { | |
| 10 | + | |
| 11 | + | |
| 12 | + private static final long serialVersionUID = 862017689923854902L; | |
| 13 | + | |
| 14 | + private Integer errorCode = ErrorEnum.SUCCESS.getErrorCode(); | |
| 15 | + | |
| 16 | + private String errorMessage = ErrorEnum.SUCCESS.getErrorMessage(); | |
| 17 | + | |
| 18 | + private Object data; | |
| 19 | + | |
| 20 | + private ErrorEnum errorEnum; | |
| 21 | + | |
| 22 | + public ResponseBean(Object data) { | |
| 23 | + this.data = data; | |
| 24 | + } | |
| 25 | + | |
| 26 | + public ResponseBean(Integer errorCode, String errorMessage, Object data) { | |
| 27 | + this.errorCode = errorCode; | |
| 28 | + this.errorMessage = errorMessage; | |
| 29 | + this.data = data; | |
| 30 | + } | |
| 31 | + | |
| 32 | + public ResponseBean(Integer errorCode, String errorMessage) { | |
| 33 | + this.errorCode = errorCode; | |
| 34 | + this.errorMessage = errorMessage; | |
| 35 | + } | |
| 36 | + | |
| 37 | + public ResponseBean() { | |
| 38 | + } | |
| 39 | + | |
| 40 | + public ResponseBean(ErrorEnum errorEnum) { | |
| 41 | + this.errorEnum = errorEnum; | |
| 42 | + } | |
| 43 | + | |
| 44 | + public Integer getErrorCode() { | |
| 45 | + if (this.errorEnum!=null) { | |
| 46 | + return this.errorEnum.getErrorCode(); | |
| 47 | + } | |
| 48 | + return errorCode; | |
| 49 | + } | |
| 50 | + | |
| 51 | + public void setErrorCode(Integer errorCode) { | |
| 52 | + this.errorCode = errorCode; | |
| 53 | + } | |
| 54 | + | |
| 55 | + public String getErrorMessage() { | |
| 56 | + if (this.errorEnum!=null) { | |
| 57 | + return this.errorEnum.getErrorMessage(); | |
| 58 | + } | |
| 59 | + return errorMessage; | |
| 60 | + } | |
| 61 | + | |
| 62 | + public void setErrorMessage(String errorMessage) { | |
| 63 | + this.errorMessage = errorMessage; | |
| 64 | + } | |
| 65 | + | |
| 66 | + public Object getData() { | |
| 67 | + return data; | |
| 68 | + } | |
| 69 | + | |
| 70 | + public void setData(Object data) { | |
| 71 | + this.data = data; | |
| 72 | + } | |
| 73 | + | |
| 74 | + | |
| 75 | +} | ... | ... |
src/main/resources/application.properties
0 → 100644
| 1 | +++ a/src/main/resources/application.properties | |
| 1 | +spring.datasource.url=jdbc:mysql://localhost:3307/zzsy?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false | |
| 2 | +spring.datasource.username=root | |
| 3 | +spring.datasource.password=root | |
| 4 | +spring.datasource.driver-class-name=com.mysql.jdbc.Driver | |
| 5 | + | |
| 6 | +spring.jpa.properties.hibernate.hbm2ddl.auto=update | |
| 7 | +spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect | |
| 8 | +spring.jpa.show-sql= true | |
| 0 | 9 | \ No newline at end of file | ... | ... |
src/test/java/zzsy/cnlive_zzsy/CnliveZzsyApplicationTests.java
0 → 100644
| 1 | +++ a/src/test/java/zzsy/cnlive_zzsy/CnliveZzsyApplicationTests.java | |
| 1 | +package zzsy.cnlive_zzsy; | |
| 2 | + | |
| 3 | +import org.junit.Test; | |
| 4 | +import org.junit.runner.RunWith; | |
| 5 | +import org.springframework.boot.test.context.SpringBootTest; | |
| 6 | +import org.springframework.test.context.junit4.SpringRunner; | |
| 7 | + | |
| 8 | +@RunWith(SpringRunner.class) | |
| 9 | +@SpringBootTest | |
| 10 | +public class CnliveZzsyApplicationTests { | |
| 11 | + | |
| 12 | + @Test | |
| 13 | + public void contextLoads() { | |
| 14 | + } | |
| 15 | + | |
| 16 | +} | ... | ... |