Mybatsi Lofter后台功能实现(一对多),高效查询方式

功能描述:

需求说明:查看某篇文章下面的评论

首先表结果如下:(文章是一的一方,评论是多的一方)

 

Mybatsi Lofter后台功能实现(一对多),高效查询方式_第1张图片

表语句如下:

 

/*
Navicat MySQL Data Transfer

Source Server         : 120.78.225.98_3306
Source Server Version : 50717
Source Host           : 120.78.225.98:3306
Source Database       : lofter

Target Server Type    : MYSQL
Target Server Version : 50717
File Encoding         : 65001

Date: 2018-10-20 16:08:55
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for content
-- ----------------------------
DROP TABLE IF EXISTS `content`;
CREATE TABLE `content` (
  `cid` varchar(255) DEFAULT NULL,
  `contents` varchar(255) DEFAULT NULL,
  `publisher` varchar(255) DEFAULT NULL,
  `title` varchar(255) DEFAULT NULL,
  `addTime` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `userId` varchar(255) DEFAULT NULL,
  KEY `FK_dyy1e2asd9khdcxduewg02y9` (`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Table structure for comment
-- ----------------------------
DROP TABLE IF EXISTS `comment`;
CREATE TABLE `comment` (
  `commenter` varchar(255) DEFAULT NULL,
  `comtents` varchar(255) DEFAULT NULL,
  `contentId` varchar(255) DEFAULT NULL,
  `addTime` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  KEY `FK_ne3ebmfwk4qaciiqotnbdy9um` (`contentId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

mybatis实体文件如下:

文章实体:

package com.ly.pojo;

import java.util.List;
import java.util.Set;

public class Content {

	private String cid;
	private String title;
	private String publisher;
	private String addTime;
	private String contents;
	private List comments;

	public String getCid() {
		return cid;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getPublisher() {
		return publisher;
	}

	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}

	public String getAddTime() {
		return addTime;
	}

	public void setAddTime(String addTime) {
		this.addTime = addTime;
	}

	public String getContents() {
		return contents;
	}

	public void setContents(String contents) {
		this.contents = contents;
	}

	public List getComments() {
		return comments;
	}

	public void setComments(List comments) {
		this.comments = comments;
	}


}

评论实体:

package com.ly.pojo;

public class Comment {

	private String cid;
	private String commenter;
	private String comtents;
	private String addTime;

	public String getCid() {
		return cid;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

	public String getCommenter() {
		return commenter;
	}

	public void setCommenter(String commenter) {
		this.commenter = commenter;
	}

	public String getComtents() {
		return comtents;
	}

	public void setComtents(String comtents) {
		this.comtents = comtents;
	}

	public String getAddTime() {
		return addTime;
	}

	public void setAddTime(String addTime) {
		this.addTime = addTime;
	}

}

Dao实体如下:

package com.ly.dao;

import java.util.List;
import java.util.Map;

import com.ly.pojo.Content;

public interface ContentDao {

	public int save(Map map);

	public Content find(Map map);

	public List findList(Map map);
}
package com.ly.dao;

import java.util.List;
import java.util.Map;

import com.ly.pojo.Comment;
import com.ly.pojo.Content;

public interface CommentDao {

	public int save(Map map);

	public Comment find(Map map);

	public List findList(Map map);
}

mybatis mapper文件:

 






	


	
		
			select count(1) from content where `cid` = #{cid}
		
		
			update comment
			
				`commenter` = #{commenter, jdbcType=VARCHAR},
				`comtents` = #{comtents,jdbcType=VARCHAR},
				contentId = #{contentId,jdbcType=VARCHAR},
			
			where `cid` = #{cid}
		
		
			insert into comment
			(
			`cid`
			`addTime`
			`commenter`
			`comtents`
			`contentId`
			)
			values
			(
			#{cid, jdbcType=VARCHAR},
			#{addTime, jdbcType=VARCHAR},
			#{commenter,jdbcType=VARCHAR},
			#{comtents,jdbcType=VARCHAR},
			#{contentId,jdbcType=VARCHAR}
			)
		
	

 

 







	
	
	
	
	
	
		
		
		
		
		
                    

你可能感兴趣的:(mybatis)