VUE Element-UI Tree JAVA Mybatis实现


JAVA 实体类

package cn.maxhou.jxcht.entity;

public class Category {

    private Integer id;

    private String name;

    private Integer parentId;

    private Integer isParent;

    private Integer sort;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getParentId() {
        return parentId;
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    public Integer getIsParent() {
        return isParent;
    }

    public void setIsParent(Integer isParent) {
        this.isParent = isParent;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    @Override
    public String toString() {
        return "Category{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", parentId=" + parentId +
                ", isParent=" + isParent +
                ", sort=" + sort +
                '}';
    }
}

JAVA DAO 类

package cn.maxhou.jxcht.dao;

import cn.maxhou.jxcht.entity.Category;
import org.apache.ibatis.annotations.*;

import java.util.List;

//标注这个接口是MyBatis映射
@Mapper
public interface CategoryMapper {

	//查询所有
	@Select("SELECT id,name,parent_id AS parentId,is_parent AS isParent,sort FROM category")
	List findCategory();

	//条件查询
	@Select("SELECT id,name,parent_id AS parentId,is_parent AS isParent,sort FROM category where id=#{id}")
	Category findCategoryById(long id);

	//插入语句
	@Insert("insert into category(name,parent_id,is_parent,sort ) values(#{name},#{parentId},#{isParent},#{sort}) ")
	//生成主键
	@SelectKey(statement = "SELECT LAST_INSERT_ID()",before=false,keyProperty="id",resultType=Integer.class)
	void addCategory(Category category);

	//插入语句
	@Insert("insert into category(name,parent_id,isParent,sort ) values(#{name},#{parentId},#{isParent},#{sort}) ")
	//生成主键
	@SelectKey(statement = "SELECT LAST_INSERT_ID()",before=false,keyProperty="id",resultType=Integer.class)
	void save(Category category);

	//修改语句
	@Update("Update category set name=#{name},parent_id=#{parentId},is_parent=#{isParent},sort=#{sort} where id=#{id}")
	void updateCategory(Category category);

	//删除语句
	@Delete("delete from category where id=#{id}")
	void deleteCategory(long id);
}

JAVA 控制器类

package cn.maxhou.jxcht.controller;

import cn.maxhou.jxcht.dao.CategoryMapper;
import cn.maxhou.jxcht.entity.Category;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CategoryResource {

	private CategoryMapper categoryMapper;

	public CategoryResource(CategoryMapper categoryMapper) {
		this.categoryMapper = categoryMapper;
	}

	@CrossOrigin
	@GetMapping("/category/list")
	public ResponseEntity>findAll(){
		return new ResponseEntity<>(categoryMapper.findCategory(),HttpStatus.OK);
	}

	@CrossOrigin
	@RequestMapping(value="/category/add",method=RequestMethod.POST,headers="Accept=application/json")
	public ResponseEntityaddCategory(@RequestBody Category category){

		System.out.println(category.toString());

		categoryMapper.addCategory(category);
		return new ResponseEntity<>(null, HttpStatus.CREATED);
	}

	@CrossOrigin
	@RequestMapping(value="/category/addByParentId",method=RequestMethod.POST,headers="Accept=application/json")
	public ResponseEntitysaveCategoryById(@RequestBody Category category){
		categoryMapper.addCategory(category);
		return new ResponseEntity<>(null, HttpStatus.CREATED);
	}


	@CrossOrigin
	@RequestMapping(value = "/category",method = RequestMethod.PUT,headers="Accept=application/json")
	public ResponseEntityupdateCategory(@RequestBody Category category){

		System.out.println(category.toString());

		categoryMapper.updateCategory(category);
		return new ResponseEntity<>(null,HttpStatus.CREATED);
	}

	@CrossOrigin
	@RequestMapping(value="/category/{id}",method=RequestMethod.DELETE,headers="Accept=application/json")
	public ResponseEntitydelete(@PathVariable(name="id") long id){
		categoryMapper.deleteCategory(id);
		return new ResponseEntity<>("已删除",HttpStatus.NO_CONTENT);
	}
}


VUE 文件






vue