SpringDataJPA는 인터페이스(프록시)를 통해 간단한 CRUD를 구현합니다.
2718 단어 장 레이블: SpringData
우선 Dao층에서 JpaRepository, JpaSpecificationExecutor 인터페이스를 실현해야 한다
/*
* SpringDatajpa dao
*JpaRepository
* CRUD
JpaSpecificationExecutor
* ( )
* */
public interface CustomerDao extends JpaRepository, JpaSpecificationExecutor
{
}
다음은 CRUD와 몇 가지 간단한 방법에 대한 테스트입니다.
@RunWith(SpringJUnit4ClassRunner.class)// spring
@ContextConfiguration(locations = "classpath:applicationContext.xml")// spring
public class CustomerDaoTest {
@Autowired
private CustomerDao customerDao;
//
/*
*
* id ( )
* */
@Test
public void testFindOne()
{
Customer customer= customerDao.findOne(3l);
System.out.println(customer);
}
/*
* id ( )
* @Transactional: getOne
* */
@Test
@Transactional
public void testGetOne()
{
Customer customer=customerDao.getOne(4l);
System.out.println(customer);
}
//
/*
*
* save :
* id
* id :
* id , id ,
*
* */
@Test
//
public void testSave()
{
Customer customer=new Customer();
customer.setCustName(" ");
customer.setCustLevel("vip");
customer.setCustIndustry("it ");
customerDao.save(customer);
}
//
@Test
public void testUpdate()
{
Customer customer=new Customer();
//
customer= customerDao.findOne(4l);
customer.setCustId(4l);
customer.setCustName(" aa");
customerDao.save(customer);
}
//
//
@Test
public void testDelete()
{
customerDao.delete(3l);
}
}
2 조회 사용자 수(총 항목 수)
/*
*
* ,
* count:
*
* */
@Test
public void testCount()
{
long count = customerDao.count();
System.out.println(count);
}
3 이 사용자의 존재 여부를 판단한다
/*
*
* 2. id 4
* 0, false, 0, true
* */
@Test
public void testExie()
{
boolean exists= customerDao.exists(2l);
System.out.println("id 4 "+exists);
}
원리:
1.JdkDynamicAopProxy의 invoke 방법을 통해 동적 프록시 대상을 만들었습니다 2.Simple Jpa Repository에서 JPA의 조작을 봉인했다(JPA의api를 빌려 데이터베이스를 완성한 CRUD) 3.hibernate를 통해 데이터베이스 작업 완료 (jdbc 봉인)