后台写的总体分为两个部分
第一部分:纯后台的代码实现CRUD(增删改查)
第二部分:前后端交互实现CRUD(增删改查)
先贴下公共的一些代码:
pom
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <!-- maven 添加json--> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> </dependency> <!--jquery-webjar--> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.3.0</version> </dependency> <!--bootstrap-webjar--> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>4.0.0</version> </dependency> <!--配置文件注入时使用后会有提示--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.2.6.RELEASE</version> </plugin> </plugins> </build> </project>
application.yml 的数据库连接代码:
spring: datasource: name: url: jdbc:mysql://127.0.0.1:3306/login?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=UTC username: root password: root messages: ##message.propertiesi是默认的国际化配置文件,不需要特别指定路径 basename: i18n.login ##禁用thymeleaf的缓存 thymeleaf: cache: false mybatis-plus: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.example.demo.entity server: port: 1234 servlet: context-path: /
application.properties html的一些配置的代码
# thymeleaf spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.check-template-location=true spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.servlet.content-type=text/html spring.thymeleaf.mode=HTML5 spring.thymeleaf.cache=false
sql的代码:
/* Navicat Premium Data Transfer Source Server : bendi Source Server Type : MySQL Source Server Version : 50732 Source Host : localhost:3306 Source Schema : login Target Server Type : MySQL Target Server Version : 50732 File Encoding : 65001 Date: 24/05/2021 16:32:11 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `ID` int(11) NOT NULL COMMENT 'ID', `NAME` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '名字', `LOGIN` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '账号', `PASSWORD` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '密码', PRIMARY KEY (`ID`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES (2, '不知道', 'yan', '78578578'); INSERT INTO `user` VALUES (11, '11111', '555555', '44'); INSERT INTO `user` VALUES (22, '8861111', '33', '335'); INSERT INTO `user` VALUES (88, '88888888888', '33781', '335787'); INSERT INTO `user` VALUES (2054, '4524', '44444', '4242'); INSERT INTO `user` VALUES (8877, '888', '887', '878787'); SET FOREIGN_KEY_CHECKS = 1;
下面介绍第一部分无页面代码:
Controller
package com.example.demo.controller; import com.example.demo.entity.UserEntity; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; import java.util.List; @RequestMapping("user") @RestController public class UserController { //todo ====================================后台调用---》 每个控制台上面有 自己复制即可 @Autowired private UserService userService; /** * 查询所有的数据 * @return * * http://localhost:1234/user/list */ @RequestMapping("list") public List<UserEntity> findall(){ return userService.findall(); } /** 当使用@RequestMapping URI id 样式映射时, * 即 someUrl/{paramId}, id @Pathvariable注解绑定它传过来的值到方法的参数上。 * * 根据ID查询 * @param id * @return * * http://localhost:1234/user/select/2 */ @RequestMapping("/select/{id}") public String selectId(@PathVariable int id){ return userService.getselectId(id).toString(); } /** * * 根据ID删除 * @param id * @return * * http://localhost:1234/user/delect/2 */ @RequestMapping("/delect/{id}") public String delectId(@PathVariable int id){ int result=userService.delectId(id); if (result>=1){ return "删除成功"; }else{ return "删除失败"; } } /** * http://localhost:1234/user/index * 展示页面 * @return */ @RequestMapping("/index") public ModelAndView index(){ return new ModelAndView("index"); } /** * http://localhost:1234/user/ahtml * 新增页面 * @return */ @RequestMapping("/ahtml") public ModelAndView adds(){ return new ModelAndView("add"); } /** * 新增数组 * @param userEntity * @return *http://localhost:1234/user/adds * * http://localhost:1234/user/ahtml 用这个 * 返回值是IdUserEntity(id=11, name=22, login=33, password=44) */ @RequestMapping("/adds") public String add(UserEntity userEntity){ String str = "返回值是"+"Id"+userEntity; System.out.println(str); int result=userService.addall(userEntity); if (result>=1){ return "添加成功"; }else{ return "添加失败"; } } /** * 修改 * @param userEntity * @return * * http://localhost:1234/user/updatess */ @RequestMapping("/updatess") public String Upate(UserEntity userEntity){ //这里是需要修改的数据 如果id么有就算新增 userEntity.setId("2"); userEntity.setName("不知道"); userEntity.setLogin("yan"); userEntity.setPassword("78578578"); int results=userService.update(userEntity); if(results==1){ return "更新成功"; } return "更新失败"; } }
实体类 entity
package com.example.demo.entity; import lombok.Data; @Data //这里引用了data 所以不需要 get/set的方法了 public class UserEntity { private String id;//id private String name;//姓名 private String login; //账号 private String password;// 密码 }
Service
package com.example.demo.service; import com.example.demo.entity.UserEntity; import java.util.List; public interface UserService { /** * 查询所有的数据 * @return */ List<UserEntity> findall(); /** * 根据iD查询数据 * @param id * @return */ UserEntity getselectId(int id); /** * 根据id删除数据 * @param id * @return */ int delectId(int id); /** * 新增数据 * @param userEntity * @return */ int addall(UserEntity userEntity); /** * 修改数据 * @param userEntity * @return */ int update(UserEntity userEntity); }