Compare commits

..

No commits in common. "7b8e45ded1788bb3964ab45beecb156678aed2bb" and "bddbc01a24acec0c0111efa6255c76c3dc6a4759" have entirely different histories.

5 changed files with 41 additions and 51 deletions

View File

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

View File

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

View File

@ -30,6 +30,7 @@ public class UserService {
public User updateUser(Long id, User user){ public User updateUser(Long id, User user){
userRepository.findById(id).ifPresent(u->{ userRepository.findById(id).ifPresent(u->{
// u.setName(user.getName());
u.setPassword(user.getPassword()); u.setPassword(user.getPassword());
u.setEmail(user.getEmail()); u.setEmail(user.getEmail());
u.setPhone(user.getPhone()); 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: spring:
jackson: jackson:
property-naming-strategy: SNAKE_CASE # 驼峰转下划线 property-naming-strategy: SNAKE_CASE # 驼峰转下划线
datasource: datasource:
url: jdbc:h2:file:./db.h2 # 使用文件存储 url: jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true # MySQL连接URLpaopao是数据库名
driverClassName: org.h2.Driver driverClassName: com.mysql.cj.jdbc.Driver # MySQL的Driver类名
username: root username: root # MySQL用户名
password: root password: root # MySQL密码
h2:
console: # 开启console访问 默认false
enabled: true
settings:
trace: true # 开启h2 console 跟踪 方便调试 默认 false
web-allow-others: true # 允许console 远程访问 默认false
path: /h2 # h2 访问路径上下文
jpa: jpa:
show-sql: true show-sql: true
open-in-view: false open-in-view: false
defer-datasource-initialization: true defer-datasource-initialization: true
database-platform: org.hibernate.dialect.H2Dialect database-platform: org.hibernate.dialect.MySQL8Dialect # 修改为MySQL的Dialect
hibernate: hibernate:
ddl-auto: update 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 访问路径上下文
# jpa:
# show-sql: true
# open-in-view: false
# defer-datasource-initialization: true
# database-platform: org.hibernate.dialect.H2Dialect
# hibernate:
# ddl-auto: update