实体类entity和sql映射由逆向工程生成。
(除了必须的,拿来即用的工具类)
controller(注入service)----->service----->serviceImpl(注入mapper)
1.controller层
ResourcesController
package cn.offcn.controller;
import cn.offcn.entity.Sources;
import cn.offcn.service.SourcesService;
import cn.offcn.utils.OAResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/resources")
public class ResourcesController {
@Autowired
private SourcesService sourcesService;
@RequestMapping("/{page}")
public String forwardPage(@PathVariable("page") String page, Integer id, Model model){
//获取id并放到request范围
model.addAttribute("id",id);
return "sources/"+page;
}
@ResponseBody
@RequestMapping("/getRootSourcesByPid")
public List<Sources> getRootSourcesByPid(@RequestParam(defaultValue = "0") Integer pid){
return sourcesService.getRootSourcesByPid(pid);
}
@ResponseBody
@RequestMapping("/getParentNodes")
public List<Sources> getParentNodes(@RequestParam(defaultValue = "0") Integer pid){
List<Sources> parentList=new ArrayList<>();
return sourcesService.getParentNodes(pid,parentList);
}
@ResponseBody
@RequestMapping("/addSources")
public OAResult addSources(Sources sources){
return sourcesService.addSources(sources);
}
@ResponseBody
@RequestMapping("/getSourcesById")
public Sources getSourcesById(int id){
return sourcesService.getSourcesById(id);
}
@ResponseBody
@RequestMapping("/updateSourcesById")
public OAResult updateSourcesById(Sources sources){
return sourcesService.updateSourcesById(sources);
}
@ResponseBody
@RequestMapping("/deleteResources")
public OAResult deleteResources(Integer id){
return sourcesService.deleteResources(id);
}
}
IndexController
package cn.offcn.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/main")
public class IndexController {
@RequestMapping("/{page}")
public String forwardPage(@PathVariable("page") String page){
return "main/"+page;
}
}
2.SourcesService
package cn.offcn.service;
import cn.offcn.entity.Sources;
import cn.offcn.utils.OAResult;
import java.util.List;
public interface SourcesService {
public List<Sources> getRootSourcesByPid(int pid);
public List<Sources> getParentNodes(int pid, List<Sources> parentList);
public OAResult addSources(Sources sources);
public Sources getSourcesById(int id);
public OAResult updateSourcesById(Sources sources);
public OAResult deleteResources(Integer id);
}
3.SourcesServiceImpl
package cn.offcn.service.impl;
import cn.offcn.entity.Sources;
import cn.offcn.entity.SourcesExample;
import cn.offcn.mapper.SourcesMapper;
import cn.offcn.service.SourcesService;
import cn.offcn.utils.OAResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class SourcesServiceImpl implements SourcesService {
/*
example没加条件:没写example.createCriteria();
执行查询:selectByExample(example).====就会查询全部。
一.分类: 框架
Mybatis逆向工程会生成实例及实例对应的example(用于添加条件,相当于where后的部分)
xxxExample example = new xxxExample();
Criteria
criteria = example.createCriteria();
二.方法说明:
1.添加升序排列条件,DESC为降序
example.setOrderByClause(“字段名ASC”)
2.去除重复,boolean类型,true为选择不重复的记录
example.setDistinct(false)
3.添加字段xxx为null的条件
criteria.andXxxIsNull
4.添加字段xxx不为null的条件
criteria.andXxxIsNotNull
5.添加xxx字段等于value条件
criteria.andXxxEqualTo(value)
6.添加xxx字段不等于value条件
criteria.andXxxNotEqualTo(value)
7.添加xxx字段大于value条件
criteria.andXxxGreaterThan(value)
8.添加xxx字段大于等于value条件
criteria.andXxxGreaterThanOrEqualTo(value)
9.添加xxx字段小于value条件
criteria.andXxxLessThan(value)
10.添加xxx字段小于等于value条件
criteria.andXxxLessThanOrEqualTo(value)
11.添加xxx字段值在List
criteria.andXxxIn(List)
12.不添加xxx字段值在List
criteria.andXxxNotIn(List)
13.添加xxx字段值在之间
criteria.andXxxBetween(value1,value2)
14.添加xxx字段值不在之间
criteria.andXxxNotBetween(value1,value2)
*/
@Autowired
private SourcesMapper sourcesMapper;
@Override
public List<Sources> getRootSourcesByPid(int pid) {
SourcesExample sourcesExample=new SourcesExample();
SourcesExample.Criteria criteria = sourcesExample.createCriteria();
//5.添加xxx字段等于value条件 criteria.andXxxEqualTo(value)
criteria.andPidEqualTo(pid);
// 执行查询:selectByExample(example).====就会查询全部。
List<Sources> sourcesList= sourcesMapper.selectByExample(sourcesExample);
//求子项
for(Sources sources : sourcesList){
List<Sources> subList=getRootSourcesByPid(sources.getId());
sources.setChildren(subList);
}
return sourcesList;
}
public List<Sources> getParentNodes(int pid, List<Sources> parentList){
SourcesExample sourcesExample=new SourcesExample();
SourcesExample.Criteria criteria = sourcesExample.createCriteria();
criteria.andPidEqualTo(pid);
List<Sources> sourcesList= sourcesMapper.selectByExample(sourcesExample);
for(Sources sources : sourcesList){
if(isExistsChildrenNodes(sources.getId())){
parentList.add(sources);
}
sourcesList=getParentNodes(sources.getId(),parentList);
}
return parentList;
}
/**
* 判断id有没有子节点,如果有返回true,没有返回false
* @param id
* @return
*/
public boolean isExistsChildrenNodes(int id){
SourcesExample sourcesExample=new SourcesExample();
SourcesExample.Criteria criteria = sourcesExample.createCriteria();
criteria.andPidEqualTo(id);
List<Sources> sourcesList= sourcesMapper.selectByExample(sourcesExample);
if(sourcesList!=null && sourcesList.size()>0){
return true;
}
return false;
}
public OAResult addSources(Sources sources){
int rows=sourcesMapper.insert(sources);
if(rows==1){
return OAResult.ok(200,"操作成功");
}
return OAResult.ok(400,"操作失败");
}
public Sources getSourcesById(int id){
return sourcesMapper.selectByPrimaryKey(id);
}
public OAResult updateSourcesById(Sources sources){
int rows= sourcesMapper.updateByPrimaryKey(sources);
if(rows==1){
return OAResult.ok(200,"操作成功");
}
return OAResult.ok(400,"操作失败");
}
public OAResult deleteResources(Integer id){
int rows= sourcesMapper.deleteByPrimaryKey(id);
if(rows==1){
return OAResult.ok(200,"操作成功");
}
return OAResult.ok(400,"操作失败");
}
}