shpringboot运行之后访问本地数据,报mapper语句和xml文件没绑定,检查了xml文件id的正确性,也没写错,运行之后也有mapper文件,contoller层的goods方法点进去进入的是service接口,而不是进入impl,反而从impl可以进入到controller层,有遇到过这样问题的吗
报错如下
代码如下
控制层
package com.duing.controller;
import com.duing.model.Goods;
import com.duing.service.GoodsService;
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;
//@Controller
@RestController
public class GoodsController {
@Autowired
private GoodsService goodsService;
@GetMapping("/")
public List<Goods> list(){
// return "list";
return goodsService.getGoods();
}
}
mapper
```java
package com.duing.mapper;
import com.duing.model.Goods;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface GoodsMapper {
List<Goods> getGoods();
}
model层
package com.duing.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Goods {
private long id;
private String goods_id;
private String goods_name;
private String goods_type;
private double price;
private String img_path;
}
service层
package com.duing.service.impl;
import com.duing.mapper.GoodsMapper;
import com.duing.model.Goods;
import com.duing.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class GoodsServiceImpl implements GoodsService {
@Autowired
private GoodsMapper goodsMapper;
@Override
public List<Goods> getGoods() {
return goodsMapper.getGoods();
}
}
package com.duing.service;
import com.duing.model.Goods;
import java.util.List;
public interface GoodsService {
List<Goods> getGoods();
}
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="com.duing.mapper.GoodsMapper">
<select id="getGoods" resultType="com.duing.model.Goods">
select id,good_id,goods_name,goods_type,price,img_path
from goods
</select>
</mapper>
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/shop
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations=classpath*:/mapper/*.xml
mybatis.type-aliases-package=com.dui
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/>
</parent>
<groupId>com.duing</groupId>
<artifactId>spring-boot-duing-shop-seckill</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-duing-shop-seckill</name>
<description>spring-boot-duing-shop-seckill</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
```