mybatis之一对多
今天主要话题围绕这么几个方面?
一、mybatis之一对多在说一对多之前,顺便说一下一对一。 一对一,常见的例子,比如以常见的班级例子来说,一个班主任只属于一个班级(排除某个班主任能力超群可兼任多个班级). 例如: <?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,namespace的值习惯上设置成包名+sql映射文件名,这样保证了namespace的值是唯一的--> <mapper namespace=com.yc.mybatis.test.classMapper"> <!-- 方式一:嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集 封装联表查询的数据(去除重复的数据) select * from class c,teacher t where c.teacher_id=t.t_id and c.c_id=1 --> <select id=getClass" parameterType=int" resultMap=getClassMap"> where c.teacher_id = t.t_id and c.teacher_id=#{id} </select> <!-- resultMap:映射实体类和字段之间的一一对应的关系 --> <resultMap type=Classes" id="> <id property=id" column=c_id"/> <result property=namec_name"/> <association property=teacher" javaType=Teacher"> <id property=t_id"/> <result property=t_name"/> </association> </resultMap> <!-- 方式二:嵌套查询:通过执行另外一个SQL映射语句来返回预期的复杂类型 SELECT * FROM class WHERE c_id=1; SELECT * FROM teacher WHERE t_id=1 //1 是上一个查询得到的teacher_id的值 property:别名(属性名) column:列名 --> <!-- 把teacher的字段设置进去 --> <getClass1getClassMap1class where c_id=select> <resultMap type=teacher_id" select=getTeacher"/> </resultMap> <" resultType=select t_id id,t_name name from teacher where t_id =select> </mapper> 顺便对association标签的属性进行解释: 一对多,以我博客为例,比如今天我写的一个近期评论的接口就是一个一对多的体现(一个评论者可以对应多篇文章,相反,多篇文章也能对应一个评论者,从中可以体现一对多,多对一,甚至多对多的关系) 关于一对一、一对多或者多对多,可以参考Mybatis 一对一,一对多,多对一,多对多的理解 话不多说,看xml代码: <?xml version="?> <!DOCTYPE mapper PUBLIC " "> <mapper com.blog.springboot.dao.CommentsDao"> <!-- 通用查询映射结果 --> <resultMap id=BaseResultMap" type=com.blog.springboot.entity.Comments"> <id column=comment_ID" property=commentId" /> <result column=comment_post_IDcommentPostIdcomment_authorcommentAuthorcomment_author_emailcommentAuthorEmailcomment_author_urlcommentAuthorUrlcomment_author_IPcommentAuthorIpcomment_datecommentDatecomment_date_gmtcommentDateGmtcomment_contentcommentContentcomment_karmacommentKarmacomment_approvedcommentApprovedcomment_agentcommentAgentcomment_typecommentTypecomment_parentcommentParentuser_iduserId" /> <collection property=posts" ofType=Posts"> <result column=post_titlepostTitle"/> </collection> </resultMap> <!-- 通用查询结果列 --> <sql id=Base_Column_List"> comment_ID AS commentId,comment_post_ID AS commentPostId,comment_author AS commentAuthor,comment_author_email AS commentAuthorEmail,comment_author_url AS commentAuthorUrl,comment_author_IP AS commentAuthorIp,comment_date AS commentDate,comment_date_gmt AS commentDateGmt,comment_content AS commentContent,comment_karma AS commentKarma,comment_approved AS commentApproved,comment_agent AS commentAgent,comment_type AS commentType,comment_parent AS commentParent,user_id AS userId </sql> <recentComments SELECT comments.comment_author,posts.post_title FROM wp_comments AS comments LEFT JOIN wp_posts AS posts ON(comments.comment_post_ID=posts.ID) WHERE comments.comment_approved='0' AND posts.post_status=publish' ORDER BY comments.comment_date_gmt DESC LIMIT 0,5 </select> </mapper> 相关属性我就不做多的解释,关于MyBatis相关的教程,除了参考官网之外,还可以参考我的博客系列文章,地址为:https://www.cnblogs.com/youcong/category/1144041.html 关于ofType还是要说的,如果你的mybatis-config.xml或者是springboot中的application.yml或application.properties没有配置对应的别名,那么请将类的完整路径填写上去,假定我没有做出相关的配置的话,那么我需要这么写 ofType=”com.blog.springboot.entity.Posts”。 collection的property要包含在com.blog.springboot.entity.Comments类里面 我贴出我的Comments类,大家可以做一个参考: package com.blog.springboot.entity; import java.io.Serializable; import java.util.Date; import java.util.List; import com.baomidou.mybatisplus.activerecord.Model; import com.baomidou.mybatisplus.annotations.TableField; import com.baomidou.mybatisplus.annotations.TableId; import com.baomidou.mybatisplus.annotations.TableName; import com.baomidou.mybatisplus.enums.IdType; /** * <p> * * </p> * * @author youcong * @since 2019-02-12 */ @TableName(wp_comments") public class Comments extends Model<Comments> { private static final long serialVersionUID = 1L; @TableId(value = ",type = IdType.AUTO) private Long commentId; @TableField() Long commentPostId; @TableField( String commentAuthor; @TableField( String commentAuthorEmail; @TableField( String commentAuthorUrl; @TableField( String commentAuthorIp; @TableField( Date commentDate; @TableField( Date commentDateGmt; @TableField( String commentContent; @TableField( Integer commentKarma; @TableField( String commentApproved; @TableField( String commentAgent; @TableField( String commentType; @TableField( Long commentParent; @TableField( Long userId; @TableField(exist=falseprivate List<Posts> posts; public List<Posts> getPosts() { return posts; } void setPosts(List<Posts> posts) { this.posts =public Long getCommentId() { commentId; } void setCommentId(Long commentId) { this.commentId = Long getCommentPostId() { commentPostId; } setCommentPostId(Long commentPostId) { this.commentPostId = String getCommentAuthor() { commentAuthor; } setCommentAuthor(String commentAuthor) { this.commentAuthor = String getCommentAuthorEmail() { commentAuthorEmail; } setCommentAuthorEmail(String commentAuthorEmail) { this.commentAuthorEmail = String getCommentAuthorUrl() { commentAuthorUrl; } setCommentAuthorUrl(String commentAuthorUrl) { this.commentAuthorUrl = String getCommentAuthorIp() { commentAuthorIp; } setCommentAuthorIp(String commentAuthorIp) { this.commentAuthorIp = Date getCommentDate() { commentDate; } setCommentDate(Date commentDate) { this.commentDate = Date getCommentDateGmt() { commentDateGmt; } setCommentDateGmt(Date commentDateGmt) { this.commentDateGmt = String getCommentContent() { commentContent; } setCommentContent(String commentContent) { this.commentContent = Integer getCommentKarma() { commentKarma; } setCommentKarma(Integer commentKarma) { this.commentKarma = String getCommentApproved() { commentApproved; } setCommentApproved(String commentApproved) { this.commentApproved = String getCommentAgent() { commentAgent; } setCommentAgent(String commentAgent) { this.commentAgent = String getCommentType() { commentType; } setCommentType(String commentType) { this.commentType = Long getCommentParent() { commentParent; } setCommentParent(Long commentParent) { this.commentParent = Long getUserId() { userId; } setUserId(Long userId) { this.userId = userId; } @Override protected Serializable pkVal() { return this.commentId; } @Override String toString() { return Comments{" + ,commentId=" + commentId + " + commentPostId + " + commentAuthor + " + commentAuthorEmail + " + commentAuthorUrl + " + commentAuthorIp + " + commentDate + " + commentDateGmt + " + commentContent + " + commentKarma + " + commentApproved + " + commentAgent + " + commentType + " + commentParent + " + userId + }; } } 也许大家发现我的mybatis与你们的mybatis不一样,实际上我用的是mybatis-plus,mybatis-plus可以说跟mybatis几乎没有什么区别,我多次强调过,mybatis-plus是mybatis的增强版,意味着mybatis原有的功能,mybatis-plus可以毫无顾忌的拿来即用。 关于mybatis-plus的学习教程,感兴趣的朋友可以参考我的这篇博客(包含从入门到使用):https://www.cnblogs.com/youcong/category/1213059.html sql优化策略sql优化的策略有很多,大家可以参考如下: (1)任何地方都不要使用select?from table_name,请使用具体的字段列表代替”“ ,不要返回用不到的任何字段; 当然远远不止这么多,知识的海洋是无穷的,探索的乐趣亦如此。 关于sql优化思路,大家可以参考SQL优化思路大全 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |