🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ package net.youworker.domain; import net.youworker.repository.CustomerRepository; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Example; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Optional; @RunWith(SpringRunner.class) @SpringBootTest public class CustomerTest { @Autowired private CustomerRepository customerRepository; /** * 查询全部 */ @Test public void testFindAll() { List<Customer> customerList = customerRepository.findAll(); for (Customer customer : customerList) { System.out.println(customer); System.out.println(customer.getCustName()); } } /** * 根据id查询 */ @Test public void testFindById() { Optional<Customer> customer = Optional.ofNullable(customerRepository.findById(6l).orElse(null)); System.out.println(customer.get()); System.out.println(customer.get().getCustName()); } /** * 保存或者更新 * 根据传递的对象是否存在主键id * 如果没有Id主键属性:保存 * 存在id主键属性:根据id查询数据,更新数据 */ @Test public void testSave() { try { Customer customer = new Customer(); customer.setCustName("大脚"); customer.setCustPhone("1955455"); customer.setCustAddress("sdsdssd"); customer.setCustIndustry("sdsds"); customer.setCustSource("来源"); customer.setCustLevel("55"); Customer save = customerRepository.save(customer); System.out.println("customer1"); System.out.println(save); } catch (Exception e) { System.out.println(e.getStackTrace()); System.out.println(e.getMessage()); } } /** * 更新 */ @Test public void update() { Customer customer = new Customer(); customer.setCustId(2l); customer.setCustIndustry("大专"); customer.setCustAddress("铁岭"); customer.setCustLevel("555"); customer.setCustPhone("199998855"); customer.setCustSource("来源..."); customer.setCustName("大脚脚"); Customer save = customerRepository.save(customer); System.out.println("更新"); System.out.println(save); } /** * 删除 */ @Test public void delete() { try { Customer customer = new Customer(); customer.setCustId(2l); customerRepository.delete(customer); // customerRepository.deleteById(1l); System.out.println("删除成功!!"); } catch (Exception e) { System.out.println(e.getMessage()); } } //复杂查询 /** * 统计 */ @Test public void testCount() { long count = customerRepository.count(); System.out.println("count"); System.out.println(count); } /** * 查询是否存在 */ @Test public void testExists() { boolean exists = customerRepository.existsById(2l); System.out.println("id为2的客户是否存在:" + exists); } /** * 根据id从数据库查询 * * @Transactional : 保证getOne正常运行 * findOne: * em.find() * getOne: * em.getRefrence * 延时加载(懒加载) getOne */ @Test @Transactional public void testGetOne() { // Customer customer = customerRepository.getOne(3l); // System.out.println(customer); Customer customer = new Customer(); customer.setCustId(3l); Example<Customer> example = Example.of(customer); Optional<Customer> optionalCustomer = customerRepository.findOne(example); System.out.println(optionalCustomer.get().getCustName()); } } ~~~