导入依赖
<!-- springbootjpa启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
application.properties
#数据库表的生成策略
spring.jpa.hibernate.ddl-auto=update
spring.datasource.password=root
spring.datasource.username=root
spring.datasource.url=jdbc:mysql://localhost:3306/sprindataJpa?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#是否显示sql在控制台
spring.jpa.show-sql=true
#格式化sql
spring.jpa.properties.hibernate.format_sql=true
#数据库是否下划线命名
#spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl
#spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
pojo类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
@Entity//作为hibernate实体类
@Table(name = "customer")
@Data
@NoArgsConstructor
@AllArgsConstructor
@EntityListeners(AuditingEntityListener.class)
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "custid")
private Long custId;//客户主键
@Column(name = "custname")
private String custName;//客户名称
@Column(name = "custaddress")
private String custAddress;//客户地址
}
repository
import com.li.jap.springbootjpa.pojo.Customer;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Component;
@Component
public interface CustomerRepository extends PagingAndSortingRepository<Customer,Long> {
}
service层
import com.li.jap.springbootjpa.pojo.Customer;
public interface CustomerService {
Iterable<Customer> getAll();
}
实现类
import com.li.jap.springbootjpa.pojo.Customer;
import com.li.jap.springbootjpa.repositorys.CustomerRepository;
import com.li.jap.springbootjpa.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CustomerServiceImpl implements CustomerService {
@Autowired
CustomerRepository repository;
@Override
public Iterable<Customer> getAll() {
return repository.findAll();
}
}
controller
import com.li.jap.springbootjpa.pojo.Customer;
import com.li.jap.springbootjpa.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CustomerController {
@Autowired
CustomerService service;
@GetMapping("/customer/all")
public Iterable<Customer> getAll() {
return service.getAll();
}
}