Java项目实战 - CMS实现

最终实现效果:

内容分类管理

实现内容分类列表的展示和内容的添加功能。
Service:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {

@Autowired
private TbContentCategoryMapper contentCategoryMapper;

@Override
public List<EasyUiTreeNode> getContentCategoryList(long parentId) {

TbContentCategoryExample example = new TbContentCategoryExample();
Criteria criteria = example.createCriteria();
criteria.andParentIdEqualTo(parentId);

List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);


List<EasyUiTreeNode> resultList = new ArrayList<>();
for (TbContentCategory tbContentCategory : list) {
EasyUiTreeNode node = new EasyUiTreeNode();
node.setId(tbContentCategory.getId());
node.setText(tbContentCategory.getName());
node.setState(tbContentCategory.getIsParent()?"closed":"open");
resultList.add(node);
}
return resultList;
}

@Override
public TaotaoResult createContentCategory(long parentId, String name) {
TbContentCategory contentCategory = new TbContentCategory();

contentCategory.setParentId(parentId);
contentCategory.setIsParent(false);
contentCategory.setName(name);
contentCategory.setStatus(1);
contentCategory.setSortOrder(1);
contentCategory.setCreated(new Date());
contentCategory.setUpdated(new Date());

contentCategoryMapper.insert(contentCategory);

TbContentCategory parentCategory = contentCategoryMapper.selectByPrimaryKey(parentId);
if(!parentCategory.getIsParent()){
parentCategory.setIsParent(true);
contentCategoryMapper.updateByPrimaryKey(parentCategory);
}

return TaotaoResult.ok(contentCategory);
}

}

Controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@Controller
@RequestMapping("/content/category")
public class ContentCategoryController {

@Autowired
private ContentCategoryService contentCategoryService;

@RequestMapping("/list")
@ResponseBody
public List<EasyUiTreeNode> getContentCategoryList(@RequestParam(value="id", defaultValue="0")long parentId) {
List<EasyUiTreeNode> result = contentCategoryService.getContentCategoryList(parentId);

return result;
}

@RequestMapping("/create")
@ResponseBody
public TaotaoResult createContentCategory(long parentId, String name){
TaotaoResult result = contentCategoryService.createContentCategory(parentId, name);

return result;
}


}

内容管理

实现内容列表的展示和内容的添加功能。
Service:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@Service
public class ContentServiceImpl implements ContentService {

@Autowired
private TbContentMapper contentMapper;

@Override
public EasyUiDataGridResult getContentList(long categoryId, int page, int rows) {
TbContentExample example = new TbContentExample();

PageHelper.startPage(page, rows);
Criteria criteria = example.createCriteria();
criteria.andCategoryIdEqualTo(categoryId);

List<TbContent> list = contentMapper.selectByExample(example);

EasyUiDataGridResult result = new EasyUiDataGridResult();
result.setRows(list);

PageInfo<TbContent> pageInfo = new PageInfo<>(list);
result.setTotal(pageInfo.getTotal());

return result;
}

@Override
public TaotaoResult createContent(TbContent tbContent) {
tbContent.setCreated(new Date());
tbContent.setUpdated(new Date());

contentMapper.insert(tbContent);

return TaotaoResult.ok();
}

}

Controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Controller
@RequestMapping("/content")
public class ContentController {

@Autowired
private ContentService contentService;

@RequestMapping("/query/list")
@ResponseBody
public EasyUiDataGridResult getContentList(long categoryId, int page, int rows){
EasyUiDataGridResult result = contentService.getContentList(categoryId, page, rows);
return result;
}

@RequestMapping("/save")
@ResponseBody
public TaotaoResult createContent(TbContent tbContent){
TaotaoResult result = contentService.createContent(tbContent);
return result;
}

}

问题

逆向工程自动生成的insert方法不会自动帮我们给主键的值赋值,需要添加主键返回,在TbContentCategoryMapper.xml文件中做如下修改:

<insert id="insert" parameterType="com.izheyi.pojo.TbContentCategory" >
      <selectKey keyProperty="id" resultType="long" order="AFTER">
          SELECT LAST_INSERT_ID()
      </selectKey>
    insert into tb_content_category (id, parent_id, name, 
      status, sort_order, is_parent, 
      created, updated)
    values (#{id,jdbcType=BIGINT}, #{parentId,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, 
      #{status,jdbcType=INTEGER}, #{sortOrder,jdbcType=INTEGER}, #{isParent,jdbcType=BIT}, 
      #{created,jdbcType=TIMESTAMP}, #{updated,jdbcType=TIMESTAMP})
  </insert>
  <insert id="insertSelective" parameterType="com.izheyi.pojo.TbContentCategory" >
      <selectKey keyProperty="id" resultType="long" order="AFTER">
          SELECT LAST_INSERT_ID()
      </selectKey>
    insert into tb_content_category
    <trim prefix="(" suffix=")" suffixOverrides="," >
唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!