springDataJpa学习笔记

目录

前言

ORM框架,会大大减少重复性代码,开发速度快;

底层使用了hibernate;

springData

准备

引用

``` org.springframework spring-context 5.0.2.RELEASE org.springframework spring-tx 5.0.2.RELEASE org.aspectj aspectjweaver 1.8.7 org.springframework spring-test 5.0.2.RELEASE mysql mysql-connector-java 5.1.6 log4j log4j 1.2.12 org.hibernate hibernate-c3p0 5.0.7.Final junit junit 4.12 org.hibernate hibernate-entitymanager 5.0.7.Final org.springframework.data spring-data-jpa 2.0.5.RELEASE org.springframework spring-orm 5.0.2.RELEASE ```

xml配置初始化JPA

applicationContext.xml ``` ```

pojo

和原生一样

dao层接口

public interface ICustomerDao extends JpaRepository<Customer,Integer>,JpaSpecificationExecutor<Customer>{
}

使用

新增、修改:save

Customer customer = new Customer();
customer.setCustName("hello Spring data JPA");
customerService.save(customer);

删除

customerService.delete(1);

查询所有

List<Customer> list = customerService.findAll();

根据ID查询findById

Customer customer = customerService.findById(1);
System.out.println(customer);

命名规则查询(条件查询)

/**
 * 根据方法的命名规则查询
 *   拓展:
 *      约定大于配置(很重要的思想)
 * @param custName
 * @param custAddress
 * @return
 */
List<Customer> findByCustNameAndCustAddressLike(StringcustName, String custAddress);

自定义JPQL查询(条件查询)

/**
 * 查询集合(带条件--JPQL)
 * @param custName
 * @param custAddress
 * @return
 */
@Query("from Customer where custName = ?1 and custAddress like ?2")
List<Customer> findAll(String custName, String custAddress);

自定义sql查询(条件查询)

/**
 * Sql
 * @param custName
 * @param custAddress
 * @return
 */
@Query(value = "select * from cst_customer where cust_name = ?1 and cust_address like ?2",nativeQuery=true)
List<Customer> findAllByNative(String custName, StringcustAddress);

自定义update、delete

/**
 * 自定更新
 * @param custId
 * @param custName
 * @param custAddress
 */
@Query("update Customer set custName = ?2,custAddress = ?3 where custId = ?1")
@Modifying
void update(Integer custId, String custName, StringcustAddress);

Specification对象查询和分页

方便拼接查询条件

查询

//创建Specification对象 select * from cst_customer where
Specification<Customer> specification = new Specification<Customer>() {
    @Override
    public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
        // 条件
        Predicate p1 = criteriaBuilder.equal(root.get("custName"), "客户2");
        Predicate p2 = criteriaBuilder.like(root.get("custAddress"), "%北%");
        return criteriaBuilder.and(p1,p2);
    }
};

List<Customer> all = service.findAll(specification);

分页

传入参数第几页,多少条!

//创建Specification对象 select * from cst_customer
Specification<Customer> specification = new Specification() {
    @Nullable
    @Override
    // 此内部方法只是负责拼接条件及参数
    public Predicate toPredicate(Root root, CriteriaQuery cq, CriteriaBuilder cb) {
        Predicate predicate = cb.and(cb.equal(root.get("custName"),"京西集团"),
                cb.like(root.get("custAddress"),"%山%"));
        return null;
    }
};

Pageable pageable = PageRequest.of(1,2);
Page<Customer> page = customerService.findAllByPage(specification, pageable);
List<Customer> list = page.getContent();

一对多和多对多查询

必须用立即加载

猜你喜欢

转载自www.cnblogs.com/birdofparadise/p/10012287.html
今日推荐