Compare commits

..

2 Commits

Author SHA1 Message Date
wu
7b8e45ded1 improve 2024-11-11 08:10:22 +08:00
wu
11aab361e1 changed 2024-11-11 08:01:26 +08:00
5 changed files with 51 additions and 41 deletions

View File

@ -261,15 +261,26 @@ JpaRepository接口继承了PagingAndSortingRepository和CrudRepository两个接
- existsById判断是否存在指定主键的实体。
- flush立即将持久化上下文的挂起状态写入数据库。
JpaRepository还支持自定义查询方法这可以通过在接口中添加自定义方法来实现。自定义查询方法应使用@Query注解具有灵活性和可扩展性例如
JpaRepository还支持自定义查询方法这可以通过在接口中添加自定义方法来实现。
**通过方法名来定义查询**
参考[Spring Data JPA方法名命名规则](https://www.jianshu.com/p/1d6f27f675bb)
**使用@Query注解**
@Query具有灵活性和可扩展性,例如:
```java
public interface UserRepository extends JpaRepository<User, Long> {
@Query("SELECT u FROM User u WHERE u.username = ?1 AND u.age = ?2")
List<User> findByUsernameAndAge(String username, Integer age);
@Query("SELECT u FROM User u WHERE u.name = ?1 AND u.age = ?2")
List<User> findByNameAndAge(String name, Integer age);
@Query(value="SELECT * FROM User u WHERE u.name Like %:name% AND u.age > :age" , nativeQuery = true)
List<User> findUsers(@Param("name") String username, @Param("age") Integer age);
}
```
以上代码中定义了一个查询方法findByUsernameAndAge使用@Query注解指定查询语句。参数值通过?1和?2进行占位并传入同时需要注意复合查询之间的关系。
##### Spring Boot项目中JPA的配置

View File

@ -15,13 +15,13 @@ public class User {
@Column(nullable = false)
private String password;
@Column()
private String email;
@Column
private String phone;
@Column
private Integer age;
public String getName() {

View File

@ -30,7 +30,6 @@ public class UserService {
public User updateUser(Long id, User user){
userRepository.findById(id).ifPresent(u->{
// u.setName(user.getName());
u.setPassword(user.getPassword());
u.setEmail(user.getEmail());
u.setPhone(user.getPhone());

View File

@ -1,42 +1,42 @@
spring:
jackson:
property-naming-strategy: SNAKE_CASE # 驼峰转下划线
datasource:
url: jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true # MySQL连接URLpaopao是数据库名
driverClassName: com.mysql.cj.jdbc.Driver # MySQL的Driver类名
username: root # MySQL用户名
password: root # MySQL密码
jpa:
show-sql: true
open-in-view: false
defer-datasource-initialization: true
database-platform: org.hibernate.dialect.MySQL8Dialect # 修改为MySQL的Dialect
hibernate:
ddl-auto: update # 根据需要调整update适用于开发和某些生产环境
format_sql: true # 保持原样用于格式化SQL输出
#
#spring:
#
# jackson:
# property-naming-strategy: SNAKE_CASE # 驼峰转下划线
#
# datasource:
# url: jdbc:h2:file:./demo.h2 # 使用文件存储
# driverClassName: org.h2.Driver
# username: root
# password: root
# h2:
# console: # 开启console访问 默认false
# enabled: true
# settings:
# trace: true # 开启h2 console 跟踪 方便调试 默认 false
# web-allow-others: true # 允许console 远程访问 默认false
# path: /h2 # h2 访问路径上下文
# url: jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true # MySQL连接URLpaopao是数据库名
# driverClassName: com.mysql.cj.jdbc.Driver # MySQL的Driver类名
# username: root # MySQL用户名
# password: root # MySQL密码
# jpa:
# show-sql: true
# open-in-view: false
# defer-datasource-initialization: true
# database-platform: org.hibernate.dialect.H2Dialect
# database-platform: org.hibernate.dialect.MySQL8Dialect # 修改为MySQL的Dialect
# hibernate:
# ddl-auto: update
# ddl-auto: update # 根据需要调整update适用于开发和某些生产环境
# format_sql: true # 保持原样用于格式化SQL输出
spring:
jackson:
property-naming-strategy: SNAKE_CASE # 驼峰转下划线
datasource:
url: jdbc:h2:file:./db.h2 # 使用文件存储
driverClassName: org.h2.Driver
username: root
password: root
h2:
console: # 开启console访问 默认false
enabled: true
settings:
trace: true # 开启h2 console 跟踪 方便调试 默认 false
web-allow-others: true # 允许console 远程访问 默认false
path: /h2 # h2 访问路径上下文
jpa:
show-sql: true
open-in-view: false
defer-datasource-initialization: true
database-platform: org.hibernate.dialect.H2Dialect
hibernate:
ddl-auto: update