SpringBoot+JPA(官方案例)

  1. 在线文档
  2. 项目结构
    1.源码克隆:git clone https://github.com/spring-guides/gs-accessing-data-jpa.git
    2.包含两个项目initial和complete,initial可以根据文档练习完善,complete是完整项目
    3.功能描述:构建应用程序,使用 Spring Data JPA 在关系数据库中存储和检索数据

  3. 源码分析

    1.POM依赖
    <dependencies>
    	<!--https://blog.csdn.net/tongxin_tongmeng/article/details/128586690-->
    	<dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-data-jpa</artifactId>
    	</dependency>
    
    	<dependency>
    		<groupId>com.h2database</groupId>
    		<artifactId>h2</artifactId>
    		<scope>runtime</scope>
    	</dependency>
    	<dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-test</artifactId>
    		<scope>test</scope>
    	</dependency>
    </dependencies>
    2.POJO类
    @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;
    	}
    }
    3.接口类
    public interface CustomerRepository extends CrudRepository<Customer, Long> {
    
    	List<Customer> findByLastName(String lastName);
    
    	Customer findById(long id);
    }
    4.测试类
    /**
     *这是一个 Java 程序的主类。它使用 Spring Boot 应用程序注解(@SpringBootApplication)标记,表示这是一个 Spring Boot 应用程序。
     *
     * 主方法是程序的入口点,在这里调用了 SpringApplication.run() 方法来启动应用程序。
     *
     * 此外,还定义了一个名为 "demo" 的 Bean,类型为 CommandLineRunner。它会在应用程序启动后立即执行。
     *
     * 在 demo 方法中,使用了一个 CustomerRepository 接口的实例来保存几个 Customer 对象,并使用日志打印出所有保存的 Customer 对象。
     *
     * CustomerRepository 接口是由 Spring Data JPA 自动生成的,它继承自 CrudRepository 接口,提供了常用的数据访问操作。
     *
     * 具体来说,它提供了以下方法:
     *
     * save():保存单个对象
     * findAll():查询所有对象
     * findById():根据 id 查询单个对象
     * findByLastName():根据 lastName 查询多个对象
     * 在 demo 方法中,还使用了 Java 8 的新特性——Lambda 表达式。在 repository.findByLastName("Bauer") 之后,使用 forEach() 方法遍历查询结果,并使用 Lambda 表达式打印每个结果。
     */
    // https://blog.csdn.net/tongxin_tongmeng/article/details/128401278
    @SpringBootApplication
    public class AccessingDataJpaApplication {
    
    	private static final Logger log = LoggerFactory.getLogger(AccessingDataJpaApplication.class);
    
    	public static void main(String[] args) {
    		SpringApplication.run(AccessingDataJpaApplication.class);
    	}
    
    	@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("");
    		};
    	}
    
    }
  4. 项目演示
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

童心同萌

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

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

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

打赏作者

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

抵扣说明:

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

余额充值