????????TKMybatis 是基于 Mybatis 框架开发的一个工具,内部实现了对单表的基本数据操作,只需要简单继承 TKMybatis 提供的接口,就能够实现无需编写任何 sql 即能完成单表操作。
依赖引入
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
BaseOptMapper.java
? ? ? ? 通用的操作类
import tk.mybatis.mapper.annotation.RegisterMapper;
import tk.mybatis.mapper.common.Marker;
import tk.mybatis.mapper.common.base.delete.DeleteMapper;
import tk.mybatis.mapper.common.base.insert.InsertMapper;
import tk.mybatis.mapper.common.base.insert.InsertSelectiveMapper;
import tk.mybatis.mapper.common.base.select.SelectCountMapper;
import tk.mybatis.mapper.common.base.select.SelectMapper;
import tk.mybatis.mapper.common.base.select.SelectOneMapper;
import tk.mybatis.mapper.common.base.update.UpdateByPrimaryKeyMapper;
import tk.mybatis.mapper.common.base.update.UpdateByPrimaryKeySelectiveMapper;
@RegisterMapper
public interface BaseOptMapper<T>
extends SelectOneMapper<T>, SelectMapper<T>, SelectCountMapper<T>, InsertMapper<T>, InsertSelectiveMapper<T>,
UpdateByPrimaryKeyMapper<T>, UpdateByPrimaryKeySelectiveMapper<T>, DeleteMapper<T>, Marker {
}
PersonDTO.java
? ? ? ? Person实体类,对应于person表
@Table(name = "person")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PersonDTO {
private static final long serialVersionUID = 2242061288359096793L;
@Id
private UUID id;
@ApiModelProperty(value = "姓名")
@NotBlank(message = "姓名不能为空!")
private String name;
@ApiModelProperty(value = "地址")
@NotBlank(message = "地址不能为空!")
private String address;
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setName(String address) {
this.address = address;
}
}
PersonMapper.java
@Mapper
public interface PersonMapper extends BaseOptMapper<PersonDTO> {
}
PersonController.java
@RestController
@RequestMapping("/person")
public class PersonController {
@Autowired
private PersonService personService;
@PostMapping("/add")
@ResponseBody
public Result add(@Validated PersonDTO personDTO, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return Result.error(bindingResult.getFieldError().getDefaultMessage());
}
int rows = personService.add(personDTO);
return rows > 0 ? Result.ok() : Result.error();
}
}
PersonService.java
public interface PersonService {
int add(List<CarbonEmissionRecordDetailDTO> updates);
}
PersonServiceImpl.java
????????? 这里就写了插入,其他的类推。
@Service
@Transactional
public class PersonServiceImpl implements PersonService {
@Autowired
private PersonMapper personMapper;
@Override
public int add(PersonDTO param) {
param.setId(UUID.randomUUID().toString());
return personMapper.insertSelective(param);
}
}
|