备注:后台传过来的json数据一定执行这个操作:eval( “[”+data+"]" ) 将string转换为对象
Controller层代码如下:
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value=“library/”)
public class LibraryController {
@Autowired
public LibraryService libraryService;
/**
跳转到分类页面
@return
*/
@RequestMapping(value=“toListLibrary/”)
public String toListLibrary(){
return “library/listLibrary”;
}
/**
查询所有分类信息
@return
*/
@RequestMapping(value=“findAllLibrary/”)
@ResponseBody
public List findAllLibrary(HttpServletRequest request, HttpServletResponse response){
return libraryService.findAllLibrary();
}
/**
保存分类
@return
*/
@RequestMapping(value=“saveLibrary/”)
@ResponseBody
public String saveLibrary(HttpServletRequest request, HttpServletResponse response){
String libraryId = UUIDUtil.randomUUID();
return libraryId;
}
/**
更新分类名称
@return
*/
@RequestMapping(value=“updateLibraryName/”)
@ResponseBody
public String updateLibraryName(HttpServletRequest request, HttpServletResponse response, Library library) {
String createname=(String) request.getSession().getAttribute(Constants.CURRENT_USER_NAME);
library.setCreate_user(createname);
library.setUpdate_user(createname);
return libraryService.addOrUpdateLibrary(library);
}
/**
删除分类
@return
*/
@RequestMapping(value=“deleteLibrary/”)
@ResponseBody
public String deleteLibrary(HttpServletRequest request, HttpServletResponse response,@RequestParam(value = “id”) String id) {
return libraryService.deleteLibrary(id);
}
}
service层代码如下:
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class LibraryService {
protected final static Log log = LogFactory.getLog(LibraryService.class);
@Autowired
private LibraryMapper libraryMapper;
/**
查询所有分类信息
@return
*/
public List findAllLibrary(){
List listZTree = new ArrayList();
List listLibrary = libraryMapper.findAllLibrary();
String str = “”;
for (int i = 0; i < listLibrary.size(); i++) {
Library library = listLibrary.get(i);//分类信息
str = “{id:’” +library.getId() + “’, pId:’”+library.getPid()+"’, name:""+library.getName()+"" }";//封装ztree需要格式的字符串
log.info(str);
listZTree.add(str);
}
return listZTree;
}
/**
保存或更新分类信息
@param library
@return
*/
public String addOrUpdateLibrary(Library library){
int numFlag = 0;
//根据id查询分类信息
if (StringUtils.isBlank(library.getId())) {
return “error”;
}
int num = libraryMapper.findLibraryById(library.getId());
if (num >0) {//更新信息
library.setUpdate_time(new Date());
library.setCreate_user(null);
library.setPid(null);
numFlag = libraryMapper.updateByPrimaryKeySelective(library);
}else{//插入信息
if(library.getPid().equals(“null”)){
library.setPid(“0”);
}
int orderId = libraryMapper.findLastLibrary(library);
orderId++;
library.setCreate_time(new Date());
library.setUpdate_time(new Date());
library.setOrder_id(orderId);
numFlag = libraryMapper.insert(library);
}
return “success”;
}
/**
删除分类
@param id
@return
*/
public String deleteLibrary(String id){
int num = libraryMapper.deleteByPrimaryKey(id);
return “success”;
}
}
备注:执行完数据操作需要判断返回值,这里我直接返回success活error了。
Mapper层代码如下
import java.util.List;
import java.util.Map;
public interface LibraryMapper extends BaseMapper
/**
查询所有分类信息
@return
*/
public List findAllLibrary();
/**
根据id查询条数
@param id
@return
*/
public int findLibraryById(String id);
/**
查询最大排序号
@return
*/
public int findLastLibrary(Library library);
}
备注:BaseMapper里有几个公用的增删改查的方法,Library是数据库表对应的实体,都很简单,避免代码过多这里就省略了
< resultMap id= “BaseResultMap” type= “Library” >
resultMap>
< sql id= “Base_Column_List” >
id, pid , name, create_time, update_time, is_delete, update_user, create_user, level_id,
order_id
sql>
< select id= “findLibraryById” parameterType =“java.lang.String” resultType= “java.lang.Integer” >
select count(*) from onair_vms_library
where is_delete=1 and id = #{id,jdbcType=VARCHAR}
select>
< select id= “findLastLibrary” resultType =“java.lang.Integer” parameterType=“Library” >
SELECT MAX(order_id) as order_id FROM onair_vms_library
where pid = #{pid,jdbcType=VARCHAR}
select>
< select id= “findAllLibrary” resultMap =“BaseResultMap”>
select
from onair_vms_library
where is_delete = 1 order by order_id
select>
< select id= “selectByPrimaryKey” resultMap =“BaseResultMap” parameterType=“java.lang.String” >
select
from onair_vms_library
where id = #{id,jdbcType=VARCHAR}
select>
< delete id= “deleteByPrimaryKey” parameterType=“java.lang.String” >
update onair_vms_library set is_delete = 0
where id = #{id,jdbcType=VARCHAR}
delete>
< insert id= “insert” parameterType=“Library” >
insert into onair_vms_library (id, pid, name,
create_time, update_time, is_delete,
update_user, create_user, level_id,
order_id)
values (#{id,jdbcType=VARCHAR}, #{pid,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR},
#{create_time,jdbcType=TIMESTAMP}, #{update_time,jdbcType=TIMESTAMP}, #{is_delete,jdbcType=INTEGER},
#{update_user,jdbcType=VARCHAR}, #{create_user,jdbcType=VARCHAR}, #{level_id,jdbcType=INTEGER},
#{order_id,jdbcType=INTEGER})
insert>
< update id= “updateByPrimaryKey” parameterType=“Library” >
update onair_vms_library
set pid = #{pid,jdbcType=VARCHAR},
name = #{name,jdbcType=VARCHAR},
create_time = #{create_time,jdbcType=TIMESTAMP},
update_time = #{update_time,jdbcType=TIMESTAMP},
is_delete = #{is_delete,jdbcType=INTEGER},
update_user = #{update_user,jdbcType=VARCHAR},
create_user = #{create_user,jdbcType=VARCHAR},
level_id = #{level_id,jdbcType=INTEGER},
order_id = #{order_id,jdbcType=INTEGER}
where id = #{id,jdbcType=VARCHAR}
update>
备注:mapper对应的xml
与jsp对应的VO层代码如下:
public class LibraryVo {
/* id */
private String id;
/* 目标id */
private String targetId;
/* pid */
private String pId;
/* 目标pid */
private String targetPId;
/* “inner”:成为子节点,“prev”:成为同级前一个节点,“next”:成为同级后一个节点 */
private String moveType;
public String getId() {
return id;
}
public void setId(String id) {
this. id = id;
}
public String getTargetId() {
return targetId;
}
public void setTargetId(String targetId) {
this. targetId = targetId;
}
public String getpId() {
return pId;
}
public void setpId(String pId) {
this. pId = pId;
}
public String getTargetPId() {
return targetPId;
}
public void setTargetPId(String targetPId) {
this. targetPId = targetPId;
}
public String getMoveType() {
return moveType;
}
public void setMoveType(String moveType) {
this. moveType = moveType;
}
}