CrudRepository   的主要方法

long count();
boolean exists(Integer arg0);  

<S extends StudentPO> S save(S arg0);
<S extends StudentPO> Iterable<S> save(Iterable<S> arg0);  

void delete(Integer arg0);
void delete(Iterable<? extends StudentPO> arg0);
void delete(StudentPO arg0);
void deleteAll();  

StudentPO findOne(Integer arg0);
Iterable<StudentPO> findAll();
Iterable<StudentPO> findAll(Iterable<Integer> arg0);  

1. 新建一个类 CurdEmployeeRespository   继承CrudRepository  里面实现了大量的增删改查方法

package org.springdata.repository;

import org.springdata.domain.Employee;
import org.springframework.data.repository.CrudRepository;

/**
 *
 */
public interface CurdEmployeeRespository extends CrudRepository<Employee, Integer> {

}

2. 编写service实现类

  

package org.springdata.service;

import org.springdata.domain.Employee;
import org.springdata.repository.CurdEmployeeRespository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

/**
 */

@Service
public class CrudEmployeeService {

    @Autowired
    private CurdEmployeeRespository employeeRespository;

    @Transactional
    public void save(){
        Employee employee = new Employee();
        employee.setName("zhangzy");
        employee.setAge(12);
        employeeRespository.save(employee);
    }
}

编写测试类

  

package org.springdata.crudservice;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springdata.repository.CurdEmployeeRespository;
import org.springdata.repository.EmployeeRepository;
import org.springdata.service.CrudEmployeeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 */
public class CurdServiceTest {

    private ApplicationContext ctx = null;

    private CrudEmployeeService crudEmployeeService = null;

    @Before
    public void setup(){
        ctx = new ClassPathXmlApplicationContext("beans_news.xml");
        crudEmployeeService = ctx.getBean(CrudEmployeeService.class);
        System.out.println("setup");
    }

    @After
    public void tearDown(){
        ctx = null;
        System.out.println("tearDown");
    }

    @Test
    public void save(){
        crudEmployeeService.save();
    }
}

测试结果

   因为我测试前把数据全部都删除了

Spring Data CrudRepository增删改查方法(八)的更多相关文章

  1. Django REST framework 五种增删改查方法

    Django-DRF-视图的演变   版本一(基于类视图APIView类) views.py: APIView是继承的Django View视图的. 1 from .serializers impor ...

  2. 通用mapper的增删改查方法 留存 备忘

    Mybatis通用Mapper介绍与使用   前言 使用Mybatis的开发者,大多数都会遇到一个问题,就是要写大量的SQL在xml文件中,除了特殊的业务逻辑SQL之外,还有大量结构类似的增删改查SQ ...

  3. Spring Ldap 的增删改查

    package ldap.entity; /** * 本测试类person对象来自schema文件的core.schema文件 * objectClass为person,必填属性和可选属性也是根据该对 ...

  4. Spring JPA实现增删改查

    1. 创建一个Spring工程 2.配置application文件 spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver spri ...

  5. 关于单链表的增删改查方法的递归实现(JAVA语言实现)

    因为在学习数据结构,准备把java的集合框架底层源码,好好的过一遍,所以先按照自己的想法把单链表的类给写出来了; 写该类的目的: 1.练习递归 2.为深入理解java集合框架底层源码打好基础 学习的视 ...

  6. idea社区版+第一个spring boot项目+增删改查+yml修改端口号

    参考:https://www.cnblogs.com/tanlei-sxs/p/9855071.html 中途出现问题时参考了太多 1.下载idea社区版 2.在settings -> Plug ...

  7. PHP历程(封装的增删改查方法)

    db.class.php   主要方法 <?php /** * 数据库配置信息 */ define('DB_HOST','127.0.0.1'); //服务器 define('DB_USER', ...

  8. Hibernate常用增删改查方法

    /** * @param obj * @return * 添加数据 */ public Serializable saveObject(Object obj){ return this.getHibe ...

  9. Spring Boot WebFlux 增删改查完整实战 demo

    03:WebFlux Web CRUD 实践 前言 上一篇基于功能性端点去创建一个简单服务,实现了 Hello .这一篇用 Spring Boot WebFlux 的注解控制层技术创建一个 CRUD ...

随机推荐

  1. 本系列love2d示例代码错误集中整理

    3.输入和音乐 音乐不是循环播放的,可以在love.audio.play(music) 之前添加music:setLooping(true)

  2. Python中import和from import

    Python里面的import和from import都是用于导入一个模块,两者的区别是 如果你在使用某模块内函数时不想写模块名,那么就用from import方式导入,如果是用import方式就要写 ...

  3. 因客户机IP与服务器IP不在同一网段导致无盘客户机开机卡tftp,提示:PXE-E11: ARP timeout

    61的地址需要在上面的地址范围段之内 问题现象] 无盘客户机启动获取DHCP后卡在tftp界面提示:PXE-E11: ARP timeout,如下图: [原因说明] 客户机的IP地址与服务器IP地址不 ...

  4. Golang HTTP文件上传

    2018年02月08日 10:07:13 冷月醉雪 阅读数:346 标签: golangHTTP文件上传更多 个人分类: Go   版权声明:本文为博主原创文章,未经博主允许不得转载. https:/ ...

  5. 002杰信-陌生的maven-web项目整改成我们熟悉的Web架构;classpath的含义;ssm框架的整合;junit测试

    这篇博客的资源来源于创智播客,先在此申明.这篇博客的出发点是jk项目,传智的做法是Maven的web模板生成的,但是这样子的结构目录与我们熟知的Web项目的结构目录相差很大,所以要按照我们熟知的项目结 ...

  6. 【BZOJ】1093: [ZJOI2007]最大半连通子图(tarjan+拓扑序)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1093 两个条件综合起来加上求最大的节点数,那么很明显如果是环一定要缩点. 然后再仔细思考下就是求da ...

  7. 【BZOJ】1630: [Usaco2007 Demo]Ant Counting(裸dp/dp/生成函数)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1630 题意,给你n种数,数量为m个,求所有的数组成的集合选长度l-r的个数 后两者待会写.. 裸dp ...

  8. Kivy: 用于NUI开发的跨平台Python框架

    http://top.jobbole.com/9915/ http://kivy.org/

  9. Request获取具有相同 name 属性表单元素值

    html代码如: <input name="txtName" id="txtFirstName" type="text" /> ...

  10. 全新的membership框架Asp.net Identity——绕不过的Claims

    http://www.cnblogs.com/JustRun1983/p/4708176.html?utm_source=tuicool&utm_medium=referral