resources包下xml:(必须和Mapper文件同包同名,创建时不能用.分割,换成/)
<?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"> //namespace属性与mapper接口全限定名一致 <mapper namespace="com.itheima.mapper.EmpMapper"> //id属性与mapper接口中的方法名一致,返回数据类型也要一致 <select id="list" resultType="com.itheima.pojo.Emp"> select * from emp where name like concat('%',#{name},'%') and gender=#{gender} and entrydate between #{begin} and #{end} order by update_time desc </select> </mapper>
//前四行去入门_MyBatis中文网找
==================================================================================================================================================
接口mapper文件:
package com.itheima.mapper; import com.itheima.pojo.Emp; import org.apache.ibatis.annotations.*; import java.time.LocalDate; import java.util.List; @Mapper public interface EmpMapper {
public List<Emp> list(String name, Short gender, LocalDate begin,LocalDate end); }
==================================================================================================================================================
SpringbootMybatisCrudApplication启动类:
package com.itheima; import com.itheima.mapper.EmpMapper; import com.itheima.pojo.Emp; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.List; @SpringBootTest class SpringbootMybatisCrudApplicationTests { @Autowired private EmpMapper empMapper; @Test public void testList(){ List<Emp> empList= empMapper.list("张",(short)1,LocalDate.of(2010,1,1),LocalDate.of(2020,1,1)); System.out.println(empList); } }
==================================================================================================================================================
出现org.apache.ibatis.binding.BindingException: Invalid bound statement (not fou****)问题仔细检查xml是否与mapper接口同包同名,namespace属性与mapper接口全限定名是否一致,id属性与mapper接口中的方法名一致,返回数据类型也要一致。
或者查看target是否编译了xml文件
如果没有,在pom.xml添加
<!-- 如果不添加此节点src/main/java目录下的所有配置文件都会被漏掉。 --> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> <include>**/*.ini</include> </includes> </resource> </resources>