Growing Hacker 2023-07-16 17:36 采纳率: 94.4%
浏览 20
已结题

Spring 整合mybatis,配置类加载失败

报错为:org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [E:\WithLayui\Spring_Test\AopWIthMybatis\target\classes\com\honphie\config\MybatisConfig.class]; nested exception is java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
路径绝对正确。
这是测试类:


package com.honphie;


import com.honphie.config.SpringConfig;
import com.honphie.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class TestDemo1 {
  @Autowired
    private UserService userService;
  @Test
    public void test1(){
      userService.selectByExample();
  }
}

SpringConfig.class 里面 导入了MybatisConfig.class:

package com.honphie.config;

import org.springframework.context.annotation.*;

@Configuration
@ComponentScan("com.honphie")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MybatisConfig.class})
@EnableAspectJAutoProxy
public class SpringConfig {

}


MybatisConfig类:

package com.honphie.config;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
@MapperScan("com.honphie.mapper")

public class MybatisConfig {
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        ssfb.setTypeAliasesPackage("com.honphie.entity");
        ssfb.setDataSource(dataSource);
        return ssfb;
    }


}
mapper接口:

```java

package com.honphie.mapper;

import com.honphie.entity.User;
import com.honphie.entity.UserExample;
import java.util.List;

import org.apache.ibatis.annotations.Param;
public interface UserMapper {
    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table tb_user
     *
     * @mbg.generated Sun Jul 16 15:00:09 CST 2023
     */
    long countByExample(UserExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table tb_user
     *
     * @mbg.generated Sun Jul 16 15:00:09 CST 2023
     */
    int deleteByExample(UserExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table tb_user
     *
     * @mbg.generated Sun Jul 16 15:00:09 CST 2023
     */
    int deleteByPrimaryKey(Integer id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table tb_user
     *
     * @mbg.generated Sun Jul 16 15:00:09 CST 2023
     */
    int insert(User row);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table tb_user
     *
     * @mbg.generated Sun Jul 16 15:00:09 CST 2023
     */
    int insertSelective(User row);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table tb_user
     *
     * @mbg.generated Sun Jul 16 15:00:09 CST 2023
     */
    List<User> selectByExample(UserExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table tb_user
     *
     * @mbg.generated Sun Jul 16 15:00:09 CST 2023
     */
    User selectByPrimaryKey(Integer id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table tb_user
     *
     * @mbg.generated Sun Jul 16 15:00:09 CST 2023
     */
    int updateByExampleSelective(@Param("row") User row, @Param("example") UserExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table tb_user
     *
     * @mbg.generated Sun Jul 16 15:00:09 CST 2023
     */
    int updateByExample(@Param("row") User row, @Param("example") UserExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table tb_user
     *
     * @mbg.generated Sun Jul 16 15:00:09 CST 2023
     */
    int updateByPrimaryKeySelective(User row);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table tb_user
     *
     * @mbg.generated Sun Jul 16 15:00:09 CST 2023
     */
    int updateByPrimaryKey(User row);
}

使用了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>
  <!--
    This file is generated by MyBatis Generator.
    This file is the shell of a Mapper Config file - in many cases you will need to add
      to this file before it is usable by MyBatis.
    This file was generated on Sun Jul 16 15:00:09 CST 2023.
  -->
  <mappers>
    <mapper resource="com.honphie.mapper/UserMapper.xml" />
  </mappers>
</configuration>


<?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="com.honphie.mapper.UserMapper">
  <resultMap id="BaseResultMap" type="com.honphie.entity.User">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 16 15:00:09 CST 2023.
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="birthday" jdbcType="TIMESTAMP" property="birthday" />
    <result column="sex" jdbcType="CHAR" property="sex" />
    <result column="address" jdbcType="VARCHAR" property="address" />
    <result column="user_status" jdbcType="INTEGER" property="userStatus" />
    <result column="gallery_url" jdbcType="VARCHAR" property="galleryUrl" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 16 15:00:09 CST 2023.
    -->
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 16 15:00:09 CST 2023.
    -->
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 16 15:00:09 CST 2023.
    -->
    id, username, birthday, sex, address, user_status, gallery_url
  </sql>
  <select id="selectByExample" parameterType="com.honphie.entity.UserExample" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 16 15:00:09 CST 2023.
    -->
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from tb_user
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 16 15:00:09 CST 2023.
    -->
    select 
    <include refid="Base_Column_List" />
    from tb_user
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 16 15:00:09 CST 2023.
    -->
    delete from tb_user
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="com.honphie.entity.UserExample">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 16 15:00:09 CST 2023.
    -->
    delete from tb_user
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.honphie.entity.User">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 16 15:00:09 CST 2023.
    -->
    insert into tb_user (id, username, birthday, 
      sex, address, user_status, 
      gallery_url)
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{birthday,jdbcType=TIMESTAMP}, 
      #{sex,jdbcType=CHAR}, #{address,jdbcType=VARCHAR}, #{userStatus,jdbcType=INTEGER}, 
      #{galleryUrl,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.honphie.entity.User">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 16 15:00:09 CST 2023.
    -->
    insert into tb_user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="username != null">
        username,
      </if>
      <if test="birthday != null">
        birthday,
      </if>
      <if test="sex != null">
        sex,
      </if>
      <if test="address != null">
        address,
      </if>
      <if test="userStatus != null">
        user_status,
      </if>
      <if test="galleryUrl != null">
        gallery_url,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="username != null">
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="birthday != null">
        #{birthday,jdbcType=TIMESTAMP},
      </if>
      <if test="sex != null">
        #{sex,jdbcType=CHAR},
      </if>
      <if test="address != null">
        #{address,jdbcType=VARCHAR},
      </if>
      <if test="userStatus != null">
        #{userStatus,jdbcType=INTEGER},
      </if>
      <if test="galleryUrl != null">
        #{galleryUrl,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.honphie.entity.UserExample" resultType="java.lang.Long">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 16 15:00:09 CST 2023.
    -->
    select count(*) from tb_user
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 16 15:00:09 CST 2023.
    -->
    update tb_user
    <set>
      <if test="row.id != null">
        id = #{row.id,jdbcType=INTEGER},
      </if>
      <if test="row.username != null">
        username = #{row.username,jdbcType=VARCHAR},
      </if>
      <if test="row.birthday != null">
        birthday = #{row.birthday,jdbcType=TIMESTAMP},
      </if>
      <if test="row.sex != null">
        sex = #{row.sex,jdbcType=CHAR},
      </if>
      <if test="row.address != null">
        address = #{row.address,jdbcType=VARCHAR},
      </if>
      <if test="row.userStatus != null">
        user_status = #{row.userStatus,jdbcType=INTEGER},
      </if>
      <if test="row.galleryUrl != null">
        gallery_url = #{row.galleryUrl,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="example != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 16 15:00:09 CST 2023.
    -->
    update tb_user
    set id = #{row.id,jdbcType=INTEGER},
      username = #{row.username,jdbcType=VARCHAR},
      birthday = #{row.birthday,jdbcType=TIMESTAMP},
      sex = #{row.sex,jdbcType=CHAR},
      address = #{row.address,jdbcType=VARCHAR},
      user_status = #{row.userStatus,jdbcType=INTEGER},
      gallery_url = #{row.galleryUrl,jdbcType=VARCHAR}
    <if test="example != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.honphie.entity.User">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 16 15:00:09 CST 2023.
    -->
    update tb_user
    <set>
      <if test="username != null">
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="birthday != null">
        birthday = #{birthday,jdbcType=TIMESTAMP},
      </if>
      <if test="sex != null">
        sex = #{sex,jdbcType=CHAR},
      </if>
      <if test="address != null">
        address = #{address,jdbcType=VARCHAR},
      </if>
      <if test="userStatus != null">
        user_status = #{userStatus,jdbcType=INTEGER},
      </if>
      <if test="galleryUrl != null">
        gallery_url = #{galleryUrl,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.honphie.entity.User">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Sun Jul 16 15:00:09 CST 2023.
    -->
    update tb_user
    set username = #{username,jdbcType=VARCHAR},
      birthday = #{birthday,jdbcType=TIMESTAMP},
      sex = #{sex,jdbcType=CHAR},
      address = #{address,jdbcType=VARCHAR},
      user_status = #{userStatus,jdbcType=INTEGER},
      gallery_url = #{galleryUrl,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

  • 写回答

2条回答 默认 最新

  • Growing Hacker 2023-07-18 01:51
    关注

    经过对spring源码层层debug 发现问题是,嗯,少了依赖,操。spring-jdbc

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

报告相同问题?

问题事件

  • 系统已结题 7月26日
  • 已采纳回答 7月18日
  • 修改了问题 7月16日
  • 创建了问题 7月16日

悬赏问题

  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥50 win10链接MySQL
  • ¥35 跳过我的世界插件ip验证
  • ¥15 抖音看过的视频,缓存在哪个文件
  • ¥15 自定义损失函数报输入参数的数目不足
  • ¥15 如果我想学习C大家有是的的资料吗
  • ¥15 根据文件名称对文件进行排序
  • ¥15 deploylinux的ubuntu系统无法成功安装使用MySQL❓
  • ¥15 有人会用py或者r画这种图吗