为啥找不到这个usermapper
本帖最后由 孤世星辰 于 2024-5-25 21:41 编辑Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-05-25T21:30:27.515+08:00 ERROR 32720 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userMapper': Error creating bean with name 'userMapper' defined in file : Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
package com.zwl.controller;
import com.zwl.mapper.UserMapper;
import com.zwl.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/selectAll")
public List<User> selectAll(){
List<User> users = userMapper.selectAll();
for (User user : users) {
System.out.println(user);
}
return users;
}
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.zwl.mapper;
import com.zwl.pojo.User;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Repository
@Mapper
public interface UserMapper {
List<User> selectAll();
User queryById(int id);
int insert(User user);
int update(User user);
int deleteById(int id);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zwl.mapper.UserMapper">
<select id="selectAll" resultType="User">
select * from user
</select>
<select id="queryById" resultType="User">
select * from user where id=#{id}
</select>
<insert id="insert" parameterType="User">
insert into uer (id,name,pwd) values (#{id},#{name},#{pwd})
</insert>
<update id="update" parameterType="User">
update user set name=#{name},pwd=#{pwd} where id=#{id}
</update>
<delete id="deleteById" parameterType="int">
delete from user where id=#{id}
</delete>
</mapper>
这个错误是因为Spring无法创建`userController` Bean,具体原因是`userMapper`注入失败。报错信息中显示`Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required`,即需要提供`sqlSessionFactory`或者`sqlSessionTemplate`。
在你的代码中,我们可以看到`userMapper` 使用了 `@Autowired` 注解进行自动注入。但是,你的代码中并没有提供`sqlSessionFactory`或者`sqlSessionTemplate`的配置,导致无法创建`userMapper` Bean。
解决方法:
1. 确保你的项目中已经正确配置了 MyBatis,包括 `sqlSessionFactory` 或 `sqlSessionTemplate` 的配置。
2. 确保 `userMapper` 的包路径被正确扫描到,也可以在主配置类上添加 `@MapperScan("com.zwl.mapper")` 注解来扫描。
3. 如果确保以上两点都没问题,可以尝试重启项目或重新构建项目。
希望能帮到你解决问题!如果还有其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]