mirror of
https://gitee.com/many2many/java-web.git
synced 2025-01-11 22:50:54 +08:00
Compare commits
No commits in common. "7b8e45ded1788bb3964ab45beecb156678aed2bb" and "bddbc01a24acec0c0111efa6255c76c3dc6a4759" have entirely different histories.
7b8e45ded1
...
bddbc01a24
@ -261,26 +261,15 @@ JpaRepository接口继承了PagingAndSortingRepository和CrudRepository两个接
|
||||
- existsById:判断是否存在指定主键的实体。
|
||||
- flush:立即将持久化上下文的挂起状态写入数据库。
|
||||
|
||||
|
||||
JpaRepository还支持自定义查询方法,这可以通过在接口中添加自定义方法来实现。
|
||||
|
||||
**通过方法名来定义查询**
|
||||
|
||||
参考[Spring Data JPA方法名命名规则](https://www.jianshu.com/p/1d6f27f675bb)
|
||||
|
||||
**使用@Query注解**
|
||||
|
||||
@Query具有灵活性和可扩展性,例如:
|
||||
JpaRepository还支持自定义查询方法,这可以通过在接口中添加自定义方法来实现。自定义查询方法应使用@Query注解,具有灵活性和可扩展性,例如:
|
||||
|
||||
```java
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
@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);
|
||||
@Query("SELECT u FROM User u WHERE u.username = ?1 AND u.age = ?2")
|
||||
List<User> findByUsernameAndAge(String username, Integer age);
|
||||
}
|
||||
```
|
||||
以上代码中,定义了一个查询方法findByUsernameAndAge,使用@Query注解指定查询语句。参数值通过?1和?2进行占位并传入,同时需要注意复合查询之间的关系。
|
||||
|
||||
##### Spring Boot项目中JPA的配置
|
||||
|
||||
|
@ -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() {
|
||||
|
@ -30,6 +30,7 @@ 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());
|
||||
|
@ -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连接URL,paopao是数据库名
|
||||
# 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:./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 访问路径上下文
|
||||
url: jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true # MySQL连接URL,paopao是数据库名
|
||||
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:./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
|
Loading…
Reference in New Issue
Block a user