commit bc2911b5ab77bac3d320195a40b0fc1455dc33e6 Author: lenghui Date: Fri Dec 20 16:00:37 2024 +0800 提交 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/crowdfunding_system.sql b/crowdfunding_system.sql new file mode 100644 index 0000000..47a222b --- /dev/null +++ b/crowdfunding_system.sql @@ -0,0 +1,74 @@ +/* + Navicat Premium Data Transfer + + Source Server : test + Source Server Type : MySQL + Source Server Version : 80026 (8.0.26) + Source Host : localhost:3306 + Source Schema : crowdfunding_system + + Target Server Type : MySQL + Target Server Version : 80026 (8.0.26) + File Encoding : 65001 + + Date: 20/12/2024 15:52:53 +*/ + +SET NAMES utf8mb4; +SET FOREIGN_KEY_CHECKS = 0; + +-- ---------------------------- +-- Table structure for crowdfundinginfo +-- ---------------------------- +DROP TABLE IF EXISTS `crowdfundinginfo`; +CREATE TABLE `crowdfundinginfo` ( + `id` int NOT NULL AUTO_INCREMENT, + `title` varchar(25) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, + `target_number` int NOT NULL, + `now_number` int NOT NULL, + `status` int NOT NULL, + PRIMARY KEY (`id`) USING BTREE +) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; + +-- ---------------------------- +-- Records of crowdfundinginfo +-- ---------------------------- +INSERT INTO `crowdfundinginfo` VALUES (1, '请给我钱', 100, 5, 1); + +-- ---------------------------- +-- Table structure for participants +-- ---------------------------- +DROP TABLE IF EXISTS `participants`; +CREATE TABLE `participants` ( + `id` int NOT NULL AUTO_INCREMENT, + `jwcode` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, + `identity` int NOT NULL, + `creat_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`) USING BTREE +) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; + +-- ---------------------------- +-- Records of participants +-- ---------------------------- +INSERT INTO `participants` VALUES (6, '10052', 0, '2024-12-20 14:59:29'); + +-- ---------------------------- +-- Table structure for participants_del_save +-- ---------------------------- +DROP TABLE IF EXISTS `participants_del_save`; +CREATE TABLE `participants_del_save` ( + `id` int NOT NULL AUTO_INCREMENT, + `jwcode` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, + `identity` int NOT NULL, + `creat_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`) USING BTREE +) ENGINE = InnoDB AUTO_INCREMENT = 8 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; + +-- ---------------------------- +-- Records of participants_del_save +-- ---------------------------- +INSERT INTO `participants_del_save` VALUES (5, '10012', 1, '2024-12-20 14:59:42'); +INSERT INTO `participants_del_save` VALUES (6, '10010', 1, '2024-12-20 15:00:48'); +INSERT INTO `participants_del_save` VALUES (7, '10011', 1, '2024-12-20 15:24:18'); + +SET FOREIGN_KEY_CHECKS = 1; diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..fc1915a --- /dev/null +++ b/pom.xml @@ -0,0 +1,93 @@ + + + 4.0.0 + com.lh + crowdfunding_system + 0.0.1-SNAPSHOT + crowdfunding_system + Demo project for Spring Boot + + 1.8 + UTF-8 + UTF-8 + 2.4.2 + + + + org.springframework.boot + spring-boot-starter-web + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.1.4 + + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + mysql + mysql-connector-java + runtime + + + + + + 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.lh.CrowdfundingSystemApplication + true + + + + repackage + + repackage + + + + + + + + diff --git a/src/main/java/com/lh/CrowdfundingSystemApplication.java b/src/main/java/com/lh/CrowdfundingSystemApplication.java new file mode 100644 index 0000000..aaa581f --- /dev/null +++ b/src/main/java/com/lh/CrowdfundingSystemApplication.java @@ -0,0 +1,14 @@ +package com.lh; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +@MapperScan(basePackages = "com.lh.mapper") +@SpringBootApplication +public class CrowdfundingSystemApplication { + + public static void main(String[] args) { + SpringApplication.run(CrowdfundingSystemApplication.class, args); + } + +} diff --git a/src/main/java/com/lh/bean/CrowdfundingInfo.java b/src/main/java/com/lh/bean/CrowdfundingInfo.java new file mode 100644 index 0000000..2d39d53 --- /dev/null +++ b/src/main/java/com/lh/bean/CrowdfundingInfo.java @@ -0,0 +1,27 @@ +package com.lh.bean; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.util.List; + +@NoArgsConstructor +@AllArgsConstructor +@Data +public class CrowdfundingInfo implements Serializable { + private Integer id; + + private String title; + + private Integer targetNumber; + + private Integer nowNumber; + + private Integer status; + + private List participantList; + + +} \ No newline at end of file diff --git a/src/main/java/com/lh/bean/Participant.java b/src/main/java/com/lh/bean/Participant.java new file mode 100644 index 0000000..79a9e41 --- /dev/null +++ b/src/main/java/com/lh/bean/Participant.java @@ -0,0 +1,23 @@ +package com.lh.bean; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.util.Date; + +@AllArgsConstructor +@NoArgsConstructor +@Data +public class Participant implements Serializable { + + private Integer id; + + private String jwcode; + + private Integer identity; + + private Date creatTime; + +} \ No newline at end of file diff --git a/src/main/java/com/lh/bean/RespBean.java b/src/main/java/com/lh/bean/RespBean.java new file mode 100644 index 0000000..774e401 --- /dev/null +++ b/src/main/java/com/lh/bean/RespBean.java @@ -0,0 +1,82 @@ +package com.lh.bean; + +public class RespBean { + //状态码 10000-成功 10001-失败 + private Integer code; + //返回的附件信息 + private String msg; + //返回的数据 + private Object data; + + public Integer getCode() { + return code; + } + + public void setCode(Integer code) { + this.code = code; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public static RespBean ok() { + RespBean respBean = new RespBean(); + respBean.setCode(10000); + + return respBean; + } + + public static RespBean ok(String msg) { + RespBean respBean = new RespBean(); + respBean.setCode(10000); + respBean.setMsg(msg); + + return respBean; + } + + public static RespBean ok(String msg, Object data) { + RespBean respBean = new RespBean(); + respBean.setCode(10000); + respBean.setMsg(msg); + respBean.setData(data); + + return respBean; + } + + public static RespBean error() { + RespBean respBean = new RespBean(); + respBean.setCode(10001); + + return respBean; + } + + public static RespBean error(String msg) { + RespBean respBean = new RespBean(); + respBean.setCode(10001); + respBean.setMsg(msg); + + return respBean; + } + + public static RespBean error(String msg, Object data) { + RespBean respBean = new RespBean(); + respBean.setCode(10001); + respBean.setMsg(msg); + respBean.setData(data); + + return respBean; + } +} \ No newline at end of file diff --git a/src/main/java/com/lh/config/ParamExceptionHandler.java b/src/main/java/com/lh/config/ParamExceptionHandler.java new file mode 100644 index 0000000..4405ba4 --- /dev/null +++ b/src/main/java/com/lh/config/ParamExceptionHandler.java @@ -0,0 +1,25 @@ +package com.lh.config; + +import com.lh.bean.RespBean; +import com.lh.exception.MyException; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@Configuration +@RestControllerAdvice +public class ParamExceptionHandler { + @ExceptionHandler(MyException.class) + public RespBean myException(MyException e){ + e.printStackTrace(); + return RespBean.error(e.getMessage()); + } + + @ExceptionHandler(Exception.class) + public RespBean exception(Exception e){ + e.printStackTrace(); + return RespBean.error("未知错误,请联系管理员"); + } + + +} \ No newline at end of file diff --git a/src/main/java/com/lh/controller/CrowdfundingController.java b/src/main/java/com/lh/controller/CrowdfundingController.java new file mode 100644 index 0000000..2941058 --- /dev/null +++ b/src/main/java/com/lh/controller/CrowdfundingController.java @@ -0,0 +1,62 @@ +package com.lh.controller; + +import com.lh.bean.CrowdfundingInfo; +import com.lh.bean.Participant; +import com.lh.bean.RespBean; +import com.lh.exception.MyException; +import com.lh.service.CrowdfundingService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@CrossOrigin +@RestController +@RequestMapping +public class CrowdfundingController { + + @Autowired + private CrowdfundingService crowdfundingService; + + // 加载页面 + @GetMapping("/page") + public RespBean loadPage() { + CrowdfundingInfo crowdfundingInfo = crowdfundingService.loadPage(); + return RespBean.ok("请求成功",crowdfundingInfo); + } + + // 修改标题 + @PutMapping("/title") + public RespBean updateTitle(@RequestParam("title") String title) throws MyException { + crowdfundingService.updateTitle(title); + return RespBean.ok("修改标题成功"); + } + + // 修改众筹目标 + @PutMapping("/target") + public RespBean updateTarget(@RequestParam("targetNumber") Integer targetNumber) throws MyException { + crowdfundingService.updateTarget(targetNumber); + return RespBean.ok("修改目标成功"); + } + + // 添加众筹用户 + @PostMapping("/addUser") + public RespBean addCrowdUser(@RequestBody Participant participant) throws MyException { + crowdfundingService.addCrowdUser(participant); + return RespBean.ok("添加成功"); + } + + // 删除众筹用户 + @DeleteMapping("/delUser/{jwcode}") + public RespBean deleteCrowdUser(@PathVariable String jwcode) throws MyException { + crowdfundingService.deleteCrowdUser(jwcode); + return RespBean.ok("删除成功"); + } + + // 查询众筹用户 + @PostMapping("/users") + public RespBean queryCrowdUser(@RequestBody(required = false) Participant participant) { + List participants = crowdfundingService.queryCrowdUser(participant); + return RespBean.ok("查询成功",participants); + } +} \ No newline at end of file diff --git a/src/main/java/com/lh/exception/MyException.java b/src/main/java/com/lh/exception/MyException.java new file mode 100644 index 0000000..948aa5d --- /dev/null +++ b/src/main/java/com/lh/exception/MyException.java @@ -0,0 +1,7 @@ +package com.lh.exception; + +public class MyException extends Exception{ + public MyException(String msg){ + super(msg); + } +} diff --git a/src/main/java/com/lh/mapper/CrowdfundingMapper.java b/src/main/java/com/lh/mapper/CrowdfundingMapper.java new file mode 100644 index 0000000..202634f --- /dev/null +++ b/src/main/java/com/lh/mapper/CrowdfundingMapper.java @@ -0,0 +1,35 @@ +package com.lh.mapper; + +import com.lh.bean.CrowdfundingInfo; +import com.lh.bean.Participant; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; +@Mapper +public interface CrowdfundingMapper { + //加载页面 + CrowdfundingInfo loadPage(); + + //修改标题 + void updateTitle(String title); + + //修改众筹目标 + void updateTarget(Integer target); + + //当前众筹数量+1 + void updateNowNumber(); + + //添加众筹用户 + void addCrowdUser(Participant participant); + + //删除众筹用户 + void deleteCrowdUser(String jwcode); + + //删除用户添加到另一个表 + void saveDeleteCrowdUser(Participant participant); + + //查询众筹用户 + List queryCrowdUser(Participant participant); + + +} diff --git a/src/main/java/com/lh/service/CrowdfundingService.java b/src/main/java/com/lh/service/CrowdfundingService.java new file mode 100644 index 0000000..914db81 --- /dev/null +++ b/src/main/java/com/lh/service/CrowdfundingService.java @@ -0,0 +1,27 @@ +package com.lh.service; + +import com.lh.bean.CrowdfundingInfo; +import com.lh.bean.Participant; +import com.lh.exception.MyException; + +import java.util.List; + +public interface CrowdfundingService { + //加载页面 + CrowdfundingInfo loadPage(); + + //修改标题 + void updateTitle(String title) throws MyException; + + //修改众筹目标 + void updateTarget(Integer target) throws MyException; + + //添加众筹用户 + void addCrowdUser(Participant participant) throws MyException; + + //删除众筹用户 + void deleteCrowdUser(String jwcode) throws MyException; + + //查询众筹用户 + List queryCrowdUser(Participant participant); +} diff --git a/src/main/java/com/lh/service/CrowdfundingServiceImpl.java b/src/main/java/com/lh/service/CrowdfundingServiceImpl.java new file mode 100644 index 0000000..b4638dd --- /dev/null +++ b/src/main/java/com/lh/service/CrowdfundingServiceImpl.java @@ -0,0 +1,82 @@ +package com.lh.service; + +import com.lh.bean.CrowdfundingInfo; +import com.lh.bean.Participant; +import com.lh.exception.MyException; +import com.lh.mapper.CrowdfundingMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +@Service +public class CrowdfundingServiceImpl implements CrowdfundingService { + @Autowired + private CrowdfundingMapper crowdfundingMapper; + @Override + public CrowdfundingInfo loadPage() { + CrowdfundingInfo crowdfundingInfoList = crowdfundingMapper.loadPage(); + List participants = crowdfundingMapper.queryCrowdUser(null); + crowdfundingInfoList.setParticipantList(participants); + return crowdfundingInfoList; + } + + @Override + public void updateTitle(String title) throws MyException { + if (title.length() == 0) { + throw new MyException("标题不能为空"); + } + crowdfundingMapper.updateTitle(title); + } + + @Override + public void updateTarget(Integer target) throws MyException { + //target不能为空 + if (target == null) { + throw new MyException("目标不能为空"); + } + //target只能输入数字且必须大于0 + if (target <= 0) { + throw new MyException("目标必须大于0"); + } + if (target.toString().matches("[0-9]+") == false) { + throw new MyException("目标只能输入数字"); + } + crowdfundingMapper.updateTarget(target); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void addCrowdUser(Participant participant) throws MyException { + //精网号重复 + if (crowdfundingMapper.queryCrowdUser(new Participant(null,participant.getJwcode(),null,null)) != null) { + throw new MyException("精网号重复"); + } + //精网号不能为空 + if (participant.getJwcode() == null) { + throw new MyException("精网号不能为空"); + } + //精网号不能为空 + if (participant.getIdentity() == null) { + throw new MyException("身份不能为空"); + } + crowdfundingMapper.updateNowNumber(); + crowdfundingMapper.addCrowdUser(participant); + } + + @Override + public void deleteCrowdUser(String jwcode) throws MyException { + if (crowdfundingMapper.queryCrowdUser(new Participant(null,jwcode,null,null)) == null) { + throw new MyException("删除失败,用户不存在"); + } + List participants = crowdfundingMapper.queryCrowdUser(new Participant(null, jwcode, null, null)); + Participant participant = participants.get(0); + crowdfundingMapper.saveDeleteCrowdUser(participant); + crowdfundingMapper.deleteCrowdUser(jwcode); + } + + @Override + public List queryCrowdUser(Participant participant) { + return crowdfundingMapper.queryCrowdUser(participant); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..f296f8c --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,29 @@ +# 配置连接池 +spring.datasource.driver-class-name=com.mysql.jdbc.Driver +spring.datasource.url=jdbc:mysql://localhost:3306/crowdfunding_system?serverTimezone=UTC +spring.datasource.username=root +spring.datasource.password=123456 +# HikariCP连接池配置 +spring.datasource.hikari.maximum-pool-size=10 +spring.datasource.hikari.minimum-idle=5 +spring.datasource.hikari.idle-timeout=30000 +spring.datasource.hikari.max-lifetime=1800000 +spring.datasource.hikari.connection-timeout=30000 +spring.datasource.hikari.pool-name=HwgoldHikariCP + +# mybatis配置 +# 打印log信息 +mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl +# 开启驼峰命名法,自动将数据库的命名方式,映射成Java中的命名方式 +mybatis.configuration.map-underscore-to-camel-case: true +#下面这些内容是为了让MyBatis映射 +#指定Mybatis的Mapper文件 +mybatis.mapper-locations=classpath:mappers/*xml +#指定Mybatis的实体目录 +mybatis.type-aliases-package=com.lh.mybatis.entity + +# 应用服务 WEB 访问端口 +server.port=8092 + + + diff --git a/src/main/resources/com/lh/mapper/CrowdfundingMapper.xml b/src/main/resources/com/lh/mapper/CrowdfundingMapper.xml new file mode 100644 index 0000000..7fbcf8c --- /dev/null +++ b/src/main/resources/com/lh/mapper/CrowdfundingMapper.xml @@ -0,0 +1,36 @@ + + + + + insert into participants (jwcode, identity) values (#{jwcode}, #{identity}) + + + insert into participants_del_save (jwcode, identity) values (#{jwcode}, #{identity}) + + + update crowdfundinginfo set title=#{title} + + + update crowdfundinginfo set target_number=#{target} + + + update crowdfundinginfo set now_number=now_number+1 + + + delete from participants where jwcode=#{jwcode} + + + + \ No newline at end of file diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html new file mode 100644 index 0000000..e2d94a2 --- /dev/null +++ b/src/main/resources/static/index.html @@ -0,0 +1,6 @@ + + +

hello word!!!

+

this is a html page

+ + \ No newline at end of file diff --git a/src/test/java/com/lh/CrowdfundingSystemApplicationTests.java b/src/test/java/com/lh/CrowdfundingSystemApplicationTests.java new file mode 100644 index 0000000..f7f89ac --- /dev/null +++ b/src/test/java/com/lh/CrowdfundingSystemApplicationTests.java @@ -0,0 +1,29 @@ +package com.lh; + +import com.lh.bean.CrowdfundingInfo; +import com.lh.mapper.CrowdfundingMapper; +import com.lh.service.CrowdfundingService; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class CrowdfundingSystemApplicationTests { + @Autowired + private CrowdfundingMapper crowdfundingMapper; + + @Autowired + private CrowdfundingService crowdfundingService; + + @Test + void contextLoads() { + CrowdfundingInfo crowdfundingInfo = crowdfundingMapper.loadPage(); + } + + @Test + void contextLoads1() { + System.out.println(crowdfundingService.loadPage()); + } + + +}