领悟书生写论坛之分页及版面功能

这是领悟书生写论坛的第二课,主要是初步完成了分类和版面的功能,具体效果看http://www.naxsu.com/bbs

下面详细来解读一下:

一、数据库设计 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
CREATE  TABLE  `bbs_category` (
    `id`  int (11)  NOT  NULL  AUTO_INCREMENT,
    ` name varchar (100)  NOT  NULL ,
    `orderId`  int (11)  DEFAULT  NULL ,
    PRIMARY  KEY  (`id`)
  )
CREATE  TABLE  `bbs_forum` (
    `id`  int (11)  NOT  NULL  AUTO_INCREMENT,
    ` name varchar (100)  NOT  NULL ,
    `iconPath`  varchar (200)  DEFAULT  NULL ,
    `orderId`  int (11)  DEFAULT  NULL ,
    `topicCount`  int (11)  DEFAULT  NULL ,
    `articleCount`  int (11)  DEFAULT  NULL ,
    `categoryId`  int (11)  DEFAULT  NULL ,
    PRIMARY  KEY  (`id`),
    KEY  `FK_bbs_forum` (`categoryId`),
    CONSTRAINT  `FK_bbs_forum`  FOREIGN  KEY  (`categoryId`)  REFERENCES `bbs_category` (`id`)
  )

二、实体类

分类

1
2
3
4
5
6
7
public  class  Category {
     private  int  id; //id
     private  String name; //分类名称
     private  int  orderId; //排序号
     private  Set<Forum> forums; //该分类下的版面
     //set get method   
}

版面 
1
2
3
4
5
6
7
8
9
10
public  class  Forum {
     private  int  id;
     private  String name; //版面名称
     private  String iconPath; //版面图标的地址,这个以后可能要存在数据库
     private  int  orderId; //排序号
     private  int  topicCount; //主题数
     private  int  articleCount; //文章数(回复数)
     private  Category category; //所属分类
     //set get method

三、mybatis映射文件

由于本人的网站是用springmvc+mybatis开发,所以mybatis的映射文件是异常重要,首先来看看分类的:

CategoryMapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<? 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 = "bbs_category" >
     < select  id = "getCategoryById"  resultType = "BBS_Category" >
         select id,name from bbs_category where id=#{id}
     </ select >
     < insert  id = "save"  parameterType = "BBS_Category" >
         insert into bbs_category(name,orderId) values(#{name},LAST_INSERT_ID()+1)
     </ insert >
     < select  id = "getCategorys"  resultMap = "ResultBBS_CategoryMap" >
     select id,name from bbs_category order by orderId;
     </ select >
     < resultMap  type = "BBS_Category"  id = "ResultBBS_CategoryMap" >
     < id  property = "id"  column = "id"  />
     < result  property = "name"  column = "name"  />
     < result  property = "orderId"  column = "orderId"  />
     <!-- 一个分类有多个版面  -->
     < collection  property = "forums"  column = "id" select = "bbs_forum.getforumsByCategoryId" />
     </ resultMap >
</ mapper >

在这里有几个知识点要说一下,第一个是排序号插入和ID号一样,用LAST_INSERT_ID()获取到最后一个ID,然后+1即是将要插入的ID号;第二是select="bbs_forum.getforumsByCategoryId"/>,这个是从版面的映射文件中查出该分类下所有的版面。


版面的映射文件ForumMapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<? 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 = "bbs_forum" >
     < insert  id = "save"  parameterType = "BBS_Forum" >
     insert into bbs_forum(name,orderId,categoryId) values(#{name},LAST_INSERT_ID()+1,#{category.id})
     </ insert >
     < select  id = "getforumsByCategoryId"  parameterType = "Integer" resultMap = "ResultBBS_ForumMap" >
     select id,name,topicCount,articleCount,iconPath from bbs_forum where categoryId=#{categoryId}
     </ select >
     < resultMap  type = "BBS_Forum"  id = "ResultBBS_ForumMap" >
     < id  property = "id"  column = "id"  />
     < result  property = "name"  column = "name"  />
     < result  property = "iconPath"  column = "iconPath"  />
     < result  property = "topicCount"  column = "topicCount"  />
     < result  property = "articleCount"  column = "articleCount"  />
     < association  property = "category"  column = "categoryId" javaType = "com.naxsu.bbs.entity.Category" select = "bbs_category.getCategoryById" />
     </ resultMap >
</ mapper >

上面的映射文件比较简单,所以也不做多解释。

另外关于dao,service层在这里就不做多描述,因为是很简单的。本文章就写到此为止,期待第三课。

本文出自http://www.naxsu.com/article/28,转载请注明出处。

 

你可能感兴趣的:(领悟书生写论坛之分页及版面功能)