SpringData入门笔记(五) - 修改、删除和事务

项目获取

本部份内容,代码戳这里java

数据库数据

insert into `employee` (`id`, `age`, `name`) values('1','13','zhangsan');
insert into `employee` (`id`, `age`, `name`) values('2','20','test1');
insert into `employee` (`id`, `age`, `name`) values('3','21','test2');
insert into `employee` (`id`, `age`, `name`) values('4','22','test3');
insert into `employee` (`id`, `age`, `name`) values('5','20','test4');
insert into `employee` (`id`, `age`, `name`) values('6','21','test5');
insert into `employee` (`id`, `age`, `name`) values('7','22','test6');
insert into `employee` (`id`, `age`, `name`) values('8','22','test16');

更新和删除

更新和删除操做用到了一个新的注解@Modifyingspring

通常状况下,咱们须要使用@Modifying@Query注解执行更新和删除sql

若是只使用@Query注解,如:数据库

@Query("update Employee o set o.age = ?2 where o.id = ?1")
    public void update(Integer id, Integer age);

会抛出下面异常ui

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [update com.dotleo.entity.Employee o set o.age = ?2 where o.id = ?1]; nested exception is java.lang.IllegalStateException: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [update com.dotleo.entity.Employee o set o.age = ?2 where o.id = ?1]
    ...
Caused by: java.lang.IllegalStateException: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [update com.dotleo.entity.Employee o set o.age = ?2 where o.id = ?1]
	... 37 more
Caused by: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [update com.dotleo.entity.Employee o set o.age = ?2 where o.id = ?1]
	... 57 more

咱们为它加上@Modifying注解,如:hibernate

@Modifying
    @Query("update Employee o set o.age = ?2 where o.id = ?1")
    public void update(Integer id, Integer age);

运行后会发现,仍是报错code

org.springframework.dao.InvalidDataAccessApiUsageException: Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query
    ...
Caused by: javax.persistence.TransactionRequiredException: Executing an update/delete query
    ...

这又是为何呢?事务

其实,若是是查询操做,咱们没有必要事务。可是更新操做,咱们要求数据的完整性,因此必须为操做添加事务。开发

事务的使用

有Java EE开发经验的人确定知道,若是一次须要操做多张表(执行多个DAO),业务逻辑会写在Service里,这样一个Service方法中调用的全部DAO层方法都被集合到一个事务中,保证了数据的完整性。get

所以,须要先创建EmployeeService,而后再调用EmployeeRepository中的方法。

注意,在Service的方法中,咱们必须为之添加@Transactional注解,让它支持事务,才能运行成功。

package com.dotleo.service;

import com.dotleo.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author LiuFei
 * @create 2017-11-10 22:57
 */
@Service
public class EmployeeService {

    @Autowired
    private EmployeeRepository employeeRepository;

    @Transactional
    public void update(Integer id, Integer age) {
        employeeRepository.update(id, age);
    }
}

删除操做不过是sql语句的变化,其余更为复杂的更新操做也不过是sql语句的变化,请加练习有助于记忆。

最终源代码戳这里

相关文章
相关标签/搜索