使用 JPA 完成增删改查操作

保存

    public void testSave(){
        //1.使用工具类获取EntityManager对象(实体管理器)
        EntityManager entityManager = JpaUtils.getEntityManager();
        //2.获取事务对象,开启事务
        EntityTransaction tx = entityManager.getTransaction();//获取事务对象
        tx.begin();//开启事务
        //3.完成增删改查操作
        Customer customer = new Customer();
        customer.setCustName("LEEWLE");
        customer.setCustIndustry("体育");
        entityManager.persist(customer);//保存操作
        //4.提交事务(回滚事务)
        tx.commit();
        //5.释放资源
        entityManager.close();
    }

删除

    //删除一个客户(根据id)
    @Test
    public void testRemove(){
        //1.使用工具类获取EntityManager对象(实体管理器)
        EntityManager entityManager = JpaUtils.getEntityManager();
        //2.开启事务
        EntityTransaction tx = entityManager.getTransaction();
        tx.begin();
        //3.执行增删改查
        //① 根据id查询客户   ② 调用remove方法完成删除操作
        Customer customer = entityManager.find(Customer.class, 1l);
        entityManager.remove(customer);
        //4.提交事务
        tx.commit();
        //5.释放资源
        entityManager.close();
    }

更新


    //更新客户的操作(merge)
    @Test
    public void testUpdate(){
        //1.使用工具类获取EntityManager对象(实体管理器)
        EntityManager entityManager = JpaUtils.getEntityManager();
        //2.开启事务
        EntityTransaction tx = entityManager.getTransaction();
        tx.begin();
        //3.执行增删改查
        //① 根据 id 查询客户
        Customer customer = entityManager.find(Customer.class, 2l);
        //2.调用merge方法完成更新操作
        customer.setCustIndustry("美术");
        entityManager.merge(customer);
        //4.提交事务
        tx.commit();
        //5.释放资源
        entityManager.close();
    }

查询

	
	//使用find方法根据 id 查询客户(立即加载):
	//1.查询对象就是当前客户对象本身
	//2.在调用find方法的时候,就会发送sql语句查询数据库
    @Test
    public void testFind(){
        //1.使用工具类获取EntityManager对象(实体管理器)
        EntityManager entityManager = JpaUtils.getEntityManager();
        //2.开启事务
        EntityTransaction tx = entityManager.getTransaction();
        tx.begin();
        //3.执行增删改查
		//参数:class(查询数据的结果需要包装的实体类型的字节码)id:查询的主键的取值
        Customer customer = entityManager.find(Customer.class, 1l);
        System.out.println(customer);
        //4.提交事务
        tx.commit();
        //5.释放资源
        entityManager.close();
    }

   
	//使用getReference方法根据 id 查询客户(延迟加载):
    //1.获取的对象是一个动态代
    //2.在调用getReference方法的时候,不会立即发送sql语句查询数据库
    //(什么时候用,什么时候发送sql查询数据库)
    @Test
    public void testReference(){
        //1.使用工具类获取EntityManager对象(实体管理器)
        EntityManager entityManager = JpaUtils.getEntityManager();
        //2.开启事务
        EntityTransaction tx = entityManager.getTransaction();
        tx.begin();
        //3.执行增删改查
		//参数:class(查询数据的结果需要包装的实体类型的字节码) id:查询的主键的取值
        Customer customer = entityManager.getReference(Customer.class, 1l);
        System.out.println(customer);
        //4.提交事务
        tx.commit();
        //5.释放资源
        entityManager.close();
    }
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,使用JPA可以方便地实现基本的增删改查操作。具体步骤如下: 1. 添加JPA和数据库驱动的依赖。在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> ``` 2. 配置数据源。在application.properties文件中添加以下配置: ```properties spring.datasource.url=jdbc:h2:mem:test spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.jpa.database-platform=org.hibernate.dialect.H2Dialect ``` 3. 创建实体类。在Java包中创建实体类,使用@Entity和@Id注解标记主键。 ```java @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; private String email; // getter/setter方法 } ``` 4. 创建Repository接口。在Java包中创建一个接口,继承JpaRepository,该接口自动继承了基本的CRUD方法。 ```java public interface UserRepository extends JpaRepository<User, Long> { } ``` 5. 创建Service层。在Java包中创建Service类,使用@Autowired注解注入Repository,并实现基本的增删改查方法。 ```java @Service public class UserService { @Autowired private UserRepository userRepository; public User save(User user) { return userRepository.save(user); } public void delete(Long id) { userRepository.deleteById(id); } public User findById(Long id) { Optional<User> optional = userRepository.findById(id); return optional.orElse(null); } public List<User> findAll() { return userRepository.findAll(); } } ``` 6. 创建Controller层。在Java包中创建Controller类,使用@Autowired注解注入Service,并实现对应的接口。 ```java @RestController @RequestMapping("/users") public class UserController { @Autowired private UserService userService; @PostMapping("") public User save(@RequestBody User user) { return userService.save(user); } @DeleteMapping("/{id}") public void delete(@PathVariable Long id) { userService.delete(id); } @GetMapping("/{id}") public User findById(@PathVariable Long id) { return userService.findById(id); } @GetMapping("") public List<User> findAll() { return userService.findAll(); } } ``` 以上就是集成JPA实现基础增删改查方法的步骤。在使用中,只需通过Controller层调用对应的接口即可完成对数据库的操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值