Spring Boot 集成JPA

本例子采用的是h2数据库

在pom.xml中添加jpa和h2数据库依赖

 

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

 

 

1、创建一个实体类

package com.example.jdbc.jpa.model;

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

}
 

2、创建Customer持久化类CustomerRepository

package com.example.jdbc.jpa.repository;

import java.util.List;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.example.jdbc.jpa.model.Customer;

 

//继承CrudRepository里面的基本增删查改方法
public interface CustomerRepository extends CrudRepository<Customer, Long> {

    List<Customer> findByLastName(String lastName);
}

 

3、写一个controler方法调用CustomerRepository

package com.example.jdbc.jpa.controler;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.jdbc.jpa.model.Customer;
import com.example.jdbc.jpa.repository.CustomerRepository;

@RestController
public class CustomerControler {
    
    @Autowired //don't forget the setter
    private CustomerRepository customerRepository;
    @Bean
    @RequestMapping("/saveCustomer")
    public String saveCustomer(){
    
        Customer customer =new Customer("Jack", "Bauer");
        
        customerRepository.save(customer);
        return customer.toString();
 
    }
    
    
    @Bean
    @RequestMapping("/getCustomer")
    public String getCustomer(){
    
        List<Customer> customers=customerRepository.findByLastName("Bauer");
        Customer customer=customers.get(0);
        return customer.toString();
 
    }

}
 

4、启动main方法

package com.example.jdbc;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import lombok.extern.slf4j.Slf4j;

@SpringBootApplication
@Slf4j
public class JdbcApplication {
    
    @Autowired
    private DataSource dataSource;

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

}

 

6、在properties配置文件中进行数据库连接配置和JPA配置


spring.datasource.url=jdbc:h2:mem:foo
spring.datasource.username=sa
spring.datasource.password=

#spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create

 

7、自定义hql语句查询,在CustomerRepository类中添加

   @Query("select u from Customer u where u.firstName = :firstName")   // hql  from 后面是实体类名
    List<Customer> getCustomerByQuery(@Param("firstName") String name);

 

   自定义sql语句

       @Query(nativeQuery = true,value="select * from customer  where first_name = :firstName")
       List<Customer> getCustomerByNativeQuery(@Param("firstName") String name);

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值