First, the SQL statement is read by using the mapping configuration file
the file is iuserdao.xml, as follows:
package com.wwh.dao;
import com.wwh.domain.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* Created By DELL on 2021/10/21-17:41
* Persistence layer interface for users
*/
public interface IUserDao {
/**
* Query all operations
* @return
*/
@Select("select * from user")
List<User> findAll();
}
The main profile is:
```css
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--The main configuration file for mybatis - >
<configuration>
<! --configuration-environments-->
<environments default="mysql">
<! --configure the type of mysql -->
<environment id="mysql">
<! --configure the type of transaction -->
<transactionManager type="JDBC"></transactionManager>
<! -- Configure data source (connection pool) -->
<dataSource type="POOLED">
<! -- Configure the four basic information for connecting to the database -- >
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/eesy"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<! -- Specify the location of the mapping profile, the mapping profile refers to a separate profile for each dao
If the configuration is done with annotations, the class attribute should be used here to specify the fully qualified class name of the annotated dao
-->
<mappers>
<mapper resource="com/wwh/dao/IUserDao.xml"/>
</mappers>
</configuration>
Then, the SQL is read by annotation, and the main configuration file is changed to:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--The main configuration file for mybatis - >
<configuration>
<! --configuration-environments-->
<environments default="mysql">
<! --configure the type of mysql -->
<environment id="mysql">
<! --configure the type of transaction -->
<transactionManager type="JDBC"></transactionManager>
<! -- Configure data source (connection pool) -->
<dataSource type="POOLED">
<! -- Configure the four basic information for connecting to the database -- >
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/eesy"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<! -- Specify the location of the mapping profile, the mapping profile refers to a separate profile for each dao
If the configuration is done with annotations, the class attribute should be used here to specify the fully qualified class name of the annotated dao
-->
<mappers>
<mapper class="com.wwh.dao.IUserDao"/>
</mappers>
</configuration>
The notes are:
package com.wwh.dao;
import com.wwh.domain.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* Created By DELL on 2021/10/21-17:41
* Persistence layer interface for users
*/
public interface IUserDao {
/**
* Query all operations
* @return
*/
@Select("select * from user")
List<User> findAll();
}
When running the mapping configuration file discovery program, it can run through, but when running the annotation program, it is found that an error is reported:
### Error building SqlSession.
### The error may exist in com/wwh/dao/IUserDao.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.wwh.dao.IUserDao.findAll
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:64)
at com.wwh.test.MybatisTest.main(MybatisTest.java:27)
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.wwh.dao.IUserDao.findAll
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:121)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:99)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:78)
... 2 more
Caused by: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.wwh.dao.IUserDao.findAll
at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:872)
at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:844)
at org.apache.ibatis.session.Configuration.addMappedStatement(Configuration.java:668)
at org.apache.ibatis.builder.MapperBuilderAssistant.addMappedStatement(MapperBuilderAssistant.java:302)
at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.parseStatement(MapperAnnotationBuilder.java:351)
at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.parse(MapperAnnotationBuilder.java:134)
at org.apache.ibatis.binding.MapperRegistry.addMapper(MapperRegistry.java:72)
at org.apache.ibatis.session.Configuration.addMapper(Configuration.java:741)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:381)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:119)
... 4 more
Process finished with exit code 1
Viewing the error information, we can see that the reason is that your iuserdao.xml has not been deleted. After the annotation method is adopted, the relevant mapping configuration file has no effect. If it is not deleted, an error will be reported (even if it is marked as excluded)
after deletion, you can run through:
Read More:
- [Solved] org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession…
- [Solved] ### Error building SqlSession. ### The error may exist in com/atguigu/dao/SysUserMapper.xml ### Caus
- [Solved] Error building SqlSession.The error may exist in XXXXXMapper/xml
- Build a mybatis and it will appear session.selectOne Method error
- [Solved] Mybatis Error: Could not find resource mybatis-conf.xml
- [Solved] mybatis Error querying database. Cause: java.sql.SQLException: The server time zone value
- [Solved] Mybatis.generator error: Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2
- [Solved] mybatis plus Insert Error: mybatis plus Error setting null for parameter #1 with JdbcType OTHER
- Mybatis Error: Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance.
- Mybatis Error: The error may exist in xxxxMapper.xml [How to Solve]
- [Solved] Tk-Mybatis Error: tk.mybatis.mapper.MapperException:
- [Solved] Mybatis Error: Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance.
- [Solved] mybatis Error: Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance.
- How to Solve SQL comments error in the mybatis query
- Xdoc generates API documents based on Java annotations
- [Solved] Could not find resource COM / atguigu / Dao / studentdao.xm, the mapper file for storing SQL statements could not be found and an error occurred
- Springboot reports an error After adding the import annotation: application failed to start
- [Solved] Solr8 establishes the cluster node as active, but the query reports an error
- After asynchronous file import and springboot multipartfile upload, the @async asynchronous processing reports an error: nosuchfileexception
- The Java class generated by protocol reports an error: cannot access