實(shí)驗(yàn)室的項(xiàng)目,需要做對(duì)用戶發(fā)布的主題進(jìn)行打標(biāo)簽的功能,糾結(jié)甚久,實(shí)現(xiàn)思路如下: 一、數(shù)據(jù)庫(kù)表的設(shè)計(jì) 1.tag表 create table qa_tag(tag_id int primary key auto_increment,tag_name varchar(32) not null,tag_time timestamp not null default CURRENT_T
實(shí)驗(yàn)室的項(xiàng)目,需要做對(duì)用戶發(fā)布的主題進(jìn)行打標(biāo)簽的功能,糾結(jié)甚久,實(shí)現(xiàn)思路如下:
一、數(shù)據(jù)庫(kù)表的設(shè)計(jì)
1.tag表
create table qa_tag ( tag_id int primary key auto_increment, tag_name varchar(32) not null, tag_time timestamp not null default CURRENT_TIMESTAMP, refered_cnt int not null default 0, user_id int not null, unique (tag_name), constraint foreign key (user_id) references user_info(user_id) ); 2.topic表
create table qa_topic ( topic_id int primary key auto_increment, topic_title varchar(128) not null, topic_body text not null, topic_time timestamp not null default CURRENT_TIMESTAMP, user_id int not null, tags varchar(128) not null default '' );
create table qa_tag_topic
(
record_id int primary key auto_increment,
tag_id int not null,
topic_id int not null,
constraint foreign key (tag_id) references qa_tag(tag_id),
constraint foreign key (topic_id) references qa_topic(topic_id)
);
1.用戶創(chuàng)建主題時(shí),給自己發(fā)布的主題打上了幾個(gè)標(biāo)簽,點(diǎn)擊提交
2.后臺(tái)接受參數(shù)后,先把數(shù)據(jù)插入到qa_topic表中,獲得了topicId;
3.把用戶輸入的標(biāo)簽轉(zhuǎn)成數(shù)組,批量插入到數(shù)據(jù)庫(kù)中,sql代碼如下:
insert into qa_tag(tag_name,user_id) values (#{o.tagName},#{o.userId}) ON DUPLICATE KEY UPDATE refered_cnt = refered_cnt + 1;//如果有重復(fù),則把tag的被引用數(shù)目+1 alter table qa_tag auto_increment = 1//保證tagId的連續(xù)性
insert ignore into qa_tag_topic(tag_id,topic_id) values
(#{o.tagId},#{o.topicId})
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com