一.数据库设计
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `tb_history`;
CREATE TABLE `tb_history` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(20) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
`description` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
`create_time` datetime NULL DEFAULT NULL,
`update_time` datetime NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 21 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
二.代码实现
1.实体类
@Data
public class Account implements Serializable {
private Integer id;
private String name;
private String description;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}
2.DTO
用于接收前端传入的数据
@Data
public class AccountDto implements Serializable {
private Integer id;
private String name;
private String description;
}
3.启动类
@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
public class AccountController {
@Autowired
private AccountSerivce accountSerivce;
@PostMapping("/add")
@ApiOperation("新增")
public String insertToA(@RequestBody AccountDto accountdto){
return accountSerivce.insert(accountdto);
}
@GetMapping("/get")
@ApiOperation("查询")
public List<Account> selectAllList(){
return accountSerivce.selectAll();
}
@PutMapping("/update")
@ApiOperation("修改")
public Account update(@RequestBody AccountDto accountdto){
return accountSerivce.update(accountdto);
}
@DeleteMapping("/{id}")
@ApiOperation("删除")
public String delete(@PathVariable Integer id){
return accountSerivce.delete(id);
}
依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.9</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.22</version>
</dependency>
3.Seervic层
/**
* 添加
* @param accountdto
* @return
*/
String insert(AccountDto accountdto);
/**
* 查询所有
* @return
*/
List<Account> selectAll();
/**
* 更新
* @param accountdto
* @return
*/
Account update(AccountDto accountdto);
/**
* 删除
* @param id
* @return
*/
String delete(Integer id);
4.ServiceImpl层
@Service
public class AccountServiceImpl implements AccountSerivce {
@Autowired
private AccountCatMapper accountCatMapper;
/**
* 添加
* @param accountdto
* @return
*/
public String insert(AccountDto accountdto) {
Account account = new Account();
BeanUtils.copyProperties(accountdto, account);
account.setCreateTime(LocalDateTime.now());
account.setUpdateTime(LocalDateTime.now());
accountCatMapper.insert(account);
return "添加成功";
}
/**
* 查询所有
* @return
*/
public List<Account> selectAll() {
return accountCatMapper.selectAll();
}
/**
* 修改
* @param accountdto
* @return
*/
public Account update(AccountDto accountdto) {
Account account = new Account();
BeanUtils.copyProperties(accountdto, account);
account.setUpdateTime(LocalDateTime.now());
accountCatMapper.update(account);
return accountCatMapper.selectById(accountdto.getId());
}
/**
* 删除
* @param id
* @return
*/
public String delete(Integer id) {
accountCatMapper.deleteById(id);
return "删除成功";
}
5.mapper层
@Mapper
public interface AccountCatMapper {
/**
* 新增
* @param account
*/
@Insert("insert into tb_history (id,name,description,create_time,update_time) values (#{id},#{name},#{description},#{createTime},#{updateTime})")
void insert(Account account);
/**
* 查询
* @return
*/
@Select("select * from tb_history")
List<Account> selectAll();
/**
* 根据id查询
* @param id
* @return
*/
@Select("select * from tb_history where id =#{id}")
Account selectById(Integer id);
/**
* 修改
* @param account
*/
@Update("update tb_history set name = #{name},description=#{description},update_time=#{updateTime} where id = #{id}")
void update(Account account);
/**
* 删除
* @param id
*/
@Delete("delete from tb_history where id=#{id}")
void deleteById(Integer id);
三.测试
1.新增测试
2.查询测试
3.修改测试
4.删除测试