[TOC]

# jpa


## 生成通用模板
实现自定义方法有两种方法:
1. 根据衍生规则进行实现,此种情况简单;查询方法衍生规则 http://docs.spring.io/spring-data/jpa/docs/1.10.1.RELEASE/reference/html/#repositories.query-methods.query-creation  
2. 自己编写接口 OrderRepositoryExt
    - 在接口中添加自定义方法
    - OrderRepository继承此接口
    - OrderRepositoryImpl 实现此接口,这个实现类命名规则是一定的,在OrderRepository的基础上加Impl;spring基础会探测到此种情况,注入OrderRepository就可以了


### OrderRepository
```
public interface OrderRepository extends PagingAndSortingRepository<OrderEntity, String>, QueryDslPredicateExecutor<OrderEntity>, OrderRepositoryExt {
    /**
     * 根据渠道号和订单编号查询订单
     *
     * @param orderNo
     * @param chanNo
     * @return
     */
    public OrderEntity findFirstByOrderNoAndChanNo(String orderNo, String chanNo);
}
```
### OrderRepositoryExt
```
public interface OrderRepositoryExt {
 
    public OrderEntity findByOrderNo(String orderNo);
}
```

### OrderRepositoryImpl
```
public class OrderRepositoryImpl implements OrderRepositoryExt {
 
    @Autowired
    private OrderRepository orderRepository;
 
    @Override
    public OrderEntity findByOrderNo(String orderNo) {
        QOrderEntity qOrderEntity = QOrderEntity.orderEntity;
        Predicate predicate = qOrderEntity.orderNo.eq(orderNo);
        return orderRepository.findOne(predicate);
    }
}
```
### 使用方法
```
@RestController
@RequestMapping("/test")
public class OrderController {
    private static Logger log = LoggerFactory.getLogger(OrderController.class);
 
    @Autowired
    private OrderRepository repository;
 
 
    @RequestMapping("/orders")
    public Object order() {
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setId(UUID.randomUUID().toString());
        orderEntity.setAccountId("111");
        orderEntity.setAmount(new BigDecimal(10));
        orderEntity.setChanNo("1111");
        orderEntity.setCreateAt(new Date());
        orderEntity.setOrderNo(UUID.randomUUID().toString());
        orderEntity.setOrderStatus("execed_success");
        orderEntity.setOrderType("INVEST");
        orderEntity.setProductId("2222");
        orderEntity.setTransactTime(new Date());
        orderEntity.setProductName("12312");
        orderEntity.setUpdateAt(new Date());
        repository.save(orderEntity);
 
        log.info("save-{}", JSONUtil.toJson(repository.findFirstByOrderNoAndChanNo(orderEntity.getOrderNo(), orderEntity.getChanNo())));
 
        orderEntity.setProductName("update");
        repository.save(orderEntity);
        log.info("update-{}", JSONUtil.toJson(repository.findByOrderNo(orderEntity.getOrderNo())));
 
        log.info("all ={}", JSONUtil.toJson(repository.findAll()));
        List<String> ids = new ArrayList<>();
        ids.add("11111");
        return null;
    }
```


## 手动编写hql查询
实体对象idea识别不出来

jpa,querydsl的更多相关文章

  1. Spring Boot JPA - Querydsl

    https://lufficc.com/blog/spring-boot-jpa-querydsl

  2. Spring Data JPA 和MyBatis比较

    现在Dao持久层的解决方案中,大部分是采用Spring Data JPA或MyBatis解决方案,并且传统企业多用前者,互联网企业多用后者. Spring Data JPA 是Spring Data ...

  3. Spring data jpa 复杂动态查询方式总结

    一.Spring data jpa 简介 首先我并不推荐使用jpa作为ORM框架,毕竟对于负责查询的时候还是不太灵活,还是建议使用mybatis,自己写sql比较好.但是如果公司用这个就没办法了,可以 ...

  4. JPQ整合Querydsl入门篇

    # JPQ整合Querydsl入门篇  不知道你们喜不喜欢用JPA ,我本人是很喜欢 不要和我说JPA不适合复杂查询等等的,你要知道现在都是微服务,只要你服务器拆分够细表设计够合理,都是服务之间调能用 ...

  5. 为什么放弃Hibernate、JPA、Mybatis,最终选择JDBCTemplate

    一.前言 因为项目需要选择数据持久化框架,看了一下主要几个流行的和不流行的框架,对于复杂业务系统,最终的结论是,JOOQ是总体上最好的,可惜不是完全免费,最终选择JDBC Template. Hibe ...

  6. Spring Boot (七): Mybatis极简配置

    Spring Boot (七): Mybatis极简配置 1. 前言 ORM 框架的目的是简化编程中的数据库操作,经过这么多年的发展,基本上活到现在的就剩下两家了,一个是宣称可以不用写 SQL 的 H ...

  7. Spring Data JPA进阶——Specifications和Querydsl

    Spring Data JPA进阶--Specifications和Querydsl 本篇介绍一下spring Data JPA中能为数据访问程序的开发带来更多便利的特性,我们知道,Spring Da ...

  8. Spring Data JPA教程, 第五部分: Querydsl(未翻译)

    The fourth part of my Spring Data JPA tutorialdescribed how you can implement more advanced queries ...

  9. 如何在Spring Data JPA中引入Querydsl

    一.环境说明 基础框架采用Spring Boot.Spring Data JPA.Hibernate.在动态查询中,有一种方式是采用Querydsl的方式. 二.具体配置 1.在pom.xml中,引入 ...

随机推荐

  1. 运行tomcat6w.exe ,提示 指定的服务未安装 unable to open the service 'tomcat6'

    错误:运行tomcat6w.exe ,提示 指定的服务未安装 unable to open the service 'tomcat6'(我用的是官网下载的解压版) 解决方法: 打开命令行提示符窗口=& ...

  2. PR 修改保存的增强 ME_UPDATE_REQUISITION

    FUNCTION me_update_requisition."""""""""""&qu ...

  3. YTU 2905: The Sum of 1...N

    2905: The Sum of 1...N 时间限制: 1 Sec  内存限制: 128 MB 提交: 281  解决: 51 题目描述 Given an integer n,your task i ...

  4. DWR(Direct Web Remoting)是什么

    DWR可以用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助你开发包含AJAX技术的网站.它可以允许在浏览器里的代码使用运行在WEB服务器上的JAVA函数,就像它就在浏览器里一 ...

  5. sphinx 针对tedfield搜索

    query = "(user can be admin)"  -> check all fields for the given words. If all words ar ...

  6. ios app 上架AppStore

    一.证书的导出      1.1 前期工作        首先你需要有一个苹果的开发者帐号,一个Mac系统.        如果没有帐号可以在打开http://developer.apple.com/ ...

  7. ubuntu搭建开发环境踩坑实录

    谨以此文,记录和ubuntu系统不死不休的搏斗过程,后续待补. 1.双系统安装,windows采用uefi模式安装(优启通可制作uefi的win10安装盘),ubuntu不要划分boot区,而应该划分 ...

  8. java日期和时间戳格式互转

    // 将日期格式转换成时间戳 public static void main(String[] args) throws Exception{ String time = "2018-05- ...

  9. UVa 1471 Defense Lines (二分+set优化)

    题意:给定一个序列,然后让你删除一段连续的序列,使得剩下的序列中连续递增子序列最长. 析:如果暴力枚举那么时间复杂度肯定受不了,我们可以先进行预处理,f[i] 表示以 i 结尾的连续最长序列,g[i] ...

  10. Gym 100531B Buffcraft (贪心+暴力+前缀和)

    题意:给定两个加血的方式,一个是直接加多少,另一种是加百分之几,然后你能够你选 k 种,问你选哪 k 种. 析:首先肯定要选加的多的,所以我们先排序,从大到小,然后用前缀和存储一下,再去枚举从第一种和 ...