java - 使用java spring boot有什么问题

标签 java spring-boot jsp jpa

我尝试使用 java-spring-boot 和 Mysql 搜索所有数据,但是当我运行代码时出现此错误

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'emp0_.get_company_name' in 'field list'

这是我的 Controller 代码

package com.example.rest.controller;


import com.example.rest.Emp;
import com.example.rest.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
@RequestMapping("emp")
public class EmpController {
    @Autowired
    private EmpService empService;

    @GetMapping(produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<List<Emp>> getAllEmps() {
        List<Emp> emps = empService.findAll();
        return new ResponseEntity<List<Emp>>(emps,HttpStatus.OK);
    }
}

这是我的 EmpRepository 代码

package com.example.rest.repository;
import com.example.rest.Emp;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface EmpRepository  extends JpaRepository<Emp, Integer> {
}

这是我的 EmpService 代码

package com.example.rest.service;

import com.example.rest.Emp;

import java.util.List;

public interface EmpService {
    List<Emp> findAll();
}

这是我的 EmpServiceImpl 代码

package com.example.rest.service;

import com.example.rest.Emp;
import com.example.rest.repository.EmpRepository;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpRepository empRepository;

    @Override
    public List<Emp> findAll() {

        List<Emp> emps= new ArrayList<>();
        empRepository.findAll().forEach((e -> emps.add(e)));

        return emps;
    }
}

这是我的应用程序代码

package com.example.rest;

import com.example.rest.repository.EmpRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@SpringBootApplication
@Configuration
public class Application {
    @Autowired
    EmpRepository empRepository;
    public static void main(String[] args) {
        SpringApplication.run(Application.class , args);
    }
}

这是我的员工代码

package com.example.rest;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "company")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    private Integer idx;
    private String company_id;
    private String getCompany_name;
}

因此,如果有人知道问题是什么,请告诉我,如果您没有提醒解决方案

谢谢!

最佳答案

在 EmpServiceImpl 类上使用 @Service 注释,在 EmpRepository 上使用 @Repository 注释。

        @Service
        public class EmpServiceImpl implements EmpService {
            @Autowired
            private EmpRepository empRepository;

            @Override
            public List<Emp> findAll() {

                List<Emp> emps= new ArrayList<>();
                empRepository.findAll().forEach((e -> emps.add(e)));

                return emps;
            }
        }

        @Repository
        public interface EmpRepository  extends JpaRepository<Emp, Integer> {

        }

关于java - 使用java spring boot有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59558568/

相关文章:

java - 如何将 spring boot 参数传递给 tomcat 部署?

java - Quartz 状态作业报告

java - eclipse hadoop 连接被拒绝

java - 在java中解析日期和时间

java - 如何设计适合自定义排序的数据结构?

java - 在 Java 中,将命令发送到另一个命令行程序

java - 有没有办法将 @Scheduled 与 Duration 字符串(如 15s 和 5m)一起使用?

jsp - 使用 scriptlet 调用 onClick 函数

java - 如何在 JSP 中基于另一个属性显示一个属性

java - 在Java中获取Map中键的值