spring boot:2.7.2
提供gradle、maven两种可选方案
gradle build.gradle
dependencies {
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'mysql:mysql-connector-java'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
}
maven pom.xml
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</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-data-jpa</artifactId>
</dependency>
</dependencies>
application.yml
server:
port: 83
spring:
jackson:
date-format: "yyyy-dd-MM HH:mm:ss"
jpa:
show-sql: true
open-in-view: false
hibernate:
ddl-auto: update
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/jpa?serverTimezone=Asia/Shanghai
username: root
password: 123456
JpaApplication.java
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@EnableJpaAuditing
@SpringBootApplication
public class JpaApplication {
public static void main(String[] args) {
SpringApplication.run(JpaApplication.class, args);
}
}
entity/User.java
lombok的好处就是不用写Getter、Setter、重写equals、hashCode方法,还可以使用@Slf4j打印日志
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.util.Date;
@Entity
@Table(name = "user")
@EntityListeners(AuditingEntityListener.class)
@Data
public class User{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name",nullable = false,unique = true,length = 20)
private String name;
@Temporal(TemporalType.DATE)
private Date birthday;
@CreatedDate
private Date createTime;
@LastModifiedDate
private Date modifyTime;
}
repository/UserRepository.java
类似dao(mapper),但jpa这里它叫什么,建议我们命名时就叫什么。
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {}
service/UserService.java
service层,实现增删改查没什么好说的,都一样
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
@Slf4j
@Service
public class UserService {
@Resource
private UserRepository userRepository;
public Optional<User> findById(Long id){
Optional<User> user = userRepository.findById(id);
log.info("通过ID查询一条用户记录:{}",user);
return user;
}
public List<User> findAll(){
List<User> userList = userRepository.findAll();
log.info("查询所有用户(不分页):{}",userList);
return userList;
}
public Page<User> findAllByPage(Integer pageNum, Integer pageSize){
Sort sort = Sort.by(Sort.Direction.DESC,"id");
Pageable pageable = PageRequest.of(pageNum,pageSize,sort);
Page<User> userPage = userRepository.findAll(pageable);
log.info("查询所有用户(分页):{}",userPage);
return userPage;
}
public void save(User user){
log.info("新增/修改一条用户记录:{}",userRepository.save(user));
}
public void deleteById(Long id){
log.info("通过ID删除一条用户记录:{}",id);
userRepository.deleteById(id);
}
}
controller/UserController.java
controller层也没什么好说的都一样
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("user")
public class UserController {
@Resource
private UserService userService;
@GetMapping("findById")
public Optional<User> findById(@RequestParam Long id){
return userService.findById(id);
}
@GetMapping("findAll")
public List<User> findAll(){
return userService.findAll();
}
@GetMapping("findAllByPage")
public Page<User> findAllByPage(@RequestParam(defaultValue = "0") Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize){
return userService.findAllByPage(pageNum,pageSize);
}
@PostMapping("save")
public void save(@RequestBody User user){
userService.save(user);
}
@PostMapping()
public void deleteById(@RequestParam Long id){
userService.deleteById(id);
}
}
mysql创建jpa数据库即可,不用创建表
建议
启动springboot项目jpa会自动帮我们创建好user表,然后用postman调用controller层的接口即可。写的很详细,这里就不赘述了。
项目结构
|