[Mybatis usage] Examples of using one-to-one, one-to-many association and collection in the Mybatis framework

Contents of this article

1. Association label (one to one)

Two, collection label (one-to-many)


1. Association label (one to one)

Association is usually used to map a one-to-one relationship. For example, there is a class User, and the corresponding entity class is as follows: (getter and setter methods are omitted)

@Data
public class User {
    /**
     * 主键
     */
    private String id;
    /**
     * 用户姓名
     */
    private String userName;
}

There is a class Article, and the corresponding entity classes are as follows:

@Data
public class Article {
    /**
     * 主键
     */
    private String id;
    /**
     * 文章标题
     */
    private String articleTitle;
    /**
     * 文章内容
     */
    private String articleContent;
}

If I want to query a user and find an article written by him, how to write it? In fact, you can add an attribute Article class to the User class:

@Data
public class User {
    /**
     * 主键
     */
    private String id;
    /**
     * 用户姓名
     */
    private String userName;
    /**
     * 类Article
     */
    private Article article;
}

mapper.xml I configure it like this in UserMapper.xml of the user class

<mapper namespace="com.uiot.practice.mysqltest.dao.UserMapper">
	<resultMap type="com.uiot.practice.mysqltest.entity.Article" id="articleMap">
		<id column="id" property="id" jdbcType="VARCHAR" javaType="java.lang.String"/>
		<result column="userName" property="userName" jdbcType="VARCHAR" javaType="java.lang.String"/>
		<!--这里把user的id传过去,com.uiot.practice.mysqltest为命名空间-->
		<association property="article" column="id" select="com.uiot.practice.mysqltest.selectArticleByUserId" />
	</resultMap>
</mapper>

At the same time, the ArticleMapper.xml corresponding to my article is written like this:

<mapper namespace="com.uiot.practice.mysqltest.dao.ArticleMapper">
    <resultMap type="com.uiot.practice.mysqltest.entity.Article" id="ArticleResultMap">
        <id column="id" property="id" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <result column="articleTitle" property="articleTitle" jdbcType="VARCHAR" javaType="java.lang.String"/>
        <result column="articleContent" property="articleContent" jdbcType="VARCHAR" javaType="java.lang.String"/>
    </resultMap>

    <select id="selectArticleByUserId" parameterType="java.lang.String" resultMap="ArticleResultMap">
        select * from tb_article where userId=#{userId}
    </select>
</mapper>

Two, collection label (one-to-many)

One-to-many, collection, understand one-to-one, one-to-many is easy to understand

@Data
public class User {
    /**
     * 主键
     */
    private String id;
    /**
     * 用户姓名
     */
    private String userName;
    /**
     * 类Article
     */
    private List<Article> articleList;
}

UserMapper.xml is configured like this

<mapper namespace="com.uiot.practice.mysqltest.dao.UserMapper">
	<resultMap type="com.uiot.practice.mysqltest.entity.User" id="articleMap">
		<id column="id" property="id" jdbcType="VARCHAR" javaType="java.lang.String"/>
		<result column="userName" property="userName" jdbcType="VARCHAR" javaType="java.lang.String"/>
		<!--这里把user的id传过去,com.uiot.practice.mysqltest为命名空间-->
		<collection property="articleList" column="id" select="com.uiot.practice.mysqltest.selectArticleByUserId"/>
	</resultMap>
	<!--以下省略,类同,Mybatis会把结果封装成List类型。-->
</mapper>

 

end!

Guess you like

Origin blog.csdn.net/weixin_44299027/article/details/112919200