create table cic_bracket ( id bigint auto_increment primary key, bracket_no varchar(6) not null comment '胶托编号', owning_airport char(3) not null comment '所属站点', current_airport varchar(3) null comment '当前站点', bluetooth_id bigint null comment '蓝牙id', bluetooth_mac varchar(12) null comment '蓝牙MAC地址', create_time timestamp default CURRENT_TIMESTAMP not null comment '创建时间', update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间' ) comment '胶托信息';。基于该表,使用spring,springboot,MybatisPlus框架生成增删改查功能代码
时间: 2024-01-18 20:03:02 浏览: 81
Python库 | cic_cli-0.2.0-py3-none-any.whl
5星 · 资源好评率100%
好的,以下是生成增删改查功能代码的步骤:
1. 首先,在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
```
2. 在application.properties文件中添加以下配置:
```properties
# 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# MybatisPlus配置
mybatis-plus.mapper-locations=classpath:mapper/**/*.xml
mybatis-plus.type-aliases-package=com.example.demo.entity
# Druid监控配置
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=admin
spring.datasource.druid.stat-view-servlet.allow=127.0.0.1
spring.datasource.druid.stat-view-servlet.deny=
```
3. 创建实体类CicBracket.java:
```java
@Data
@TableName("cic_bracket")
public class CicBracket {
@TableId(type = IdType.AUTO)
private Long id;
@NotBlank(message = "胶托编号不能为空")
private String bracketNo;
@NotBlank(message = "所属站点不能为空")
@Length(min = 3, max = 3, message = "所属站点必须为3位字符")
private String owningAirport;
private String currentAirport;
private Long bluetoothId;
private String bluetoothMac;
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
}
```
4. 创建mapper接口CicBracketMapper.java:
```java
@Mapper
public interface CicBracketMapper extends BaseMapper<CicBracket> {
}
```
5. 创建service接口ICicBracketService.java:
```java
public interface ICicBracketService extends IService<CicBracket> {
}
```
6. 创建service实现类CicBracketServiceImpl.java:
```java
@Service
public class CicBracketServiceImpl extends ServiceImpl<CicBracketMapper, CicBracket> implements ICicBracketService {
}
```
7. 创建controller类CicBracketController.java:
```java
@RestController
@RequestMapping("/cic-bracket")
public class CicBracketController {
@Autowired
private ICicBracketService cicBracketService;
@PostMapping("/add")
public Result add(@Validated @RequestBody CicBracket cicBracket) {
cicBracketService.save(cicBracket);
return Result.ok();
}
@PostMapping("/update")
public Result update(@Validated @RequestBody CicBracket cicBracket) {
cicBracketService.updateById(cicBracket);
return Result.ok();
}
@GetMapping("/delete/{id}")
public Result delete(@PathVariable Long id) {
cicBracketService.removeById(id);
return Result.ok();
}
@GetMapping("/get/{id}")
public Result get(@PathVariable Long id) {
CicBracket cicBracket = cicBracketService.getById(id);
return Result.ok().put("data", cicBracket);
}
@GetMapping("/list")
public Result list() {
List<CicBracket> list = cicBracketService.list();
return Result.ok().put("data", list);
}
}
```
8. 最后,启动Spring Boot项目,访问http://localhost:8080/swagger-ui.html,即可使用Swagger测试接口。
以上就是基于该表,使用Spring Boot、MybatisPlus框架生成增删改查功能代码的步骤,如果有疑问可以随时问我。
阅读全文