1、不同 Mapper XML 文件中 id 是否可以相同?

  • 新版本 Mapper XML mapper 标签的 namespace 参数值不能为空

  • 两个 Mapper XML mapper 标签的 namespace 参数值相同,id 不可以相同。否则,提示异常 Mapped Statements collection already contains value

  • 两个 Mapper XML mapper 标签的 namespace 参数值不同,id 可以相同

  • 从源码实现上看,namespace.id 是 MappedStatement 对象的 id 属性;MappedStatement 对象的 id 属性作为 key,MappedStatement 对象作为 value 保存在 Configuration 对象的 mappedStatements Map 中,即 namespace.id 是方法对应 SQL 的唯一标识


2、为什么说 MyBatis 是半自动 ORM?

说 MyBatis 是 半自动 ORM 最主要的一个原因是,它需要在 XML 或者注解里通过手动或插件生成 SQL,才能完成 SQL 执行结果与对象映射绑定。


3、MyBatis 如何进行 1对1 和 1对多 的关联查询?

  • 在 Mapper xml 中的

    标签里使用可以完成 1对1 关联查询
  • 在 Mapper xml 中的

    标签里使用可以完成 1对多 关联查询
//sql
create table user (
id int primary key,
name varchar(400
)
)
;
insert user info VALUES(1'ConstXiong1');

create table info (
user_id int primary key,
name varchar(400
)
)
;
insert into info VALUES(1'大熊');

create table article (
user_id int,
title varchar(400
)
)
;
insert into article VALUES(1'文章1');
insert into article VALUES(1'文章2');




//Mapper xml
<!-- 11 -->
<select id="selectUserWithInfo" resultMap="UserWithInfo">
    select user.id, user.name, info.user_id, info.name as info_name from user,info where user.id = info.user_id
</select>

<!-- 1对多 -->
<select id="selectUserWithArticles" resultMap="UserWithArticles">
    select user.id, user.name, article.user_id, article.title from user,article where user.id = article.user_id
</select>

<resultMap id="UserWithInfo" type="constxiong.po.User">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <!-- 11 -->
    <association property="info" javaType="constxiong.po.Info">
        <id property="userId" column="user_id"/>
        <result property="name" column="info_name"/>
    </association>
</resultMap>

<resultMap id="UserWithArticles" type="constxiong.po.User">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <!-- 1对多 -->
    <collection property="articles" ofType="constxiong.po.Article">
        <result property="userId" column="user_id"/>
        <result property="title" column="title"/>
    </collection>
</resultMap>

//User.java
/**
 * 用户表模型
 */

public class User {
    private Integer id;

    private String name;

    private String mc;

    private Info info;

    private List<Article> articles;

    public User() {
    }

    public User(Integer id, String name{
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id{
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name{
        this.name = name;
    }

    public String getMc() {
        return mc;
    }

    public void setMc(String mc{
        this.mc = mc;
    }

    public Info getInfo() {
        return info;
    }

    public void setInfo(Info info{
        this.info = info;
    }

    public List<Article> getArticles() {
        return articles;
    }

    public void setArticles(List<Article> articles{
        this.articles = articles;
    }

    @Override
    public String toString() 
{
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", mc='" + mc + '\'' +
                ", info=" + info +
                ", articles=" + articles +
                '}';
    }
}


//测试代码
System.out.println("------ selectUserWithInfo ------");
user = userMapper.selectUserWithInfo();
System.out.println(user);

System.out.println("------ selectUserWithArticles ------");
user = userMapper.selectUserWithArticles();
System.out.println(user);

//打印
------ selectUserWithInfo ------
User{id=1, name='ConstXiong1', mc='null', info=Info{userId=1, name=大熊}, articles=null}
------ selectUserWithArticles ------
User{id=1, name='ConstXiong1', mc='null', info=null, articles=[Article{userId=1, title='文章1'}, Article{userId=1, title='文章2'}]}


完整 Demo:

https://javanav.com/val/a9fe4555c1614b40b0da07afeabf2a66.html



4、什么是 MyBatis 的接口绑定?有哪些实现方式?

接口绑定就是把接口里的方法与对应执行的 SQL 进行绑定,以及 SQL 执行的结果与方法的返回值进行转换匹配。
方式:

  • 接口与对应 namespace 的 xml 进行绑定,接口方法名与 xml 中<select>、<delete>、<update>、<delete> 标签的 id 参数值进行绑定

  • 接口方法与方法上的 @Select 或 @Update 或 @Delete 或 @Insert 的注解及注解里 SQL 进行绑定