Spring Data CrudRepository增删改查方法(八)

 

  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();
    }
}

测试结果

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

  

 

转载于:https://www.cnblogs.com/fzng/p/7256757.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值