org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘studentController’: Unsatisfied dependency expressed through field ‘studentService’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘studentServiceImpl’: Unsatisfied dependency expressed through field ‘baseMapper’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.case02.scu.student.mapper.StudentMapper’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
第一步 少勾选会报错
新建項目
勾选
developer Tools === SpringBoot devTools
developer Tools===lombok
web === springweb
sql==mabatis framework 、mysql Driver
建包
controller
mapper
pojo
until
service
resource下也建mapper(看情况)
第二步 pom。xml导入依赖 不导会报错
添加数据源Druid
com.alibaba
druid
1.1.6
第三步 配置文件 不导会报错
在resources 下 mybatis: 中:后面一定要加个空格 不然会报错
map-underscore-to-camel-case: true开启驼峰
注意点
mapper接口上加@mapper和@Repository(第二个不加项目报错但是能运行)
或者你在启动类写@mapperScan()(不推荐)
server:
port: 8080
spring:
profiles:
active: dev
---
server:
port: 8082
spring:
profiles:test
---
server:
port: 8083spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/kj08?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
username: root
password: 9320
type: com.alibaba.druid.pool.DruidDataSource
druid:
initial-size: 5
min-idle: 5
max-active: 20
#配置获取连接等待超时的时间
max-wait: 60000
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
time-between-eviction-runs-millis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
min-evictable-idle-time-millis: 30000mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: mapper/*.xml
第四步 连接数据库 创建实体类
注意与Application同级目录
package com.kj15.sb02.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.ibatis.type.Alias;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@Alias("student")
public class Student {
private Long id;
private Long studentNo;
private String loginPwd;
private String studentName;
private Long sex;
private String phone;
private String address;
private Date bornDate;
private String email;
private String identityCard;
private Long classId;
private String picPath;
}
第五步 创建mapper接口
注意与Application同级目录
package com.kj15.sb02.mapper; import com.kj15.sb02.pojo.Student; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; import java.util.List; @Mapper @Repository public interface StudentMapper { @Select("select * from student") public ListqueryAll(); }
第六步 测试
package com.kj15.sb02; import com.kj15.sb02.mapper.StudentMapper; import com.kj15.sb02.pojo.Student; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; @SpringBootTest class Sb02ApplicationTests { @Autowired private StudentMapper studentMapper; @Test void contextLoads() { Liststudents = studentMapper.queryAll(); for (Student s : students) { System.out.println(s); } } }



