【问题标题】:what is the problem using java spring boot使用java spring boot有什么问题
【发布时间】:2020-01-02 05:15:01
【问题描述】:

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

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

这是我的控制器代码

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;
}

所以如果有人知道是什么问题,请告诉我,如果你不提醒解决方案

谢谢!

【问题讨论】:

    标签: java spring-boot jsp jpa


    【解决方案1】:

    在 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> {
    
            }
    

    【讨论】:

    • 现在正在工作,但我得到了 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'emp0_.get_company_name' in 'field list' 这个错误你知道为什么会出现这个错误吗?
    • getCompany_name 此列在公司表中不存在。您必须更改表并将 getCompany_name 列添加到公司表中。对于开发环境,您可以在应用程序 yml 中设置 jpa:hibernate:ddl-auto: update 以便它可以自动更新。
    • 始终注意,每当在实体类中添加新字段(即新列)时,您需要使用 jpa ddl 设置重新启动项目以更新或手动获取添加到数据库中的列以避免这样的冲突。您可以参考-spring.io/guides/gs/accessing-data-mysql 了解更多信息。
    【解决方案2】:

    需要在下面的类上添加注释:

    @Service  
    class EmpServiceImpl implements EmpService {
    
    }
    

    【讨论】:

    • 重复答案。
    猜你喜欢
    • 2021-07-31
    • 1970-01-01
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 2020-05-15
    • 2017-06-28
    • 2010-11-06
    相关资源
    最近更新 更多