leopard1214
leopard1214
2021-01-22 02:07

mybatis报错:Invalid bound statement (not found)

5
  • spring
  • mysql
  • intellij-idea

我是一个初学者,学习mybatis框架。我查询可以查得出结果,但是添加就不能出结果。

首先这是实体数据类

package test;

public class Book1 {
    private String name;
    private String author;

    public String getName() {
        return name;
    }

    public String getAuthor() {
        return author;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public Book1(String name, String author) {
        this.name = name;
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                '}';
    }

    public Book1() {
    }
}

然后是mapper接口

public interface Bookmapper {
    public List<Book1> select(String s);
    public void add(Book1 book1);
}

两个xml文件

<?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="test.Bookmapper">
    <select id="select" resultType="test.Book1" parameterType="String">
        select * from book where author = #{author}
    </select>
    <insert id="add" parameterType="test.Book1" databaseId="mysql">
        insert into book (name,author) values(#{name}, #{author})
    </insert>
</mapper>
<?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/class?verifyServerCertificate=false&amp;useSSL=false"/>
                <property name="username" value="root"/>
                <property name="password" value="weiziyao1214"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper.xml"/>
    </mappers>
</configuration>

这是测试类

public class Text {
    @Test
    public void test() throws IOException {
        Book1 b=new Book1("悉达多","黑塞");
        String resource = "mybatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =
                new SqlSessionFactoryBuilder().build(inputStream);

        try (SqlSession session = sqlSessionFactory.openSession()) {
            Bookmapper mapper = session.getMapper(Bookmapper.class);
            List<Book1> book = mapper.select("川端康成");
            System.out.println(book);
            mapper.add(b);
            session.commit();
        }

    }
}

输出结果如下

[Book{name='雪国', author='川端康成'}, Book{name='雪国', author='川端康成'}]

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): test.Bookmapper.add

奇怪的是,我查询单条结果的时候返回为空,当我把返回值变成结果集的时候返回成功。但是添加操作就是不成。

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答