Mybatis [2] – multiple mapper files and the role of namespace

Time:2021-5-7

The code is placed directly in GitHub warehouse【 https://github.com/Damaer/Myb… 】It can be run directly, so it doesn’t take up space.

Mybatis [2] - multiple mapper files and the role of namespace

Multiple mapper files and the role of namespace

How to deal with multiple mapper files and what is the use of namespace
First, let’s look at the create database statement

#Create database
CREATE DATABASE `test` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
#Create data table
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 talk about the running process of mybatis again: first, we pass theResources.getResourceAsStream("mybatis.xml")Read tomybatis.xmlIn this file, the configuration of the whole project is related to the database, such as the running database environment (which database to connect to, the address of the database server, user name, password), or the configuration of external configuration files,Most importantly, this file registers the mapper fileThen we useSqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);When I was young,sqlSessionFactoryGo back and read itmybatis.xmlIt will read the configuration files, and will get the information read by each configuration file one by oneMapperMapping files when we useopenSession()GetsqlSessionFor example, we usesqlSession.insert("insertStudent",student);, you will find the SQL configuration statements 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>

findidThe same is OK, so many people will say, since the distinction is usedidWhat’s the use of the namespace attribute in my mapper file?

When we have two or more of the sameidIf there is only one, we must use namespace to distinguishmapper.xmlDocuments, then wenamespaceYou can write anything. When using it, you only need to:sqlSession.insert("insertStudent",student);If our ID is the same, we need to use:sqlSession.insert("mapper1.insertStudent",student);Add in frontnamspace. Otherwise, the following error will appear, prompting us to use the full name includingnamespaceOr redefine oneid
In general, eitheridIt’s not the same. You can use it directly, oridIt’s the same, butnamespaceIt’s not the same. Add it when you use itnamespacedistinguish. Otherwise, the following error will be reported:
Mybatis [2] - multiple mapper files and the role of namespace

There are multiple mapper files. You need to register two files in mybatis.xml file:

<!--  Register mapping file -- >
          <mappers>
              <mapper resource="mapper/mapper1.xml"/>
            <mapper resource="mapper/mapper2.xml"/>
          </mappers>

When using, add the namespace:
Mybatis [2] - multiple mapper files and the role of namespace

This article is only on behalf of their own (this rookie) learning accumulation records, or learning notes, if there is infringement, please contact the author to delete. No one is perfect, so is the article. The style of writing is immature. If you don’t have talent, don’t spray. If you have any mistakes, I hope you can point them out. Thank you very much~

The road of technology is not temporary, high mountains and long rivers, even if it is slow, it will not stop.
Official account: Qinhai grocery store