您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在springdata中使用jpa單表操作crud,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
使用boot整合,導入springdata jap, mysql 驅動,lombok,web。
# boot add jpa, oh~ crud in single table server: port: 8888 spring: # datasource datasource: username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mydatabase?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai # jpa config jpa: hibernate: #配置創建表的相關操作 ddl-auto: update #顯示sql show-sql: true
package top.bitqian.springdatajpa.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import javax.persistence.*; import java.math.BigDecimal; import java.util.Date; /** * @author echo lovely * @date 2021/1/10 16:06 */ @Data @NoArgsConstructor @AllArgsConstructor @Table(name = "programmer_info") @Entity public class ProgrammerInfo { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer programmerId; @Column(name = "name", unique = true, nullable = false, length = 50) private String name; /** * 0 male * 1 female */ private Integer gender; private Date personBirth; @Transient // 生成表的時候忽略money字段 private BigDecimal money; }
package top.bitqian.springdatajpa.repository; import org.springframework.data.jpa.repository.JpaRepository; import top.bitqian.springdatajpa.entity.ProgrammerInfo; /** * @author echo lovely * @date 2021/1/10 16:13 */ public interface ProgrammerRepository extends JpaRepository<ProgrammerInfo, Integer> { // TODO: todo sth... }
業務類
package top.bitqian.springdatajpa.service.impl; import org.springframework.stereotype.Service; import top.bitqian.springdatajpa.entity.ProgrammerInfo; import top.bitqian.springdatajpa.repository.ProgrammerRepository; import top.bitqian.springdatajpa.service.ProgrammerInfoService; import javax.annotation.Resource; import java.util.List; /** * @author echo lovely * @date 2021/1/10 16:15 */ @Service public class ProgrammerInfoServiceImpl implements ProgrammerInfoService { @Resource private ProgrammerRepository programmerRepository; @Override public void saveBatch(List<ProgrammerInfo> programmerInfos) { programmerRepository. saveAll(programmerInfos). forEach(r -> System.out.println("批量新增完成 =>" + r)); } }
測試
package top.bitqian.springdatajpa; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import top.bitqian.springdatajpa.entity.ProgrammerInfo; import top.bitqian.springdatajpa.service.ProgrammerInfoService; import javax.annotation.Resource; import java.util.ArrayList; import java.util.Date; import java.util.List; @SpringBootTest class SpringdataJpaApplicationTests { @Resource private ProgrammerInfoService programmerInfoService; @Test void contextLoads() { List<ProgrammerInfo> list = new ArrayList<>(); Date date = new Date(); list.add(new ProgrammerInfo(null, "小千", 0, date, null)); list.add(new ProgrammerInfo(null, "小小", 1, date, null)); list.add(new ProgrammerInfo(null, "二哈", 0, date, null)); programmerInfoService.saveBatch(list); } }
根據條件精確查詢, Example作為參數。
@Override public List<ProgrammerInfo> getProgrammerByInfo(ProgrammerInfo programmerInfo) { // 根據條件查詢集合 Example<ProgrammerInfo> example = Example.of(programmerInfo); return programmerRepository.findAll(example); }
@Test void testGetProgrammerByInfo() { // 根據姓名查詢。 ProgrammerInfo info = new ProgrammerInfo(null, "小小", null, null, null); programmerInfoService.getProgrammerByInfo(info).forEach(System.out::println); }
自定義hql語句查詢
// 1. query by hql @Query(value = "from ProgrammerInfo pi where pi.name like ?1") List<ProgrammerInfo> findProgrammerByName(String name);
@Test void testPrByName() { // 查詢名稱包含 '小' pr.findProgrammerByName("%小%").forEach(System.out::println); }
自定義sql語句查詢
// 2. query by sql @Query(value = "select * from programmer_info where name like :#{#pi.name} and gender = :#{#pi.gender}", nativeQuery = true) List<ProgrammerInfo> findPrByPrInfo(@Param("pi") ProgrammerInfo programmerInfo);
@Test void testPrByPr() { ProgrammerInfo info = new ProgrammerInfo(null, "%小%", 0, null, null); pr.findPrByPrInfo(info).forEach(System.out::println); }
4. 修改 by hql
sql也行,hql優先。
// upd @Transactional @Modifying @Query(value = "update ProgrammerInfo p set p.name = :#{#pi.name}, p.personBirth = :#{#pi.personBirth} where p.programmerId = :#{#pi.programmerId}") void updPrById(@Param("pi") ProgrammerInfo programmerInfo);
@Test void testPrUpd() { ProgrammerInfo info = new ProgrammerInfo(1, "小小可愛", 0, new Date(), null); pr.updPrById(info); }
5. 刪除 by hql
// del @Transactional @Modifying @Query(value = "delete from ProgrammerInfo where programmerId = :#{#pi.programmerId}") void delPrById(@Param("pi") ProgrammerInfo programmerInfo);
@Test void testPrDel() { ProgrammerInfo info = new ProgrammerInfo(1, null, 0, null, null); pr.delPrById(info); }
坑:
1. 修改操作得加上事務控制。
2. @Query 進行操作時,占位符使用注意套娃。不要套錯。
單表操作可以的,但是mybatisPlus 更easy.jpg.
上述內容就是怎么在springdata中使用jpa單表操作crud,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。