spring boot2.0+mybatis三表联查

明天计算机组成原理考试,然而还是想码一会儿
pom依赖不再列出,太长了
整体项目结构

  1. application.properties文件配置:

mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.config-location=classpath\:mybatis/sqlMapConfig.xml
  1. mybatis配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <!-- 对在此配置文件下的所有cache进行全局性开/关设置 true|false true -->
        <setting name="cacheEnabled" value="true" />
        <!-- 全局性设置懒加载。如果设为‘关',则所有相关联的都会被初始化加载。 -->
        <setting name="lazyLoadingEnabled" value="true" />
        <!-- 当设置为‘开’的时候,懒加载的对象可能被任何懒属性全部加载。否则,每个属性都按需加载。 -->
        <setting name="aggressiveLazyLoading" value="true" />
        <!-- 允许和不允许单条语句返回多个数据集(取决于驱动需求) -->
        <setting name="multipleResultSetsEnabled" value="true" />
        <!-- 使用列标签代替列名称。不用的驱动器有不同的作法。 -->
        <setting name="localCacheScope" value="STATEMENT" />
        <!-- 允许JDBC生成主键。需要驱动器支持.如果设为了true,这个设置将强制使用被生成的主键, 有一些驱动器不兼容不过仍然可以执行。 -->
        <setting name="useGeneratedKeys" value="true" />
        <!-- 指定MyBatis是否并且如何来自动映射数据表字段与对象的属性。PARTIAL将只自动映射简单的,NONE没有嵌套的结果。 FULL将自动映射所有复杂的结果。 -->
        <setting name="autoMappingBehavior" value="PARTIAL" />
        <!-- 配置和设定执行器,SIMPLE执行器执行其它语句。REUSE执行器可能重复使用preparedstatements语句,BATCH执行器可以重复执行语句和批量更新。 -->
        <setting name="defaultExecutorType" value="SIMPLE" />
        <!-- 设置一个时限,以决定让驱动器等待数据库回应的多长时间为超时. 正整数 -->
        <setting name="defaultStatementTimeout" value="5000" />
        <setting name="jdbcTypeForNull" value="OTHER"/>
        <setting name="logImpl" value="LOG4J"/>
    </settings>

</configuration>
  1. mapper:
<?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="com.hxz.mapper.ArticleCommentMapper">
	<resultMap id="articleCommentsWithUser" type="com.hxz.entity.ArticleComment">
		<id column="id" property="id" jdbcType="INTEGER" />
		<result column="commentUserId" property="commentUserId"
			jdbcType="INTEGER" />
		<result column="articleId" property="articleId" jdbcType="INTEGER" />
		<result column="content" property="content" jdbcType="VARCHAR" />
		<result column="createTime" property="createTime" jdbcType="TIMESTAMP" />
		<!-- property指的是在bean中字段名 ofType类的全定向名 -->
		<association property="user" javaType="com.hxz.entity.User">
			<result column="name" property="username" jdbcType="VARCHAR" />
		</association>
		<collection property="replyCommentList" ofType="com.hxz.entity.ReplyComment">
			<id column="id" property="id"></id>
			<result column="replyUserId" property="replyUserId" jdbcType="INTEGER" />
			<result column="repliedUserId" property="repliedUserId"
				jdbcType="INTEGER" />
			<result column="content" property="content" jdbcType="VARCHAR" />
			<result column="createTime" property="createTime" jdbcType="TIMESTAMP" />
		</collection>
	</resultMap>

	<sql id="Base_Column_List">
		id, commentUserId, articleId, content, createTime
	</sql>
	<!-- 添加一条评论 -->
	<insert id="addArticleComment" parameterType="com.hxz.entity.ArticleComment">
		insert into
		articlecomments
		(commentUserId, articleId,content, createTime)
		values
		(#{commentUserId},#{articleId},#{content},#{createTime})
	</insert>
	<!-- 根据文章ID查找评论 -->
	<select id="getArticleCommentsByArticleId" parameterType="java.lang.Integer"
		resultMap="articleCommentsWithUser">
		SELECT
		a.id,
		a.createTime,
		a.content,
		a.commentUserId,
		b.`name`,
		c.replyUserId,
		c.repliedUserId,
		c.createTime,
		c.content,
		commentId
		FROM
		( articlecomments a LEFT JOIN `user` b ON a.commentUserId = b.id )
		LEFT JOIN replycomment c ON c.articleId = a.articleId
		WHERE
		a.articleId = #{id}
	</select>
</mapper>
  1. list接口:
package com.hxz.mapper;

import java.util.List;

import com.hxz.entity.ReplyComment;

public interface ReplyCommentMapper {
	/**
	 * 回复评论
	 * @param replyComment
	 * @return
	 */
	public int addReplyCommentMapper(ReplyComment replyComment);
	/**
	 * 评论List集合
	 * @param id
	 * @return
	 */
	public List<ReplyComment> getReplyCommentsByCommentId(int id);

}

  1. 实体类
package com.hxz.entity;
import java.io.Serializable;
import java.util.List;

/**
 * Create by hxz He on 
 */
public class ArticleComment implements Serializable{
	
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Integer id;//主键id
    private int commentUserId;//创建该评论的用户id
    private User user;
    private int articleId;//评论的文章id
    private String content;//评论内容
    private String createTime;//评论的日期
    private List<ReplyComment> replyCommentList;
    public ArticleComment(){}
    public ArticleComment(int commentUserId,int articleId,String content){
        this.articleId=articleId;
        this.commentUserId=commentUserId;
        this.content=content;
    }
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public int getCommentUserId() {
		return commentUserId;
	}
	public void setCommentUserId(int commentUserId) {
		this.commentUserId = commentUserId;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public int getArticleId() {
		return articleId;
	}
	public void setArticleId(int articleId) {
		this.articleId = articleId;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getCreateTime() {
		return createTime;
	}
	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}
	public List<ReplyComment> getReplyCommentList() {
		return replyCommentList;
	}
	public void setReplyCommentList(List<ReplyComment> replyCommentList) {
		this.replyCommentList = replyCommentList;
	}
    
}

  1. controller部分:
package com.hxz.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hxz.entity.ArticleComment;
import com.hxz.entity.ReplyComment;
import com.hxz.service.ArticleCommentService;
import com.hxz.service.ReplyCommentService;
import com.hxz.util.StaticConfigParam;

import io.swagger.annotations.Api;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Create by Alden He on 2019/5/7
 */
@RestController
@EnableAutoConfiguration
@Api(description = "评论controller")
public class CommentRestController {
	@Autowired
	private ArticleCommentService articleCommentService;

	@Autowired
	private ReplyCommentService replyCommentService;

	/**
	 * 添加评论
	 * @param articleId
	 * @param content
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/comments/addComments")
	@ResponseBody
	public Map<String, String> addComments(@RequestParam(value = "articleId", required = true) int articleId,
			@RequestParam(value = "content", required = true) String content, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, String> result = new HashMap<>();
		int userId = 1;
		if (articleId > StaticConfigParam.ARTICLE_ID_BEGIN && content.length() <= StaticConfigParam.COMMENT_MAX_LENGTH
				&& content != "") {
			ArticleComment articleComment = new ArticleComment(userId, articleId, content);
			result.put("status", articleCommentService.addArticleComment(articleComment) + "");
			result.put("success", "1");
		} else {
			result.put("success", "0");
		}
		return result;
	}
	/**
	 * get文章评论
	 * @param articleId
	 * @return
	 */
	@RequestMapping(value = "/comments/getComments")
	@ResponseBody
	public List<ArticleComment> getCommentsByArticleId(
			@RequestParam(value = "articleId", required = true) int articleId) {
		if (articleId >= StaticConfigParam.ARTICLE_ID_BEGIN)
			return articleCommentService.getArticleCommentsByArticleId(articleId);
		else
			return null;
	}
	/**
	 * 回复评论
	 * @param articleId
	 * @param commentId
	 * @param repliedUserId
	 * @param content
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/comments/addReplyComment")
	@ResponseBody
	public Map<String, String> addReplyComment(@RequestParam(value = "articleId", required = true) int articleId,
			@RequestParam(value = "commentId", required = true) int commentId,
			@RequestParam(value = "repliedUserId", required = true) int repliedUserId,
			@RequestParam(value = "content", required = true) String content, HttpServletRequest request) {
		int replyUserId = 0;
		Map<String, String> result = new HashMap<>();
		if (articleId >= StaticConfigParam.ARTICLE_ID_BEGIN && commentId > 0
				&& repliedUserId >= StaticConfigParam.USER_ID_BEGIN && content != ""
				&& content.length() <= StaticConfigParam.COMMENT_MAX_LENGTH) {
			ReplyComment replyComment = new ReplyComment(articleId, commentId, replyUserId, repliedUserId, content);
			int status = replyCommentService.addReplyCommentMapper(replyComment);
			result.put("status", status + "");
			result.put("success", "1");
		} else {
			result.put("success", "0");
		}
		return result;
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值