业务需求:
综合练习:
实现通过REST接口来对供应商的增删改查功能
利用框架自动生成开发供应商简单维护界面
开发接口:对供应商批量或者单个新增
开发接口:对供应商更新
开发接口:查询供应商
开发接口:删除供应商
实现效果图:
![13265172-1bb27da9b30b56e5.png](https://i-blog.csdnimg.cn/blog_migrate/51bac249098577580a4ecb20ce2c0f43.webp?x-image-process=image/format,png)
image.png
![13265172-dbeb5a34b17ed55b.png](https://i-blog.csdnimg.cn/blog_migrate/6dbed3879d3c276685cd2ea052fac16d.webp?x-image-process=image/format,png)
image.png
![13265172-97083469142dd520.png](https://i-blog.csdnimg.cn/blog_migrate/f092db5f53d7ba55a355aea670f8331a.webp?x-image-process=image/format,png)
image.png
![13265172-fa48bfb6b655a5c5.png](https://i-blog.csdnimg.cn/blog_migrate/330d9544c528fc0dbb65fac954872d7a.webp?x-image-process=image/format,png)
image.png
步骤一:数据表准备:
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;
步骤二:自动生成代码
![13265172-9a419ce9b64f8e38.png](https://i-blog.csdnimg.cn/blog_migrate/c18814b406650dab25c17e23f4156027.webp?x-image-process=image/format,png)
image.png
步骤三:去到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>
![13265172-9f60d57b6886fab0.png](https://i-blog.csdnimg.cn/blog_migrate/d492bc8d298cb95636f0e330b775cfbf.webp?x-image-process=image/format,png)
image.png
步骤四:去到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();
}
![13265172-650b4f1c95e932b7.png](https://i-blog.csdnimg.cn/blog_migrate/cb6a550fe86dc0b97eab777d87876470.webp?x-image-process=image/format,png)
image.png
![13265172-159bc6d59ef92eea.png](https://i-blog.csdnimg.cn/blog_migrate/6fdbde9a817211ac8eccf06d1da958bd.webp?x-image-process=image/format,png)
image.png
![13265172-09529d4bcc7a18b3.png](https://i-blog.csdnimg.cn/blog_migrate/a0b3018191c01bfbf15e3f0b78b8f430.webp?x-image-process=image/format,png)
image.png
![13265172-4ff3ce3bb1ae8328.png](https://i-blog.csdnimg.cn/blog_migrate/f6f267361537d206d1bf39a3407e5e37.webp?x-image-process=image/format,png)
image.png
到这里就结束了,重启,去到postman测试测试吧