本例子采用的是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);