添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- druid数据库连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!--lombok依赖--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> 编辑application.yml文件 spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&charactEnoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai username: root password: root initiaSize: 20 minIdle: 50 maxActive: 500 jpa: hibernate: ddl-auto: update show-sql: true
创建文件夹
创建文件
编辑文件
编辑User package com.bigdate.demo.entity; import lombok.Data; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import java.io.Serializable; import java.math.BigDecimal; import java.sql.Timestamp; @Table(name = "company_monitor_sensor") @Entity @Data public class User implements Serializable { @Id private Integer id; //主键 private String company_code; //企业编号 private Integer monitor_type; //监测类型,1-存储监测,0-作业场所监测 private String sys_name; //系统名称 private Integer equipment_type_id; //设备类型ID private String equipment_type_name; //设备类型名称 private Integer sensor_type_id; //传感器类型ID private String sensor_type_name; //传感器类型名称 private Integer technology_type_id; //危险工艺类型ID private String technology_type_name; //危险工艺类型名称 private String department; //车间/部门 private String sensor_code; //传感器编号 private String location; //传感器位置 private String storage_material; //存储物质 private BigDecimal level_alarm_high; //一级报警(高值) private BigDecimal secondary_alarm_high; //二级报警(高值) private BigDecimal level_alarm_low; //一级报警(低值) private BigDecimal secondary_alarm_low; //二级报警(低值) private Integer operate_id; //操作人ID private String operate_username; //操作人用户名 private Timestamp create_time; //创建时间 private Timestamp update_time; //更新时间 } 编辑UserDao package com.bigdate.demo.dao; import com.bigdate.demo.entity.User; import org.springframework.data.jpa.repository.JpaRepository; public interface UserDao extends JpaRepository<User,Integer > { User findById(int id); } 编辑UserService package com.bigdate.demo.service; import com.bigdate.demo.entity.User; import java.util.List; public interface UserService { /** * 保存用户 * @param user * @return */ User save(User user); /** * 根据主键删除项目 * @param id */ void deleteUserById(int id); /** * 根据主键查询 * @param id * @return */ User queryById(int id); } 编辑UserServiceImpl package com.bigdate.demo.service.Impl; import com.bigdate.demo.dao.UserDao; import com.bigdate.demo.entity.User; import com.bigdate.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; /** * 保存 * @param user * @return */ @Override public User save(User user) { return userDao.save(user); } /** * 根据ID删除 * @param id */ @Override public void deleteUserById(int id) { userDao.deleteById(id); } /** * 根据id查找 * @param id * @return */ @Override public User queryById(int id) { return userDao.getOne(id); } } 编辑UserController package com.bigdate.demo.cntroller; import com.bigdate.demo.entity.User; import com.bigdate.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("user") public class UserController { @Autowired private UserService userService; @GetMapping("/queryById") public User queryById(int id){ return userService.queryById(id); } @PostMapping("/save_or_update") public User save_or_update(User user){ return userService.save(user); } @DeleteMapping("/Delete") public Boolean Delete(int id) { userService.deleteUserById(id); return true; } } 运行 测试