SpringBoot整合mybatis时提示not found,加resource却显示冲突

这个target里面没有xml文件,我在mapper目录下有个StudentMapper.xml文件

现在去访问显示

2021-05-26 12:38:32.421 ERROR 15712 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): cn.dwg.mapper.StudentMapper.selectByPrimaryKey] with root cause

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): cn.dwg.mapper.StudentMapper.selectByPrimaryKey
	at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235) ~[mybatis-3.5.6.jar:3.5.6]
	at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:53) ~[mybatis-3.5.6.jar:3.5.6]
	at org.apache.ibatis.binding.MapperProxy.lambda$cachedInvoker$0(MapperProxy.java:115) ~[mybatis-3.5.6.jar:3.5.6]

然后我去pom里面加了resource

clean之后再运行xml文件有了,但又报了这个错误

说已经存在了,我@mapper@controller什么的都加好的,时区也设置的没问题,这边不加没xml文件加了又说重复实在把我搞晕了,希望有大佬可以指点一下

首先具体原因就是你的studentMapper.xml出现了语法错误,可能情况有如下:

1.当同一个xml映射文件内存在两个相同的id(即两个sql语句的id相同)时会报此错

2.在mybatis的配置文件mybatis.xml内使用了<mapper/>标签加载xxxMapper.xml的映射文件报错,因为如果xxxMapper.xml与namespace的接口在同一路径下,就不需要在mybaits.xml中再进行配置了。  你使用的是spring-boot,可以排除这项。

3.parameterType中的问题。这里的类名如果找不到也会报这个错,比如你之前是将该类名写死在这里,之后由于重构将该类转移到其他包中,如果这里不修改也会报这个错。

4.还是parameterType中的问题,这次是关于自定义类的,当你使用基本类型的时候,比如int、string等,千万不要写错,比如写成strnig,咋一看看不出来,结果该问题就很难找

5.resultType的值与resultMap的id值相同的话会报错,可能是冲突了

6.这一点和上一点差不多,如果是自定义resultMap,如果返回类型写成resultType,也会报这个错

还望采纳~!

错误的内容应该是在你studentmapper.xml中,大概是你们有重复内容的东西,比如有两个BaseResultMap,可能Mybaits生成了两次,有了重复内容,最好检查一下studentmapper.xml,或者贴出来看看

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="cn.dwg.mapper.StudentMapper">
  <resultMap id="BaseResultMap" type="cn.dwg.model.Student">
    <!--id标签只能修改主键字段-->
    <!--result除了主键以外的字段-->
<!--    column数据库中的字段名称property映射对象的属性名称-->
<!--    jdbcType列中数据库中字段的类型(可以省略不写)-->
<!--    resultMap作用:-->
<!--    1.当数据库中字段名称与实体类对象的属性名不一致时,可以进行转换
        2.当前查询的结果没有对象一个表的时候,可以自定义一个结果集-->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="age" jdbcType="INTEGER" property="age" />
  </resultMap>
<!--  sql语句片段,将公共的部分抽取出来
      通过include引入
-->
  <sql id="Base_Column_List">
    id, name, age
  </sql>

  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from student
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from student
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="cn.dwg.model.Student">
    insert into student (id, name, age
      )
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}
      )
  </insert>
<!-- suffixOverrides去除多余逗号 -->
  <insert id="insertSelective" parameterType="cn.dwg.model.Student">
    insert into student
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="name != null">
        name,
      </if>
      <if test="age != null">
        age,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="age != null">
        #{age,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="cn.dwg.model.Student">
    update student
    <set>
      <if test="name != null">
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="age != null">
        age = #{age,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="cn.dwg.model.Student">
    update student
    set name = #{name,jdbcType=VARCHAR},
      age = #{age,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <resultMap id="BaseResultMap" type="cn.dwg.model.Student">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="age" jdbcType="INTEGER" property="age" />
  </resultMap>
</mapper>

这个是xml文件,是用逆向生成的,之前发现居然重复了两遍,但删掉重复之后还是老问题