Skip to content

Commit

Permalink
fix(jpa) 单一实体操作实例
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhengjiaao committed Apr 22, 2024
1 parent 6fe9e33 commit 3b06bd4
Show file tree
Hide file tree
Showing 5 changed files with 334 additions and 118 deletions.
8 changes: 4 additions & 4 deletions starter-data/starter-data-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<properties>
<org.mapstruct.version>1.5.3.Final</org.mapstruct.version>
<lombok.version>1.18.20</lombok.version>
<!-- <querydsl.version>4.4.0</querydsl.version>-->
<!-- <querydsl.version>4.4.0</querydsl.version>-->
</properties>

<dependencies>
Expand Down Expand Up @@ -45,11 +45,11 @@
<artifactId>postgresql</artifactId>
<version>42.3.4</version>
</dependency>
<!--<dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>-->
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
Expand All @@ -23,38 +26,105 @@ public interface ExampleUserRepo extends
CrudRepository<ExampleUser, String>,
JpaSpecificationExecutor<ExampleUser> {

//==================查询==================

// 简单查询
Optional<ExampleUser> findByLoginName(String loginName);
// ExampleUser findByLoginName(String loginName);

// 按loginName查询name值
@Query("SELECT u.name FROM ExampleUser u WHERE u.loginName = ?1")
Optional<String> findNameByLoginName(String loginName);
// String findNameByLoginName(String loginName);

List<ExampleUser> findByIdIn(List<String> ids);
// 批量查询
List<ExampleUser> findByLoginNameIn(List<String> loginNames);

//统计查询
// 模糊查询,支持:%name%、%name、name%
List<ExampleUser> findByNameLike(String name);

// 统计查询
long countByName(String name);

//删除查询
long deleteByName(String name);
//==================查询-end==================

List<ExampleUser> removeByName(String name);

//实体类名称
@Query("select u from #{#entityName} u where u.name = ?1")
// @Query("select u from ExampleUser u where u.name = ?1")
List<ExampleUser> findByName(String name);
//==================分页查询+排序==================

// 分页查询
Page<ExampleUser> findByName(String name, Pageable pageable);
// List<ExampleUser> findByName(String name, Pageable pageable);

//分页查询
Page<ExampleUser> findByName(String lastname, Pageable pageable);
// 分页模糊查询,支持:%name%、%name、name%
Page<ExampleUser> findByNameLike(String name, Pageable pageable);

//分页模糊查询
Page<ExampleUser> findByNameLike(String lastname, Pageable pageable);
// 按name模糊分页查询,并按age和createTime排序
Page<ExampleUser> findByNameContainingIgnoreCaseOrderByAgeAscCreateTimeDesc(String name, Pageable pageable);

// 排序查询
List<ExampleUser> findByName(String name, Sort sort);
//List<ExampleUser> findByName(String lastname, Pageable pageable);

/* //使用 SpEL 表达式 - 通配符快捷方式
@Query("select u from ExampleUser u where u.name like %:#{[0]}% and u.name like %:lastname%")
List<ExampleUser> findByNameWithSpelExpression(@Param("name") String name);
//==================分页查询-end==================


//==================SpEL 表达式 通配符 ==================

// 按关键词name模糊分页查询,并按age和createTime排序,采用SpEL 表达式 通配符
@Query("SELECT u FROM ExampleUser u WHERE u.name LIKE %:keyword% ORDER BY u.age ASC, u.createTime DESC")
Page<ExampleUser> findByKeywordOrderByAgeAscCreateTimeDesc(@Param("keyword") String keyword, Pageable pageable);

// 按实体类名称和name字段模糊分页查询,并按age和createTime排序,采用SpEL 表达式 通配符
// 注:entityName=ExampleUser
@Query("SELECT u FROM #{#entityName} u WHERE u.name LIKE %:keyword% ORDER BY u.age ASC, u.createTime DESC")
Page<ExampleUser> findByEntityAndNameLikeOrderByAgeAscCreateTimeDesc(@Param("entityName") String entityName, @Param("keyword") String keyword, Pageable pageable);

// 实体类名称
@Query("select u from #{#entityName} u where u.name = :name")
// @Query("select u from ExampleUser u where u.name = ?1")
List<ExampleUser> querySpELByName(String entityName, String name);

//==================SpEL 表达式 通配符-end ==================


//==================更新==================

//修改查询
// 按loginName更新name值
@Transactional
@Modifying
@Query("update ExampleUser u set u.firstname = ?1 where u.name = ?2")
int setFixedFirstnameFor(String firstname, String name);*/
@Query("UPDATE ExampleUser u SET u.name = :newName WHERE u.loginName = :loginName")
void updateNameByLoginName(String newName, String loginName);
// @Query("update ExampleUser u set u.name = ?1 where u.loginName = ?2")
// int updateNameByLoginName(String name, String loginName);

// 按loginName更新name值,采用nativeQuery=true写法
// @Modifying
// @Query(value = "UPDATE example_user SET name = :newName WHERE login_name = :loginName", nativeQuery = true)
// void updateNameByLoginName(String newName, String loginName);

//==================更新-end==================

//==================删除==================

// 按 loginName 删除相关记录
void deleteByLoginName(String loginName);

// 按loginName删除name值
@Transactional
@Modifying
@Query("UPDATE ExampleUser u SET u.name = NULL WHERE u.loginName = :loginName")
void deleteNameByLoginName(String loginName);

// 按loginName删除name值,采用nativeQuery=true写法
// @Modifying
// @Query(value = "UPDATE example_user SET name = NULL WHERE login_name = :loginName", nativeQuery = true)
// void deleteNameByLoginName(String loginName);

// 按name删除,并返回删除的相关记录个数
long deleteByName(String name);

// 按name删除,并放回删除的相关记录
List<ExampleUser> removeByName(String name);

//==================删除-end==================

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class ExampleUser extends BaseEntity {
/**
* 用户名
*/
@Column(name = "user_name", nullable = false)
@Column(name = "user_name")
private String name;

/**
Expand All @@ -34,6 +34,11 @@ public class ExampleUser extends BaseEntity {
@Column(name = "LOGIN_NAME", nullable = false)
private String loginName;

/**
* 住址
*/
private String address;

/**
* 年龄
*/
Expand Down

This file was deleted.

Loading

0 comments on commit 3b06bd4

Please sign in to comment.