springboot-web一些默认实现
一、引入依赖
以maven管理依赖的项目举例
<dependency>
<groupId>com.github.wpyuan</groupId>
<artifactId>default-core</artifactId>
<version>0.0.1</version>
</dependency>@SpringBootApplication
@MapperScan({"com.github.mybatis.crud.mapper"})
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}注意:com.github.mybatis.crud.mapper这是mybatis-crud插件的默认mapper,需要注册,注册方法很多,自行选择用自己喜欢的方式注册,这里只举个栗子
@RestController
@RequestMapping("/employee")
public class EmployeeController extends DefaultController<Employee>{
}注意:com.github.defaultcore.controller.DefaultController
如果有接口层,则一样继承使用
public interface EmployeeService extends DefaultService<Employee> {
}和serviceImpl
@Service
public class EmployeeServiceImpl extends DefaultServiceImpl<Employee> implements EmployeeService {
}注意:com.github.defaultcore.service.DefaultService和com.github.defaultcore.service.impl.DefaultServiceImpl
public interface EmployeeMapper extends DefaultMapper<Employee>, BatchInsertMapper<Employee> {
}注意:com.github.mybatis.crud.mapper.DefaultMapper这个是必须继承的,com.github.mybatis.crud.mapper.BatchInsertMapper这是批量插入实现,可选择性继承使用
上面示例一顿操作的结果是,直接完成了crud的后端restful风格的接口,具体如下:
- 新建接口,接收返回数据格式
json
POST http://ip:port/employee/create
- 新建接口,接收数据格式
multipart/form-data或queryString,返回数据格式json
POST http://ip:port/employee/create-form
- 删除接口,接收数据格式
queryString,返回数据格式json
DELETE http://ip:port/employee/remove
- 更新接口,接收数据格式
queryString,返回数据格式json
PUT http://ip:port/employee/update
- 单条记录明细接口,接收数据格式
queryString,返回数据格式json
GET http://ip:port/employee/detail
- 列表查询接口,接收数据格式
queryString,返回数据格式json
GET http://ip:port/employee/list
接下来直接注入上述例子bean使用即可