晚场电影 2021-01-22 02:07 采纳率: 66.7%
浏览 222
已采纳

mybatis报错:Invalid bound statement (not found)

我是一个初学者,学习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条回答 默认 最新

  • sh_c_1314 2021-01-22 08:17
    关注

    通过查看你的异常:BindingException: Invalid bound statement (not found): test.Bookmapper.add

    你的接口方法和xml文件中的add方法映射存在问题:

    <insert id="add" parameterType="test.Book1" databaseId="mysql">

    只需要把 databaseId="mysql" 删除就可以了,配置多数据库时用的,这还用不到.

    改成这样就可以了:

    <insert id="add" parameterType="test.Book1" >

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 手写签名不能上传的问题
  • ¥30 linux odbc怎么添加gbase数据库
  • ¥20 电脑开机黑屏,只有一个鼠标,联想zj者y7000
  • ¥20 DXSDK_jun10
  • ¥20 请问这种量表怎么用spss量化分析(作为中介模型的因变量
  • ¥55 AD844 howland电流源如何驱动大额负载
  • ¥15 C++ /QT 内存权限的判断函数列举
  • ¥15 深度学习GFnet理解问题
  • ¥15 单细胞小提琴堆叠图代码
  • ¥50 升级strust2版本到2.3.15.1后使用ognl3.0.6.jar windows环境中没有问题,但部署到linux环境报错