Mybatis的Xml映射文件中,不同Xml映射文件,id是否可以重复?

Mybatis的Xml映射文件中,不同Xml映射文件,id是否可以重复?

不同的Xml映射文件,如果配了namespace,那么id可以重复,并且调用的时候要写全namespace.id;如果没有配置namespace,那么id不能重复.

EmployeeMapper.xml跟EmployeeMapper2.xml中的namespace是不一样的,但是里面的id是一样的.
EmployeeMapper.xml


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="test">
<select id="selectAllEmployee" resultType="Employee">
/*查询全部*/
select id,name,age,birthday,salary from t_employee
select>
mapper>

EmployeeMapper2.xml


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="test2">
<select id="selectAllEmployee" resultType="Employee">
/*查询全部*/
select id,name,age,birthday,salary from t_employee
select>
mapper>

MabatisTest.java

package com.xiongluoluo.test;

import com.xiongluoluo.bean.Employee;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by Administrator on 2019/12/21 0021.
 */
public class MybatisTest {
    @Test
    public void testSelectAllEmployee(){
        String resource = "mybatis-config.xml";
        try {
            InputStream is = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            List<Employee> list = sqlSession.selectList("test.selectAllEmployee");

           //如果这里用的是namespace+id那么就不会报错.
           //如果这里直接写的id那么就会报错.因为在不同的映射文件里有相同的id.

            for(Employee employee : list){
                System.out.println(employee);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

你可能感兴趣的:(Java,xml,java,spring,mybatis)