秋日的晚霞 2021-07-04 17:36 采纳率: 94.4%
浏览 49
已采纳

Mybatis报错 找不到映射文件

img

1.BooksMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.vx202158.dao.BooksMapper">

    <!--  添加  -->
    <insert id="addBooks" parameterType="com.vx202158.pojo.Books">
        insert into books
        values (#{bookId}, #{bookName}, #{bookCounts}, #{datail});
    </insert>

    <!--删除-->
    <delete id="delBooks" parameterType="int">
        delete
        from books
        where bookId = #{bookId}
    </delete>

    <!--   修改 -->
    <update id="updateBooks" parameterType="com.vx202158.pojo.Books">
        update books
        set bookName=#{bookName},
            bookCounts= #{bookCounts},
            datail= #{datail}
        where bookId = #{bookId}
    </update>
    <!--   查询 -->
    <select id="queryBooks" parameterType="int" resultType="com.vx202158.pojo.Books">
        select * from books where bookId = #{bookId}
    </select>

    <!--    查询所有-->
    <select id="queryAllBooks" resultType="com.vx202158.pojo.Books">
        select * from books
    </select>
</mapper>

2.BooksMapper

package com.vx202158.dao;

import com.vx202158.pojo.Books;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface BooksMapper {
    //添加一本书
    int addBooks(Books books);

    //删除一本书
    int delBooks(@Param("bookId") int bookId);

    //修改一本书
    int updateBooks(Books books);

    //查询一本书
    Books queryBooks(@Param("bookId") int bookId);

    //查询所有书
    List<Books> queryAllBooks();
}

3.mybatis-config.xml

<?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>

<typeAliases>
    <package name="com.vx202158.pojo"/>
</typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/ssmbuild?serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>

    <mappers><mapper resource="com/vx202158/dao/BooksMapper.xml"></mapper></mappers>

</configuration>


4.测试类

 @Test
    public void test2() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");

        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);

        SqlSession sqlSession = build.openSession(true);

        List<Object> objects = sqlSession.selectList("sqlSession");

        System.out.println("objects = " + objects);
    }
  • 写回答

1条回答 默认 最新

  • 404警告 2021-07-04 18:26
    关注

    再pom.xml文件下添加如下代码:
    img

    <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
                <resource>
                    <directory>src/main/resources</directory>
                </resource>
            </resources>
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Matlab求解微分方程,如何用fish2d进行预优?