1.1.1 Controller
package com.geji.controller;
import com.geji.pojo.Category;
import com.geji.pojo.Result;
import com.geji.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/category")
public class CategoryController {
@Autowired
private CategoryService categoryService;
@PostMapping
public Result add(@RequestBody @Validated(Category.Add.class) Category category) {
categoryService.add(category);
return Result.success();
}
服务器托管网 @GetMapping
public Result> list(){
List cs = categoryService.list();
return Result.success(cs);
}
@GetMapping("/detail")
public Result detail(Integer id){
Category c = categoryService.findById(id);
return Result.success(c);
}
@PutMapping
public Result update(@RequestBody @Validated(Category.Update.class) Category category){
categoryService.update(category);
return Result.success();
}
@DeleteMapping
public Result delete(Integer id){
categoryService.deleteById(id);
return Result.success();
}
}
1.1.2 Service
package com.geji.service;
import com.geji.pojo.Category;
import java.util.List;
public interface CategoryService {
void add(Category category);
List list();
Category findById(Integer id);
void update(Category category);
void deleteById(Integer id);
}
1.1.3 ServiceImpl
package com.geji.service.impl;
import com.geji.mapper.CategoryMapper;
import com.geji.pojo.Category;
import com.geji.service.CategoryService;
import com.geji.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
@Service
publ服务器托管网ic class CategoryServiceImpl implements CategoryService {
@Autowired
private CategoryMapper categoryMapper;
@Override
public void add(Category category) {
//补充属性值
category.setCreateTime(LocalDateTime.now());
category.setUpdateTime(LocalDateTime.now());
Map map = ThreadLocalUtil.get();
Integer userId = (Integer) map.get("id");
category.setCreateUser(userId);
categoryMapper.add(category);
}
@Override
public List list() {
Map map = ThreadLocalUtil.get();
Integer userId = (Integer) map.get("id");
return categoryMapper.list(userId);
}
@Override
public Category findById(Integer id) {
Category c = categoryMapper.findById(id);
return c;
}
@Override
public void update(Category category) {
category.setUpdateTime(LocalDateTime.now());
categoryMapper.update(category);
}
@Override
public void deleteById(Integer id) {
categoryMapper.deleteById(id);
}
}
1.1.4 Mapper
package com.geji.mapper;
import com.geji.pojo.Category;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface CategoryMapper {
//新增
@Insert("insert into category(category_name,category_alias,create_user,create_time,update_time) " +
"values(#{categoryName},#{categoryAlias},#{createUser},#{createTime},#{updateTime})")
void add(Category category);
//查询所有
@Select("select * from category where create_user = #{userId}")
List list(Integer userId);
//根据id查询
@Select("select * from category where id = #{id}")
Category findById(Integer id);
//更新
@Update("update category set category_name=#{categoryName},category_alias=#{categoryAlias},update_time=#{updateTime} where id=#{id}")
void update(Category category);
//根据id删除
@Delete("delete from category where id=#{id}")
void deleteById(Integer id);
}
1.1.5 Postman测试
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
我们参加了本次大学生创新创业服务外包大赛,在项目中大量使用到了合合信息所提供的api进行相关功能实现,所以在这里写一篇博客分享一下我们在项目的实际推进中关于TextIn.com API使用心得 我们的产品是一款面向公司管理的REP微信小程序,由于需要覆盖大部分…