1.pom.xml(创建项目默认勾选springweb,以下是需要自己添加的依赖)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
<dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency>
2.主体
《1》userDao接口(注意方法名不可以随便乱取,参考JpaRepository接口源码)
package com.example.jpa.Dao; import com.example.jpa.entity.User; import jakarta.transaction.Transactional; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; public interface UserDao extends JpaRepository<User, Integer> { //查询所有用户 List<User>findAll(); //添加 User save(User user); //id查询 User getUserById(int id); //id删除,下面这行注解防止删除失败 @Transactional void deleteUserById(int id); //分页查询 Page<User>findAll(Pageable pageable); }
《2》userService实现类
package com.example.jpa.Service; import com.example.jpa.Dao.UserDao; import com.example.jpa.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService { @Autowired private UserDao userDao; //查询所有 public List<User> findAll() { return userDao.findAll(); } //添加 public User save(User user) { return userDao.save(user); } //id查 public User getUserById(int id) { return userDao.getUserById(id); } //id删 public void deleteUserById(int id) { userDao.deleteUserById(id); } //分页查询 public Page<User>findAll(Pageable pageable) { return userDao.findAll(pageable); } } 《3》控制类
package com.example.jpa.Controller; import com.example.jpa.Service.UserService; import com.example.jpa.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/getAll") public List<User> getAll() { return userService.findAll(); } //添加 @GetMapping("/save") public void saveUser(){ User user = new User(); user.setName("张三"); user.setJobNo("110"); user.setDepartment("研发部"); userService.save(user); System.out.println(user); } //id获取 @GetMapping("/getUser/{id}") public void getUserById(@PathVariable int id){ User user = userService.getUserById(id); System.out.println(user); } //id删除 @GetMapping("/deleteUser/{id}") public void deleteUserById(@PathVariable int id){ userService.deleteUserById(id); System.out.println("删除成功"); } //分页查询 @GetMapping("/findAll") public void findAll(){ PageRequest pageable= PageRequest.of(0,2); Page<User>page= userService.findAll(pageable); System.out.println("总页数"+page.getTotalElements()); } }
《4》user实体类
package com.example.jpa.entity; import jakarta.persistence.*; import lombok.Data; @Entity(name="tb_customer") @Data public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name = "name", nullable = false) private String name; @Column(name = "jobNo", nullable = false) private String jobNo; @Column(name = "department", nullable = false) private String department; }
3.application.properties
spring.jpa.open-in-view=false spring.datasource.url=jdbc:mysql://localhost:3306/springboot spring.datasource.username=root spring.datasource.password=3121384913 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.show-sql=true spring.jpa.database=mysql spring.jpa.hibernate.ddl-auto=none spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl 注意:如果你的数据库是5.7版本的有可能需要添加该配置项:spring.jpa.properties.hibernate.dialect=,但是如果你版本为8以上可以不加,如果5.7的加了报错也可以不加。