脚本宝典收集整理的这篇文章主要介绍了springboot 整合/集成 jpa,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
1.依赖
2.新增model
3.新增接口
4.测试
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId></dependency>
package com.ligy.springbootstudyjpa.model; import javax.persistence.*; @Entity @Table(name = "account") public class Account { @Id @Column(name = "id") @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name = "username") private String username; @Column(name = "password") private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "Account{" + "id=" + id + ", username='" + username + ''' + ", password='" + password + ''' + '}'; } }
package com.ligy.springbootstudyjpa.repository; import com.ligy.springbootstudyjpa.model.Account; import org.springframework.data.jpa.repository.JpaRepository; public interface AccountRepository extends JpaRepository<Account, Integer> { }
package com.ligy.springbootstudyjpa; import com.ligy.springbootstudyjpa.model.Account; import com.ligy.springbootstudyjpa.repository.AccountRepository; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import javax.annotation.Resource; import java.util.List; @SpringBootTest public class JpaTest { @Resource AccountRepository accountRepository; @Test void test4() { Account account = accountRepository.findById(1).get(); System.out.println("查询成功:" + account); account.setPassword("11111111"); accountRepository.save(account); System.out.println("更新成功:" + account); } @Test void test3() { List<Account> all = accountRepository.findAll(); System.out.println("查询成功:" + all); } @Test void test2() { Account account = new Account(); account.setId(3); accountRepository.delete(account); System.out.println("删除成功:"); } @Test void test1() { Account account = new Account(); account.setUsername("tom"); account.setPassword("123456"); Account save = accountRepository.save(account); System.out.println("插入成功:" + save); } }
以上是脚本宝典为你收集整理的springboot 整合/集成 jpa全部内容,希望文章能够帮你解决springboot 整合/集成 jpa所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。