Mybatis [2]-multiple mapper files and namespace function

pexels-Johann-larger-1435075

Multiple mapper files and namespace function

What to do if there are multiple mapper files, and what is the namespace for?
First, let's look at the creation of a database statement:

#创建数据库
CREATE DATABASE `test` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
#创建数据表
CREATE TABLE `student` ( `id` INT NOT NULL AUTO_INCREMENT , `name` VARCHAR(20) NOT NULL ,
`age` INT NOT NULL , `score` DOUBLE NOT NULL , PRIMARY KEY (`id`)) ENGINE = MyISAM;

Here we have to re-explain the running process of Mybatis: First, we Resources.getResourceAsStream("mybatis.xml")read mybatis.xmlthis file. The configuration in this file is the configuration related to the entire project and the database, such as the database environment when it is running (which database to connect to, The address, user name, password of the database server), or configure an external configuration file, etc. The most important thing is that this file is registered with a mapping file (mapper file) , then when we use SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);it, sqlSessionFactorygo back mybatis.xmland read the configuration file read inside , And will obtain the Mappermapping file read by each configuration file one by one . When we use the openSession()obtained sqlSessioninstance, for example , when we use it, we sqlSession.insert("insertStudent",student);will look for the sql configuration statement in each mapper, which is similar to the following :

<mapper namespace="mapper1">
    <insert id="insertStudent" parameterType="bean.Student">
        insert into student(name,age,score) values(#{name},#{age},#{score})
    </insert>
</mapper>

idJust find the same one, so many people will say, since the distinction is used id, what is the namespace attribute in my mapper file for?

When we have two or more of the same id, we must use namespace to distinguish, if there is only one mapper.xmlfile, then we can namespacewrite anything, when using, only need: sqlSession.insert("insertStudent",student);it is fine, if our ids are the same , Then we need to use: sqlSession.insert("mapper1.insertStudent",student);add in front namspace. Otherwise, the following error will appear, prompting us to use the full name to include namespace, or to redefine one id.
In general, either idthey are different and can be used directly, or idthey are the same, but they are namespacenot the same, and namespacedifferentiate them when using them . Otherwise, the following error will be reported:
Mybatis [2]-multiple mapper files and namespace function

For multiple mapper files, two files need to be registered in the mybatis.xml file:

        <!-- 注册映射文件 -->
        <mappers>
            <mapper resource="mapper/mapper1.xml"/>
            <mapper resource="mapper/mapper2.xml"/>
        </mappers>

Add namespace when using:
Mybatis [2]-multiple mapper files and namespace function

This article only represents my (the rookie) study accumulation record, or study notes, if there is any infringement, please contact the author to delete it. No one is perfect, so is the article, the writing is immature, not good at the next, do not spray, if there are mistakes, I hope to point out, thank you for it~

The road to technology is not at a time, the mountains are high and the rivers are long, even if it is slow, it will not stop.
Public number: Qinhuai grocery store

Guess you like

Origin blog.51cto.com/13604316/2668459