欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot+MySQL+JPA

程序员文章站 2022-04-20 21:11:31
...

1、maven

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

2、application.yml

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver

3、主键或复合主键

import lombok.Data;

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

@Embeddable
@Data
public class TcFstKey implements Serializable {
//	PRIMARY KEY (`TSID`,`FCID`,`FORECASTTIME`,`LEADTIME`)
	private Integer TSID;
	private String FCID;
	private Integer FORECASTTIME;
	private String LEADTIME;
}
import com.fasterxml.jackson.annotation.JsonInclude;
import com.mlog.njyjy.es.core.annotation.EsIndex;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;

@Data
@NoArgsConstructor
@Accessors(chain = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
@Entity
@Table(name="t_tc_fst")
public class TcFst {
    @EmbeddedId
    private TcFstKey id;
//    private Integer TSID;
//    private String FCID;
//    private Integer FORECASTTIME;
//    private String LEADTIME;
    private String TCRANK;
    private Float LATITUDE;
    private Float LONGITUDE;
}

4、JPA

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface TcFstRepository extends JpaRepository<TcFst, Integer> {
	@Query(value = "select * from t_tc_fst a where a.FORECASTTIME >= ?1 ",nativeQuery = true)
	List<TcFst> findTcFstByTime(Integer start);
}

报错1、No identifier specified for entity:

实体类id缺少@Id 或者复合主键

import javax.persistence.Id;
@Id
private Integer id;

报错2、Not a managed type:

缺少@EntityScan(“com.entity”)


@EntityScan("com.entity")
public class Application {  
  
    public static void main(String[] args) {  
        SpringApplication.run(Application.class, args);  
    }  
}

报错3、You need either to explicitly disable SSL by setting useSSL=false

原因是MySQL在高版本需要指明是否进行SSL连接,缺少useSSL=false

jdbc:mysql://127.0.0.1:3306/test?useSSL=false