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.xml
this 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, sqlSessionFactory
go back mybatis.xml
and read the configuration file read inside , And will obtain the Mapper
mapping file read by each configuration file one by one . When we use the openSession()
obtained sqlSession
instance, 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>
id
Just 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 onemapper.xml
file, then we cannamespace
write 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 frontnamspace
. Otherwise, the following error will appear, prompting us to use the full name to includenamespace
, or to redefine oneid
.
In general, eitherid
they are different and can be used directly, orid
they are the same, but they arenamespace
not the same, andnamespace
differentiate them when using them . Otherwise, the following error will be reported:
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:
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