Problem: BindingException exception, that is, there is no binding between Mapper and XML, that is, the Mapper interface does not find the XML implementation class. The main reason is that the binding relationship is not configured in Mybatis Configuration. Here we provide the binding relationship between two storage locations
org.apache.ibatis.binding.BindingException: Type interface dao.OrderMapper is not known to the MapperRegistry. at org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:47) at org.apache.ibatis.session.Configuration.getMapper(Configuration.java:845) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at
Configuration configures the binding relationship, mainly by specifying the correct XML path
<?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"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/gwmdb?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="itsme999"/> </dataSource> </environment> </environments> <!--appoint Mapper Implementation class of interface xml file--> <mappers> <mapper resource="mapper/OrderMapper.xml"/> </mappers> </configuration>
Entity class
public class Order { private String user_id; private String order_content; private String order_id; private Date create_time; public String getUser_id() { return user_id; }
xml implementation class of mapper interface
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="dao.OrderMapper"> <select id="getOrderList" resultType="entity.Order"> select * from ksd_order where order_id = 1 </select> </mapper>
mybatis configuration profile
<?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"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/gwmdb?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="itsme999"/> </dataSource> </environment> </environments> <mappers> <mapper resource="gmaper/OrderMapper.xml"/> </mappers> </configuration>
Test class
public class DaoTest { @Test public void test1(){ SqlSession sqlSession = MyBatisUtil.getSqlSession(); OrderMapper mapper = sqlSession.getMapper(OrderMapper.class); List<Order> orderList = mapper.getOrderList(); orderList.forEach(e->{ System.out.println(e); }); } }
Operation results:
Why do you report an error? Here, let's take a look at our maven project structure, as shown in the figure
From the project structure diagram, we clearly have the gmaper folder, but the target folder is not generated after compilation. Why?
This is because we put the configuration files of non java files under the src/main/java directory. maven has a default resource filtering configuration for non java files under the src/main/java directory. By default, it is turned on. Here, we just need to turn it off without filtering, just add a section of configuration:
Note: in this configuration, I added an additional file resource. All txt files will not be filtered, which can be ignored
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/*.txt</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build>
After adding this configuration, our configuration file will be clearly seen in the compiled target directory. The classes folder in the target actually refers to our classpath, which is also our root directory/
At this point, let's take a look at our target directory. First, we have the gmaper folder and the XML file
At this time, our test class query results also display the results normally:
In fact, in most cases, our configuration files are placed under src/main/resource, which is actually easier. maven will finally compile the files here and put them together with the classes file, that is, they will be placed under the classpath with our code, which naturally does not need to manage maven resource filtering
As shown in the figure below:
We can clearly see that the files under src/mian/resource are compiled under classes, so we can also find the results normally. Here, we can quickly solve the binding exception problem raised at the beginning
public static void main(String[] args) throws Exception{ ClassLoader classLoader = TestDom.class.getClassLoader(); URL resource = classLoader.getResource("test.xml"); String path = resource.getPath(); System.out.println(path); InputStream resourceAsStream = classLoader.getResourceAsStream("test.xml"); }
Read maven project resource file, see How to read the resource file in the resource directory of maven project?