黔程似锦 2022-08-26 12:48 采纳率: 50%
浏览 74
已结题

springboot报错

shpringboot运行之后访问本地数据,报mapper语句和xml文件没绑定,检查了xml文件id的正确性,也没写错,运行之后也有mapper文件,contoller层的goods方法点进去进入的是service接口,而不是进入impl,反而从impl可以进入到controller层,有遇到过这样问题的吗
报错如下

img


代码如下
控制层

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>


```

  • 写回答

3条回答 默认 最新

  • 黔程似锦 2022-08-27 07:47
    关注

    xml文件语句查询id跟数据库的不一致

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

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 8月27日
  • 已采纳回答 8月27日
  • 创建了问题 8月26日

悬赏问题

  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决