发表于: 2020-06-17 23:42:47

2 1699


今天完成的事情:完成基本5个模块的增删改查,作品分页,分页用的是pagehelper,类目自关联查询,今天大部分时间解决昨天新增接口的问题。
明天计划的事情:增加相应的动态查询在mapper文件中。再铺厚点业务层的东西。再看下能不能用封装的方法把分类查询出来。
遇到的问题:新增接口是因为自己没有填写完整字段,导致测试失败。但是那个int类型字段我数据库明明设计的可以为空,但是必须要添加,目前原因还没找到。
收获:

就贴下类目的分类查询,其他的我上传github.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
       PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hyx.dao.CatalogDao">
<resultMap id="forCatalog" type="Catalog">
<id column="id" property="id"  javaType="Integer"></id>
<result column="create_at" property="createAt" javaType="Long"></result>
<result column="update_at" property="updateAt" javaType="Long"></result>
<result column="create_by" property="createBy" javaType="String"></result>
<result column="create_by" property="updateBy" javaType="String"></result>
</resultMap>
<insert id="addCatalog" useGeneratedKeys="true" keyProperty="id" parameterType="Catalog">
insert into catalog (name,parent_id,create_at,update_at,create_by,update_by)value(#{name},#{parentId},#{createAt},#{updateAt},#{createBy},#{updateBy})
</insert>
<!--查询所有分类并得到所有父类-->
   <select id="findCatalogParent" resultMap="forCatalog" >
select c.id,c.name,c.parent_id,c.create_at,c.update_at,c.create_by,c.update_by
from catalog AS c left join catalog as g on c.parent_id=g.id
</select>
<!--根据id查询所有的子类-->
   <select id="findCatalogById" resultMap="forCatalog" >
select c.id,c.name,c.parent_id,c.create_at,c.update_at,c.create_by,c.update_by
from catalog AS c left join catalog as g on c.parent_id=g.id where c.parent_id=#{id}
</select>
<update id="updateCatalog" parameterType="Catalog">
update catalog set intro=#{intro},update_at=#{updateAt},update_by=#{updateBy} where id=#{id}
</update>
<delete id="deleteCatalog" parameterType="Integer">
delete from catalog where id = #{id}
</delete>
</mapper>

控制层

@Controller
@RequestMapping("/catalog")
public class CatalogController {
@Resource
   private CatalogService catalogService;
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
   public Result addCatalog(Catalog catalog){
int num = catalogService.addCatalog(catalog);
return Result.back(num);
}
@RequestMapping(value = "/all",method = RequestMethod.GET)
@ResponseBody
   public Result findCatalogParent(){
List<Catalog> listCatalog = catalogService.findCatalogParent();
return Result.back(listCatalog);
}
@RequestMapping(value = "/all",method = RequestMethod.GET)
@ResponseBody
   public Result findCatalogById(int id){
List<Catalog> listCatalog = catalogService.findCatalogById(id);
return Result.back(listCatalog);
}
@RequestMapping(method = RequestMethod.PUT)
@ResponseBody
   public Result updateCatalog(@RequestBody Catalog catalog){
int num = catalogService.updateCatalog(catalog);
return Result.back(num);
}
@RequestMapping(value = "{id}",method = RequestMethod.DELETE)
@ResponseBody
   public Result deleteCatalog(@PathVariable int id){
int num = catalogService.deleteCatalog(id);
return Result.back(num);
}
}



返回列表 返回列表
评论

    分享到