编写 mybatis-plus3.5.1.1版本 controller、service、impl、mapper、entity、dto、vo模板
时间: 2023-09-26 10:07:56 浏览: 211
mybatis-plus-boot-starter-3.5.3.1.jar
以下是 mybatis-plus 3.5.1.1 版本的模板代码:
Controller 模板:
```java
@RestController
@RequestMapping("/${table_name}")
public class ${table_nameUpper}Controller {
@Autowired
private ${table_nameUpper}Service ${table_name};
@GetMapping("/{id}")
public ${table_nameUpper}VO getById(@PathVariable("id") Long id) {
${table_nameUpper} entity = ${table_name}.getById(id);
return BeanUtil.copyProperties(entity, ${table_nameUpper}VO.class);
}
@PostMapping
public boolean save(@RequestBody ${table_nameUpper}DTO dto) {
${table_nameUpper} entity = BeanUtil.copyProperties(dto, ${table_nameUpper}.class);
return ${table_name}.save(entity);
}
@PutMapping
public boolean update(@RequestBody ${table_nameUpper}DTO dto) {
${table_nameUpper} entity = BeanUtil.copyProperties(dto, ${table_nameUpper}.class);
return ${table_name}.updateById(entity);
}
@DeleteMapping("/{id}")
public boolean delete(@PathVariable("id") Long id) {
return ${table_name}.removeById(id);
}
@GetMapping("/list")
public List<${table_nameUpper}VO> list() {
List<${table_nameUpper}> entityList = ${table_name}.list();
return BeanUtil.copyProperties(entityList, ${table_nameUpper}VO.class);
}
@GetMapping("/page")
public IPage<${table_nameUpper}VO> page(Page<${table_nameUpper}> page) {
IPage<${table_nameUpper}> entityPage = ${table_name}.page(page);
return entityPage.convert(entity -> BeanUtil.copyProperties(entity, ${table_nameUpper}VO.class));
}
}
```
Service 模板:
```java
public interface ${table_nameUpper}Service extends IService<${table_nameUpper}> {
}
```
ServiceImpl 模板:
```java
@Service
public class ${table_nameUpper}ServiceImpl extends ServiceImpl<${table_nameUpper}Mapper, ${table_nameUpper}> implements ${table_nameUpper}Service {
}
```
Mapper 模板:
```java
public interface ${table_nameUpper}Mapper extends BaseMapper<${table_nameUpper}> {
}
```
Entity 模板:
```java
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class ${table_nameUpper} implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Long id;
#foreach($column in $columns)
@TableField("${column.column_name}")
private ${column.java_type} ${column.column_name};
#end
}
```
DTO 模板:
```java
@Data
public class ${table_nameUpper}DTO {
#foreach($column in $columns)
private ${column.java_type} ${column.column_name};
#end
}
```
VO 模板:
```java
@Data
public class ${table_nameUpper}VO {
private Long id;
#foreach($column in $columns)
private ${column.java_type} ${column.column_name};
#end
}
```
其中,$table_name 表示表名,$table_nameUpper 表示表名的首字母大写形式,$columns 表示表的所有列信息。需要在模板中使用 Velocity 模板引擎进行解析。
阅读全文