标签:mybatis 事务 需要 思想 use 总结 uil 目的 排序
一、JPA概述及基本操作
1、ORM思想
2、JPA的介绍
3、CRUD入门案例
4、常用操作
5、jpql查询
二、Spring Data JPA
1、概述
2、快速入门
3、Spring Data JPA查询
三、动态查询与多表操作
1、Specification动态查询
Specification<Customer> spec = new Specification<Customer>() { @Override public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) { return null; }};
//多条件查询 Predicate p1 = criteriaBuilder.equal(custName, "传智播客"); Predicate p2 = criteriaBuilder.equal(custIndustry, "IT教育"); Predicate and = criteriaBuilder.and(p1, p2); //以与的形式拼接多个查询条件 //模糊匹配 Path<Object> custName = root.get("custName"); //查询方式:模糊匹配 Predicate like = criteriaBuilder.like(custName.as(String.class), "传智播客%"); //排序 Sort sort = new Sort(Sort.Direction.DESC,"custId"); List<Customer> all = customerDao.findAll(spec,sort); //分页 Pageable pageable = new PageRequest(0,2); Page<Customer> page = customerDao.findAll(null, pageable); System.out.println(page.getContent()); //得到数据集合列表 System.out.println(page.getTotalElements()); //得到总条数 System.out.println(page.getTotalPages()); //得到总页数
2、多表操作
3、一对多操作(客户与联系人)
@OneToMany(mappedBy = "customer",cascade = CascadeType.ALL) private Set<LinkMan> linkMans = new HashSet<>();
4、多对多操作(用户和角色)
@ManyToMany(targetEntity = Role.class) @JoinTable(name = "sys_role_user", //joinColumns,当前对象在中间表中的外键 joinColumns = {@JoinColumn(name = "sys_user_id",referencedColumnName = "user_id")}, //inverseJoinColumns,对方对象在中间表的外键 inverseJoinColumns = {@JoinColumn(name = "sys_role_id",referencedColumnName = "role_id")} ) private Set<Role> roles= new HashSet<>();
@ManyToMany(mappedBy = "roles") //对方配置的属性名称 private Set<User> users = new HashSet<>();
5、对象导航查询
/* fetch配置关联对象的加载方式 * EAGER:立即加载 * LAZY:延迟加载 */ @OneToMany(mappedBy = "customer",cascade = CascadeType.ALL,fetch = FetchType.EAGER) private Set<LinkMan> linkMans = new HashSet<>();
标签:mybatis 事务 需要 思想 use 总结 uil 目的 排序
原文地址:https://www.cnblogs.com/liujinhui/p/14624547.html