Spring Boot 2020 官方基础68课程第二十二个 Accessing Data with JPA

创建一个SpringBoot项目,依赖JPA and H2 dependencies.

package com.dongyu.springbootguide22.accessingdatajpa;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Customer {

  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private Long id;
  private String firstName;
  private String lastName;

  protected Customer() {}

  public Customer(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  @Override
  public String toString() {
    return String.format(
        "Customer[id=%d, firstName='%s', lastName='%s']",
        id, firstName, lastName);
  }

  public Long getId() {
    return id;
  }

  public String getFirstName() {
    return firstName;
  }

  public String getLastName() {
    return lastName;
  }
}

修改启动类增加Command.

package com.dongyu.springbootguide22;

import com.dongyu.springbootguide22.accessingdatajpa.Customer;
import com.dongyu.springbootguide22.accessingdatajpa.CustomerRepository;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;


 

@SpringBootApplication
public class DemoApplication {
	private static final Logger log = LoggerFactory.getLogger(DemoApplication.class);

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}



	@Bean
	public CommandLineRunner demo(CustomerRepository repository) {
	  return (args) -> {
		// save a few customers
		repository.save(new Customer("Jack", "Bauer"));
		repository.save(new Customer("Chloe", "O'Brian"));
		repository.save(new Customer("Kim", "Bauer"));
		repository.save(new Customer("David", "Palmer"));
		repository.save(new Customer("Michelle", "Dessler"));
  
		// fetch all customers
		log.info("Customers found with findAll():");
		log.info("-------------------------------");
		for (Customer customer : repository.findAll()) {
		  log.info(customer.toString());
		}
		log.info("");
  
		// fetch an individual customer by ID
		Customer customer = repository.findById(1L);
		log.info("Customer found with findById(1L):");
		log.info("--------------------------------");
		log.info(customer.toString());
		log.info("");
  
		// fetch customers by last name
		log.info("Customer found with findByLastName('Bauer'):");
		log.info("--------------------------------------------");
		repository.findByLastName("Bauer").forEach(bauer -> {
		  log.info(bauer.toString());
		});
		// for (Customer bauer : repository.findByLastName("Bauer")) {
		//  log.info(bauer.toString());
		// }
		log.info("");
	  };
	}
}

JPA是SpringBoot默认启动的。

 

It seems that you are encountering an error related to the Spring Boot starter parent dependency with version 2.3.0.RELEASE. This error usually occurs when the specified dependency cannot be found in the repository or when there is an issue with the dependency declaration. To resolve this issue, you can try a few troubleshooting steps: 1. Verify the dependency declaration: Double-check the spelling and syntax of the dependency declaration in your project's build file (e.g., pom.xml for Maven or build.gradle for Gradle). Ensure that the version number is correct and matches the available versions in the repository. 2. Clear local repository cache: Sometimes, the issue can be caused by a corrupted local repository cache. You can try deleting the contents of your local repository cache and re-downloading the dependencies. The location of the local repository varies depending on your build tool (e.g., ~/.m2/repository for Maven). 3. Check network connectivity: Ensure that your network connection is stable and capable of accessing the remote repository. Try pinging the repository URL or accessing it via a web browser to ensure that it is accessible. 4. Use a different version: If the specific version (2.3.0.RELEASE) is not available or causing issues, you can try using a different version of the Spring Boot starter parent dependency. Check the official Spring Boot documentation or repository for the available versions and select a suitable one. If none of these steps resolve the issue, please provide more details about your project setup, build tool, and any relevant error messages, so that I can assist you better.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东宇科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值