您好,登錄后才能下訂單哦!
怎么入門SpringJPA,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
從一個helloworld開始
建立好springboot的項目后
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional></dependency><dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version></dependency><dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.29</version></dependency><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope></dependency>
配置文件
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/jpa?useSSL=FALSE&serverTimezone=GMT%2B8username: rootpassword: roottype: com.alibaba.druid.pool.DruidDataSourcefilters: statmaxActive: 20initialSize: 1maxWait: 60000minIdle: 1timeBetweenEvictionRunsMillis: 60000minEvictableIdleTimeMillis: 300000validationQuery: select 'x'testWhileIdle: truetestOnBorrow: falsetestOnReturn: falsepoolPreparedStatements: truemaxOpenPreparedStatements: 20 jpa: hibernate: ddl-auto: updateshow-sql: true
實體類
@Entity@Data@RequiredArgsConstructor@AllArgsConstructor@NoArgsConstructorpublic class User {@Id @GeneratedValue(strategy = GenerationType.IDENTITY)private Long id; @NonNull private String name; @NonNull private String email;}
倉庫文件
public interface UserRepository extends CrudRepository<User,Long> { }
controller
@RestControllerpublic class UserController {@Autowired private UserRepository userRepository; @SuppressWarnings("unchecked")@Transactional @GetMapping("/add")public Result<String> addNewUser(@RequestParam("name") String name,@RequestParam("email") String email) { User user = new User(name,email); userRepository.save(user); return Result.success("添加用戶成功"); }@GetMapping("/all")@SuppressWarnings("unchecked")public Result<Iterable<User>> getAllUsers() {return Result.success(userRepository.findAll()); }@GetMapping("/user")@SuppressWarnings("unchecked")public Result<User> getUser(@RequestParam("id") Long id) {return Result.success(userRepository.findById(id)); } }
也可以直接用名字查詢
public interface UserAloneRepository extends Repository<User,Long> { List<User> findByName(String name);}
在UserController中添加
@Autowiredprivate UserAloneRepository userAloneRepository;
@GetMapping("/user/name")@SuppressWarnings("unchecked")public Result<User> getUserByName(@RequestParam("name") String name) {return Result.success(userAloneRepository.findByName(name));}
jpa的一個好處是可以不用手工去數據庫建表,一運行就可以自動建表。
如果要分頁和排序
public interface UserPagingAndSortingRepository extends PagingAndSortingRepository<User,Long> { }
Controller中添加
/** * 分頁和排序 * @return */@SuppressWarnings("unchecked")@GetMapping("/page")public Result<Page<User>> getAllUserByPage() {return Result.success(userPagingAndSortingRepository.findAll(new PageRequest(0,2,new Sort(new Sort.Order(Sort.Direction.ASC,"name"))) ));}/** * 排序 * @return */@SuppressWarnings("unchecked")@GetMapping("/sort")public Result<Iterable<User>> getAllUserWithSort() {return Result.success(userPagingAndSortingRepository.findAll(new Sort(new Sort.Order(Sort.Direction.ASC,"name")) ));}
我們來看一下他的總體繼承關系結構圖
其中SimpleJpaRepository是他們所有接口的實現類,而JpaRepository以上其實都是兼容noSql的接口,而只有JpaRepository以下才是對數據庫特有的接口。
而使用的方法也是一樣,只需要用一個自定義接口繼承即可
public interface UserJpaRespository extends JpaRepository<User,Long> { }
jpa有自己的根據方法名的查詢生成器機制,例如之前的UserAloneRepository中的findByName,findBy是固定前綴,Name是屬性名。我們來看幾個and、or的查詢。
public interface UserAloneRepository extends Repository<User,Long> { List<User> findByName(String name); List<User> findByNameAndEmail(String name,String email); List<User> findByNameOrEmail(String name,String email);}
Controller中的
@SuppressWarnings("unchecked")@GetMapping("/and")public Result<List<User>> getUserByNameAndEmail(@RequestParam("name") String name,@RequestParam("email") String email) {return Result.success(userAloneRepository.findByNameAndEmail(name,email));}@SuppressWarnings("unchecked")@GetMapping("/or")public Result<List<User>> getUserByNameOrEmail(@RequestParam("name") String name,@RequestParam("email") String email) {return Result.success(userAloneRepository.findByNameOrEmail(name,email));}
我們從打印出來的日志可以看到
Hibernate: select user0_.id as id1_0_, user0_.email as email2_0_, user0_.name as name3_0_ from user user0_ where user0_.name=? and user0_.email=?
Hibernate: select user0_.id as id1_0_, user0_.email as email2_0_, user0_.name as name3_0_ from user user0_ where user0_.name=? or user0_.email=?
其實就是where語句后面的and和or.
這種and以及or是可以無限接下去的多條件查詢。現在我們來看一下去重和取前幾個數據
public interface UserAloneRepository extends Repository<User,Long> { List<User> findByName(String name); List<User> findByNameAndEmail(String name,String email); List<User> findByNameOrEmail(String name,String email); //去重 List<User> findDistinctByName(String name); //查找前2個 List<User> findTop2ByName(String name);}
只要加上Distinct和Top數字就可以了
現在給User實體類增加兩個字段
@Entity@Data@RequiredArgsConstructor@AllArgsConstructor@NoArgsConstructorpublic class User {@Id @GeneratedValue(strategy = GenerationType.IDENTITY)private Long id; @NonNull private String name; @NonNull private String email; private int age; private boolean sex;}
啟動項目后,數據庫會自動給user表增加上該字段
所有的jpa關鍵字如下
public interface UserAloneRepository extends Repository<User,Long> { List<User> findByName(String name); List<User> findByNameAndEmail(String name,String email); List<User> findByNameOrEmail(String name,String email); //去重 List<User> findDistinctByName(String name); //查找前2個 List<User> findTop2ByName(String name); //查找小于age參數的 List<User> findByAgeBefore(int age); //查找小于age參數的 List<User> findByAgeLessThan(int age); //查找小于等于age參數的 List<User> findByAgeLessThanEqual(int age); //查找大于age參數的 List<User> findByAgeAfter(int age); //查找大于age參數的 List<User> findByAgeGreaterThan(int age); //查找大于等于age參數的 List<User> findByAgeGreaterThanEqual(int age); //查找name為null的 List<User> findByNameIsNull(); //查找name不為null的 List<User> findByNameNotNull(); //查找like name的(此處不帶%) List<User> findByNameLike(String name); //查找not like name的(此處不帶%) List<User> findByNameNotLike(String name); //查找like %name(無后綴%) List<User> findByNameStartingWith(String name); //查找like name%(無前綴%) List<User> findByNameEndingWith(String name); //查找like %name% List<User> findByNameContaining(String name); //根據name查找,按照age排序 List<User> findByNameOrderByAgeDesc(String name); //查找不等于age的 List<User> findByAgeNot(int age); //根據age查找在ages集合內的 List<User> findByAgeIn(Set<Integer> ages); //根據age查找不在ages集合內的 List<User> findByAgeNotIn(Set<Integer> ages); //查找sex為1的 List<User> findBySexTrue(); //查找sex為0的 List<User> findBySexFalse(); //按照email查找,不區分大小寫 List<User> findByEmailIgnoreCase(String email); //按照name查找總數 long countByName(String name); //按照name刪除 //此處是根據查找出來的id一條一條刪除的,所以在調用的時候必須加事務 long deleteByName(String name); //按照name刪除 //此處是根據查找出來的id一條一條刪除的,所以在調用的時候必須加事務 List<User> removeByName(String name);}
在刪除時必須帶上事務
@Transactional@SuppressWarnings("unchecked")@GetMapping("/delete")public Result<String> deleteByName(@RequestParam("name") String name) {userAloneRepository.deleteByName(name); return Result.success("刪除成功");}@Transactional@SuppressWarnings("unchecked")@GetMapping("/remove")public Result<List<User>> removeByName(@RequestParam("name") String name) {return Result.success(userAloneRepository.removeByName(name));}
根據打印的日志可以看到其實是按照id來刪除的
Hibernate: select user0_.id as id1_0_, user0_.age as age2_0_, user0_.email as email3_0_, user0_.name as name4_0_, user0_.sex as sex5_0_ from user user0_ where user0_.name=?
Hibernate: delete from user where id=?
Hibernate: delete from user where id=?
關于怎么入門SpringJPA問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。