日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

mybatis之一对多

發布時間:2025/4/16 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mybatis之一对多 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今天主要話題圍繞這么幾個方面?

  • mybatis一對多示例
  • sql優化策略

一、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">select * from class c, teacher t where c.teacher_id = t.t_id and c.teacher_id=#{id}</select><!-- resultMap:映射實體類和字段之間的一一對應的關系 --><resultMap type="Classes" id="getClassMap"><id property="id" column="c_id"/> <result property="name" column="c_name"/><association property="teacher" javaType="Teacher"> <id property="id" column="t_id"/><result property="name" column="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的字段設置進去 --><select id="getClass1" parameterType="int" resultMap="getClassMap1">select * from class where c_id=#{id}</select><resultMap type="Classes" id="getClassMap1"><id property="id" column="c_id"/> <result property="name" column="c_name"/><association property="teacher" column="teacher_id" select="getTeacher"/> </resultMap><select id="getTeacher" parameterType="int" resultType="Teacher">select t_id id,t_name name from teacher where t_id =#{id}</select> </mapper>

順便對association標簽的屬性進行解釋:
property:對象屬性名稱
javaType:對象屬性類型
column:所對應的外鍵字段名稱

一對多,以我博客為例,比如今天我寫的一個近期評論的接口就是一個一對多的體現(一個評論者可以對應多篇文章,相反,多篇文章也能對應一個評論者,從中可以體現一對多,多對一,甚至多對多的關系)

關于一對一、一對多或者多對多,可以參考Mybatis 一對一,一對多,多對一,多對多的理解

話不多說,看xml代碼:

<?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.blog.springboot.dao.CommentsDao"><!-- 通用查詢映射結果 --><resultMap id="BaseResultMap" type="com.blog.springboot.entity.Comments"><id column="comment_ID" property="commentId" /><result column="comment_post_ID" property="commentPostId" /><result column="comment_author" property="commentAuthor" /><result column="comment_author_email" property="commentAuthorEmail" /><result column="comment_author_url" property="commentAuthorUrl" /><result column="comment_author_IP" property="commentAuthorIp" /><result column="comment_date" property="commentDate" /><result column="comment_date_gmt" property="commentDateGmt" /><result column="comment_content" property="commentContent" /><result column="comment_karma" property="commentKarma" /><result column="comment_approved" property="commentApproved" /><result column="comment_agent" property="commentAgent" /><result column="comment_type" property="commentType" /><result column="comment_parent" property="commentParent" /><result column="user_id" property="userId" /><collection property="posts" ofType="Posts"><result column="post_title" property="postTitle"/></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><select id="recentComments" resultMap="BaseResultMap">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 = "comment_ID", type = IdType.AUTO)private Long commentId;@TableField("comment_post_ID")private Long commentPostId;@TableField("comment_author")private String commentAuthor;@TableField("comment_author_email")private String commentAuthorEmail;@TableField("comment_author_url")private String commentAuthorUrl;@TableField("comment_author_IP")private String commentAuthorIp;@TableField("comment_date")private Date commentDate;@TableField("comment_date_gmt")private Date commentDateGmt;@TableField("comment_content")private String commentContent;@TableField("comment_karma")private Integer commentKarma;@TableField("comment_approved")private String commentApproved;@TableField("comment_agent")private String commentAgent;@TableField("comment_type")private String commentType;@TableField("comment_parent")private Long commentParent;@TableField("user_id")private Long userId;@TableField(exist=false)private List<Posts> posts;public List<Posts> getPosts() {return posts;}public void setPosts(List<Posts> posts) {this.posts = posts;}public Long getCommentId() {return commentId;}public void setCommentId(Long commentId) {this.commentId = commentId;}public Long getCommentPostId() {return commentPostId;}public void setCommentPostId(Long commentPostId) {this.commentPostId = commentPostId;}public String getCommentAuthor() {return commentAuthor;}public void setCommentAuthor(String commentAuthor) {this.commentAuthor = commentAuthor;}public String getCommentAuthorEmail() {return commentAuthorEmail;}public void setCommentAuthorEmail(String commentAuthorEmail) {this.commentAuthorEmail = commentAuthorEmail;}public String getCommentAuthorUrl() {return commentAuthorUrl;}public void setCommentAuthorUrl(String commentAuthorUrl) {this.commentAuthorUrl = commentAuthorUrl;}public String getCommentAuthorIp() {return commentAuthorIp;}public void setCommentAuthorIp(String commentAuthorIp) {this.commentAuthorIp = commentAuthorIp;}public Date getCommentDate() {return commentDate;}public void setCommentDate(Date commentDate) {this.commentDate = commentDate;}public Date getCommentDateGmt() {return commentDateGmt;}public void setCommentDateGmt(Date commentDateGmt) {this.commentDateGmt = commentDateGmt;}public String getCommentContent() {return commentContent;}public void setCommentContent(String commentContent) {this.commentContent = commentContent;}public Integer getCommentKarma() {return commentKarma;}public void setCommentKarma(Integer commentKarma) {this.commentKarma = commentKarma;}public String getCommentApproved() {return commentApproved;}public void setCommentApproved(String commentApproved) {this.commentApproved = commentApproved;}public String getCommentAgent() {return commentAgent;}public void setCommentAgent(String commentAgent) {this.commentAgent = commentAgent;}public String getCommentType() {return commentType;}public void setCommentType(String commentType) {this.commentType = commentType;}public Long getCommentParent() {return commentParent;}public void setCommentParent(Long commentParent) {this.commentParent = commentParent;}public Long getUserId() {return userId;}public void setUserId(Long userId) {this.userId = userId;}@Overrideprotected Serializable pkVal() {return this.commentId;}@Overridepublic String toString() {return "Comments{" +", commentId=" + commentId +", commentPostId=" + commentPostId +", commentAuthor=" + commentAuthor +", commentAuthorEmail=" + commentAuthorEmail +", commentAuthorUrl=" + commentAuthorUrl +", commentAuthorIp=" + commentAuthorIp +", commentDate=" + commentDate +", commentDateGmt=" + commentDateGmt +", commentContent=" + commentContent +", commentKarma=" + commentKarma +", commentApproved=" + commentApproved +", commentAgent=" + commentAgent +", commentType=" + commentType +", commentParent=" + commentParent +", userId=" + 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,請使用具體的字段列表代替”“ ,不要返回用不到的任何字段;
(2)對查詢進行優化,應盡量避免全表掃描,首先應考慮在where及order by涉及的列建立索引;
(3)應盡量避免在where子句中使用or來連接條件,否則將導致引擎放棄使用索引而進行全表掃描;
(4)應盡量避免在where子句中使用!=或<>操作符,否則將導致引擎放棄使用索引而進行全表掃描;
(5)int和not in慎用,否則會導致全表掃描;
(6)應盡量避免在where子句中對字段進行表達式操作,這將導致引擎放棄使用索引而進行全表掃描;
(7)很多時候使用exists代替in是一個好的選擇;
(8)盡量使用數字型字段,若只含數值信息的字段設計為字符型,這將會降低查詢和連接的性能,并會增加存儲開銷,這是因為引擎在處理查詢和連接時會逐個比較字符串職工的每一個字符,而對于數字型而言只需要比較一次就夠了;
(9)盡可能使用varchar代替char,因為首先變長字段存儲空間小,可以節省存儲空間,其次對于查詢來說,在一個相對較小的字段內搜索效率顯然要高些;

當然遠遠不止這么多,知識的海洋是無窮的,探索的樂趣亦如此。

關于sql優化思路,大家可以參考SQL優化思路大全

轉載于:https://www.cnblogs.com/youcong/p/10503069.html

總結

以上是生活随笔為你收集整理的mybatis之一对多的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。