Spring Data 增删改查事务的使用(七)
@Modifying 注解使用
@Modifying 结合 @Query注解进行更新操作
咱们单单的一个查询注解是没有办法完成事务的操作的
我们还要结合一点就是@Transaction 在springdata的使用
话不多说 我们开始
开始之前我先讲下上篇文章的 漏了一个 查询总记录数的 本来想给大家写个例子 今天在这里补上
还是在 EmployeeRepository.java 该类增加新的方法
//获取总记录数
//nativeQuery =true 表示支持本地sql查询
@Query(nativeQuery = true,value = "select count(*) from employee")
public long getCount();
编写一个测试方法
@Test
public void tesquerayParams(){
List<Employee> employees = employeeRepository.querayParams("wangwu",12);
for (Employee employee: employees) {
System.out.println("id:" + employee.getId()
+ " , name:" + employee.getName()
+ " ,age:" + employee.getAge());
}
}
执行结果

进入正题 咱们讲下事务的操作
还是一样 通过例子给大家展示
还是在EmployeeRepository.java ---------->update方法
package org.springdata.repository;
import org.springdata.domain.Employee;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.RepositoryDefinition;
import org.springframework.data.repository.query.Param;
import java.util.List;
/***
*
*/
@RepositoryDefinition(domainClass = Employee.class, idClass = Integer.class)
public interface EmployeeRepository /*extends Repository<Employee,Integer>*/ {
/**
* 根据名字找员工
* desc 大家可以发现 我只声明了一个方法 并没有写任何的实现类 哦了 就这样 咱们写个实现类
* @param name
* @return
*/
public Employee findByName(String name);
// name 根据模糊查询 并且 年龄<多少岁的员工
public List<Employee> findByNameIsStartingWithAndAgeLessThan(String name, Integer gae);
// names in ('','','') 年龄小于多少
public List<Employee> findByNameInOrAgeLessThan(List<String> names,Integer age);
//获取年龄最大的信息
@Query(" select o from Employee o where o.age=(select max(age) from Employee t1)")
public Employee getEmployeeByAge();
//获取name包含 哪些 并且年龄等于多少岁
@Query("select o from Employee o where o.name like %?1% and o.age = ?2")
public List<Employee> querayParams(String name,Integer age);
//获取总记录数
//nativeQuery =true 表示支持本地sql查询
@Query(nativeQuery = true,value = "select count(*) from employee")
public long getCount();
//根据id修改年龄
@Modifying //该注解表示允许修改
@Query("update Employee o set o.age=:age where o.id = :id")
public void update(@Param("id")Integer id, @Param("age")Integer age);
}
写完之后是不是可以马上测试呢? 不是的
咱们做过javaee开发的同学都知道 我们事务都是放在service 里面控制的 所以这次也是一样 咱们新建一个service包 见一个EmployeeService类
所以咱们新建一个EmployeeService.java 进行事务控制

编写一下代码:
package org.springdata.service;
import org.springdata.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
/**
* Employee 事务的控制
*/
@Service
public class EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
@Transactional //事务注解
public void update(Integer id,Integer age){
employeeRepository.update(id,age);
}
}
编写测试类-----------testupdate
package org.springdata.service;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springdata.repository.EmployeeRepository;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*/
public class SpringDataTransaction {
private ApplicationContext ctx = null;
private EmployeeService employeeService = null;
@Before
public void setup(){
ctx = new ClassPathXmlApplicationContext("beans_news.xml");
employeeService = ctx.getBean(EmployeeService.class);
System.out.println("setup");
}
@After
public void tearDown(){
ctx = null;
System.out.println("tearDown");
}
@Test
public void testUpdate(){
employeeService.update(1,52);
}
}
测试结果

Spring Data 增删改查事务的使用(七)的更多相关文章
- sssp-springmvc+spring+spring-data-jpa增删改查
环境:IDE:eclipse.jdk1.7.mysql5.7.maven 项目结构图 上面目录结构你可以自己创建 搭建框架 首先加入maven依赖包以及相关插件 <dependencies> ...
- vue.js+element ui Table+spring boot增删改查
小白初学,不懂的还是太多了,找了好多资料才做出来的先记录一下 1.先用Spring boot创建一个包含了增删改查的项目 2.创建vue.js项目 3.安装Element UI (1)进入项目文件夹下 ...
- C# Dapper 基本使用 增删改查事务等
using DapperTest.Models; using System.Collections.Generic; using System.Web.Http; using Dapper; usin ...
- C# Dapper 基本使用 增删改查事务
来源:https://blog.csdn.net/Tomato2313/article/details/78880969 using DapperTest.Models; using System.C ...
- 4、Spring+MyBatis增删改查
0.oracle数据库脚本 create table userinfo (id ), name ), password telephone ), isadmin )); --4.2 用户表序列 cre ...
- spring mongodb增删改查操作
添加数据 School @Id @GeneratedValue private long id; @Indexed(unique = true) private String name; studen ...
- PHP PDO扩展整理,包括环境配置\基本增删改查\事务\预处理
相关文章:PHP的mysql扩展整理,操作数据库的实现过程分析 PHPmysqli扩展整理,包括面向过程和面向对象的比较\事务控制\批量执行\预处理 介绍 PDO是一种PHP程序连接数据库的接口 ...
- 【php增删改查实例】第七节 - 部门管理模块(画一个datagrid表格)
在easyui中,datagrid组件需要用一个table标签去渲染. <table id="grid0" title="部门管理" class=&quo ...
- Python 模拟SQL对文件进行增删改查
#!/usr/bin/env python # _*_ coding:UTF-8 _*_ # __auth__: Dalhhin # Python 3.5.2,Pycharm 2016.3.2 # 2 ...
随机推荐
- GPIO—位带操作
GPIO—位带操作本章参考资料:< STM32F4xx 中文参考手册>存储器和总线构架章节. GPIO 章节,< Cortex®-M4 内核编程手册> 2.2.5 Bit-ba ...
- 用广搜实现的spfa
用广搜实现的spfa,如果是用一般的最短路,会发现构图很麻烦,因为它不是路径带权值,而是自身带权值.写起来只要注意,在点出队列的生活将其标记为0,在要压入队列的时候,判断其标记是否为0,为0表示队列中 ...
- 使用python对mysql主从进行监控,并调用钉钉发送报警信息
1.编写python的监控脚本 A.通过获取mysql库中的状态值来判断这个mysql主从状态是否正常 B.进行两个状态值的判断 C.进行调取钉钉机器人,发送消息 2.设置定时任务进行脚本运行 cro ...
- sama5d3 环境检测 adc测试
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h># ...
- Android——Activity生命周期(转)
Activity生命周期 子曰:溫故而知新,可以為師矣.<論語> 学习技术也一样,对于技术文档或者经典的技术书籍来说,指望看一遍就完全掌握,那基本不大可能,所以我们需要经常回过头再仔细 ...
- Ubuntu下安装SSH服务
判断是否安装ssh服务,可以通过如下命令进行: $ ssh localhost ssh: connect to host localhost port 22: Connection refused 如 ...
- java----内部类与匿名内部类的各种注意事项与知识点
Java 内部类分四种:成员内部类.局部内部类.静态内部类和匿名内部类.1.成员内部类: 即作为外部类的一个成员存在,与外部类的属性.方法并列.注意:成员内部类中不能定义静态变量,但可以访问外部类的所 ...
- 【UVa】Headmaster's Headache(状压dp)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- python MySQLdb在windows环境下的快速安装
python MySQLdb在windows环境下的快速安装.问题解决方式 使用python访问mysql,需要一系列安装 linux下MySQLdb安装见 Python MySQLdb在Linux下 ...
- 【BZOJ3572】[Hnoi2014]世界树 虚树
[BZOJ3572][Hnoi2014]世界树 Description 世界树是一棵无比巨大的树,它伸出的枝干构成了整个世界.在这里,生存着各种各样的种族和生灵,他们共同信奉着绝对公正公平的女神艾莉森 ...