Multiple Mapper files and namespace functions

Create a namespace from mapper; create a namespace from mapper;

Create databaseCREATE DATABASE `test` DEFAULT CHARACTER SET utf8 COLLATEutf8_general_ci; Create tableCREATE 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;
Copy the code

Here we must restate the general operation process of Mybatis: First we through Resources. GetResourceAsStream (” mybatis. XML “) reads to mybatis. The XML the file, the file is the whole project and the database configuration inside related configuration, such as the database environment of the running time (which a database connection, Database server address, username, password), or configure external configuration files, and most importantly, this file registers the mapper file, SqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream); SqlSessionFactory reads the configuration file from mybatis. XML, and obtains the Mapper mapping file from each configuration file. When we use openSession() to obtain the sqlSession instance, For example, we use sqlSession. Insert (“insertStudent”,student); Mapper = mapper = mapper = mapper = mapper

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

Mapper = mapper = mapper = mapper = mapper = mapper = mapper = mapper = mapper

When we have two or more of the same ID, we must use namespace to distinguish. If there is only one mapper. XML file, then we can write anything to the namespace. sqlSession.insert(“insertStudent”,student); Insert (” mapper1.insertstudent “,student); Add namspace in front of it. Otherwise, the following error prompts us to use the full name to include namespace, or to redefine an ID. In general, either the ids are different and the namespace can be used directly, or the IDS are the same but the namespace is different. Otherwise, the following error will be reported:

Mybatis. XML file, you need to register two files:

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

Add namespace when using:

This article only represents their own (this rookie) learning accumulation record, or learning notes, if there is infringement, please contact the author to delete. No one is perfect, the article is the same, the writing style is immature, I am not talented, do not spray, if there is a mistake, also hope to point out, grateful ~

The road of technology is not at that time, the mountain is high and the water is long, even if it is slow, and not stop. Public account: Qinhuai Grocery store