业务需求:
综合练习:
实现通过REST接口来对供应商的增删改查功能
利用框架自动生成开发供应商简单维护界面
开发接口:对供应商批量或者单个新增
开发接口:对供应商更新
开发接口:查询供应商
开发接口:删除供应商
实现效果图:
步骤一:数据表准备:
CREATE TABLE `wht_ora_20796_vendor` (
`VENDOR_ID` int(128) NOT NULL AUTO_INCREMENT,
`VENDOE_NUMBER` varchar(128) DEFAULT NULL COMMENT '供应商编码',
`VENDOR_NAME` varchar(128) DEFAULT NULL,
`VENDOR_TYPE_CODE` varchar(128) DEFAULT NULL COMMENT '供应商类型',
`ADDRESS` varchar(128) DEFAULT NULL COMMENT '供应商地址',
`TEL_NUMBER` varchar(128) DEFAULT NULL,
`BEGIN_DATE` date DEFAULT NULL,
`END_DATE` date DEFAULT NULL,
`OBJECT_VERSION_NUMBER` int(11) DEFAULT '1' COMMENT '版本号',
`CREATION_DATE` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`CREATED_BY` int(11) NOT NULL DEFAULT '-1',
`LAST_UPDATED_BY` int(11) NOT NULL DEFAULT '-1',
`LAST_UPDATE_DATE` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`LAST_UPDATE_LOGIN` int(11) DEFAULT NULL,
`PROGRAM_ID` int(11) DEFAULT NULL,
`REQUEST_ID` int(11) DEFAULT NULL,
`ATTRIBUTE_CATEGORY` varchar(30) DEFAULT NULL,
`ATTRIBUTE1` varchar(240) DEFAULT NULL,
`ATTRIBUTE2` varchar(240) DEFAULT NULL,
`ATTRIBUTE3` varchar(240) DEFAULT NULL,
`ATTRIBUTE4` varchar(240) DEFAULT NULL,
`ATTRIBUTE5` varchar(240) DEFAULT NULL,
`ATTRIBUTE6` varchar(240) DEFAULT NULL,
`ATTRIBUTE7` varchar(240) DEFAULT NULL,
`ATTRIBUTE8` varchar(240) DEFAULT NULL,
`ATTRIBUTE9` varchar(240) DEFAULT NULL,
`ATTRIBUTE10` varchar(240) DEFAULT NULL,
`ATTRIBUTE11` varchar(240) DEFAULT NULL,
`ATTRIBUTE12` varchar(240) DEFAULT NULL,
`ATTRIBUTE13` varchar(240) DEFAULT NULL,
`ATTRIBUTE14` varchar(240) DEFAULT NULL,
`ATTRIBUTE15` varchar(240) DEFAULT NULL,
PRIMARY KEY (`VENDOR_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
步骤二:自动生成代码
步骤三:去到Ora20796VendorMapper.xml写sql语句
<!--查询经销商-->
<select id="selectVendorRest" resultMap="BaseResultMap" parameterType="wht.ora20796.dto.Ora20796Vendor">
SELECT
VENDOR_ID,
VENDOE_NUMBER,
VENDOR_NAME,
VENDOR_TYPE_CODE,
ADDRESS,
TEL_NUMBER,
BEGIN_DATE,
END_DATE
FROM wht_ora_20796_vendor
</select>
<!--保存经销商-->
<insert id="saveVendorRest" parameterType="wht.ora20796.dto.Ora20796Vendor">
insert into wht_ora_20796_vendor (VENDOE_NUMBER,VENDOR_NAME)value(#{vendoeNumber},#{vendoeNumber})
</insert>
<!--更改经销商-->
<update id="updateVendorRest" parameterType="wht.ora20796.dto.Ora20796Vendor">
update wht_ora_20796_vendor set VENDOE_NUMBER=#{vendoeNumber} where VENDOR_ID=#{vendorId}
</update>
<!--删除经销商-->
<delete id="deleteVendorRest" parameterType="wht.ora20796.dto.Ora20796Vendor">
delete from wht_ora_20796_vendor where VENDOR_ID=#{vendorId}
</delete>
步骤四:去到Ora20796VendorMapper.java
注意!!!方法名要和Ora20796VendorMapper.xml里的id一样
package wht.ora20796.mapper;
import com.hand.hap.mybatis.common.Mapper;
import wht.ora20796.dto.Ora20796Vendor;
import java.util.List;
public interface Ora20796VendorMapper extends Mapper<Ora20796Vendor> {
/**
* 查询经销商
* @param dto
* @return
*/
List<Ora20796Vendor> selectVendorRest(Ora20796Vendor dto);
/**
* rest保存经销商
*
* @param criteria
*/
void saveVendorRest(Ora20796Vendor criteria);
/**
* 更新经销商
*
* @param criteria
*/
void updateVendorRest(Ora20796Vendor criteria);
/**
* 删除经销商
* @param criteria
*/
void deleteVendorRest(Ora20796Vendor criteria);
}
步骤五:去到控制类Ora20796VendorController.java写我们的rest
@Autowired
private IOra20796VendorService service;
@Autowired
private Ora20796VendorMapper mapper;
/*--------自定义rest测试-----开始--------*/
//rest查询
@RequestMapping(
value = {"/api/public/wht/ora20796/vendor/rest/query/v1"},
method = {RequestMethod.GET, RequestMethod.POST}
)
@ResponseBody
@HapInbound(apiName = "wht.ora20796.vendor.rest.query.v1")
public ResponseData queryVendor(Ora20796Vendor dto, @RequestParam(defaultValue = DEFAULT_PAGE) int page,
@RequestParam(defaultValue = DEFAULT_PAGE_SIZE) int pageSize, HttpServletRequest request) {
IRequest requestContext = createRequestContext(request);
return new ResponseData(mapper.selectVendorRest(dto));
}
//rest添加
@RequestMapping(
value = {"/api/public/wht/ora20796/vendor/rest/add/v1"},
method = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT}
)
@ResponseBody
@HapInbound(apiName = "wht.ora20796.vendor.rest.add.v1")
public ResponseData saveVendor(@RequestBody List<Ora20796Vendor> dto, BindingResult result, HttpServletRequest request) {
for (Ora20796Vendor ora20796Vendor : dto) {
mapper.saveVendorRest(ora20796Vendor);
}
return new ResponseData();
}
//更新经销商
@RequestMapping(
value = {"/api/public/wht/ora20796/vendor/rest/update/v1"},
method = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT}
)
@ResponseBody
@HapInbound(apiName = "wht.ora20796.vendor.rest.update.v1")
public ResponseData updateVendor(@RequestBody List<Ora20796Vendor> dto, BindingResult result, HttpServletRequest request) {
for (Ora20796Vendor ora20796Vendor : dto) {
mapper.updateVendorRest(ora20796Vendor);
}
return new ResponseData();
}
//删除经销商
@RequestMapping(
value = {"/api/public/wht/ora20796/vendor/rest/delete/v1"},
method = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE}
)
@ResponseBody
@HapInbound(apiName = "wht.ora20796.vendor.rest.delete.v1")
public ResponseData deleteVendor(@RequestBody List<Ora20796Vendor> dto, BindingResult result, HttpServletRequest request) {
for (Ora20796Vendor ora20796Vendor : dto) {
mapper.deleteVendorRest(ora20796Vendor);
}
return new ResponseData();
}
到这里就结束了,重启,去到postman测试测试吧