Error:

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-10-30 11:18:52.721 ERROR 16868 --- [main] o.s.boot.SpringApplication : Application startup failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jobDetailServiceImpl': Unsatisfied dependency expressed through field 'jobDetailRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobDetailRepository': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Method must not have Pageable *and* Sort parameter. Use sorting capabilities on Pageable instead! Offending method: public abstract org.springframework.data.domain.Page com.hujiang.career.bank.data.analysis.repository.JobDetailRepository.findAll(org.springframework.data.domain.Pageable,org.springframework.data.domain.Sort)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at com.hujiang.career.bank.data.analysis.DataAnalysisApplication.main(DataAnalysisApplication.java:13) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobDetailRepository': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Method must not have Pageable *and* Sort parameter. Use sorting capabilities on Pageable instead! Offending method: public abstract org.springframework.data.domain.Page com.hujiang.career.bank.data.analysis.repository.JobDetailRepository.findAll(org.springframework.data.domain.Pageable,org.springframework.data.domain.Sort)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
... 19 common frames omitted
Caused by: java.lang.IllegalStateException: Method must not have Pageable *and* Sort parameter. Use sorting capabilities on Pageable instead! Offending method: public abstract org.springframework.data.domain.Page com.hujiang.career.bank.data.analysis.repository.JobDetailRepository.findAll(org.springframework.data.domain.Pageable,org.springframework.data.domain.Sort)
at org.springframework.data.repository.query.QueryMethod.<init>(QueryMethod.java:87) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryMethod.<init>(JpaQueryMethod.java:89) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:77) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:436) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:221) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
... 29 common frames omitted

解决办法:

    @GetMapping("/list")
public Page<JobDetail> list(@PageableDefault(size = 5) Pageable pageable) {
return jobDetailService.list(pageable);
}
    @Autowired
private JobDetailRepository jobDetailRepository; @Override
public Page<JobDetail> list(Pageable pageable) {
return jobDetailRepository.findAll(pageable,new Sort(Sort.Direction.DESC,"id"));
}

改成:

    @GetMapping("/list")
public Page<JobDetail> list(@PageableDefault(size = 5, sort = "id", direction = Sort.Direction.DESC) Pageable pageable) {
return jobDetailService.list(pageable);
}
    @Autowired
private JobDetailRepository jobDetailRepository; @Override
public Page<JobDetail> list(Pageable pageable) {
return jobDetailRepository.findAll(pageable);
}

原因:

A Pageable already carries a Sort. Why would you want to hand in an additional one? The other point is, that the method can't have the same return type. 
A Pageable usually results in a Page returned, which doesn't work if only a Sort is given.
Yes, there's a case for returning a List or the like for both but I'd argue that giving up the constraints opens up a source for undetected misdeclarations.
So I'd recommend to simply declare two query methods, one taking a Pageeable, one taking a Sort.

https://jira.spring.io/browse/DATAJPA-1138

Pageable 是Spring Data库中定义的一个接口,该接口是所有分页相关信息的一个抽象,通过该接口,我们可以得到和分页相关所有信息(例如pageNumber、pageSize等)。
Pageable定义了很多方法,但其核心的信息只有两个:一是分页的信息(page、size),二是排序的信息。
在springmvc的请求中只需要在方法的参数中直接定义一个pageable类型的参数,当Spring发现这个参数时,Spring会自动的根据request的参数来组装该pageable对象,
Spring支持的request参数如下:

page,第几页,从0开始,默认为第0页
size,每一页的大小,默认为20
sort,排序相关的信息,以property,property(,ASC|DESC)的方式组织,例如sort=firstname&sort=lastname,desc表示在按firstname正序排列基础上按lastname倒序排列。

这样,我们就可以通过url的参数来进行多样化、个性化的查询。
Spring data提供了@PageableDefault帮助我们个性化的设置pageable的默认配置。
例如@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC)表示默认情况下我们按照id倒序排列,每一页的大小为15。

@ResponseBody
@RequestMapping(value = "list", method=RequestMethod.GET)
public Page<blog> listByPageable(@PageableDefault(value = 15,
sort = { "id" },
direction = Sort.Direction.DESC)
Pageable pageable) {
return blogRepository.findAll(pageable);
}

http://blog.csdn.net/zsg88/article/details/66025560

今天给大家介绍一下SpringBoot中JPA的一些常用操作,例如:增删改查、分页、排序、事务操作等功能。
下面先来介绍一下JPA中一些常用的查询操作:

    //And --- 等价于 SQL 中的 and 关键字,比如 findByHeightAndSex(int height,char sex);
public List<User> findByHeightAndSex(int height,char sex); // Or --- 等价于 SQL 中的 or 关键字,比如 findByHeightOrSex(int height,char sex);
public List<User> findByHeightOrSex(int height,char sex); //Between --- 等价于 SQL 中的 between 关键字,比如 findByHeightBetween(int min, int max);
public List<User> findByHeightBetween(int min,int max); //LessThan --- 等价于 SQL 中的 "<",比如 findByHeightLessThan(int max);
public List<User> findByHeightLessThan(int max); //GreaterThan --- 等价于 SQL 中的">",比如 findByHeightGreaterThan(int min);
public List<User> findByHeightGreaterThan(int min); //IsNull --- 等价于 SQL 中的 "is null",比如 findByNameIsNull();
public List<User> findByNameIsNull(); //IsNotNull --- 等价于 SQL 中的 "is not null",比如 findByNameIsNotNull();
public List<User> findByNameIsNotNull(); //NotNull --- 与 IsNotNull 等价;
public List<User> findByNameNotNull(); //Like --- 等价于 SQL 中的 "like",比如 findByNameLike(String name);
public List<User> findByNameLike(String name); //NotLike --- 等价于 SQL 中的 "not like",比如 findByNameNotLike(String name);
public List<User> findByNameNotLike(String name); //OrderBy --- 等价于 SQL 中的 "order by",比如 findByNameNotNullOrderByHeightAsc();
public List<User>findByNameNotNullOrderByHeightAsc(); //Not --- 等价于 SQL 中的 "! =",比如 findByNameNot(String name);
public List<User> findByNameNot(String name); //In --- 等价于 SQL 中的 "in",比如 findByNameIN(String name);
public List<User> findByNameIn(String name); //NotIn --- 等价于 SQL 中的 "not in",比如 findByNameNotIN(String name);
public List<User> findByNameNotIn(String name);

JPA中的风格就是这样,每个方法其实都是一条SQl命令,通过一些关键字就可以实现SQL中类似于like in等等之类的命令了。

最重要的是我们再开发的过程中,只需要编写dao中一个个方法,不需要我们编写dao的实现类,这样就可以大大的挺高代码的复用率、提高我们的开发效率。

说道这里不免会有人会问,那一些比较复杂的关联查询要怎么实现呢,JPA的处理方法是:利用原生的SQl命令来实现那些复杂的关联查询,下面就来看下案例。

 //利用原生的SQL进行查询操作
@Query(value = "select o.* from orders o ,user u where o.uid=u.id and u.name=?1", nativeQuery = true)
@Modifying
public List<Order> findOrderByName(String name); //利用原生的SQL进行删除操作
@Query(value = "delete from orders where id=?1 ", nativeQuery = true)
@Modifying
public void deleteOrderById(int id); //利用原生的SQL进行删除操作
@Query(value = "delete from orders where uid=?1 ", nativeQuery = true)
@Modifying
public void deleteOrderByUId(int uid); //利用原生的SQL进行修改操作
@Query(value = "update orders set name=?1 where id=?2 ", nativeQuery = true)
@Modifying
public void updateOrderName(String name,int id); //利用原生的SQL进行插入操作
@Query(value = "insert into orders(name,uid) value(?1,?2)", nativeQuery = true)
@Modifying
public void insertOrder(String name,int uid);

上面的案例中给出了,利用JPA实现原生的SQL操作,可以很方便的进行数据库表的操作。
所以如果是那种查询语句不是非常复杂,对查询时间要求不是特别苛刻的项目,完全可以采用JPA来进行项目的开发。

下面接着来介绍JPA是怎么实现分页的效果,其实JPA脱胎于hibernate,所以本身就对分页功能有很好的支持。下面给出具体例子:

    //实现分页功能
Page<User> findByNameNot(String name,Pageable pageable);
 @RequestMapping(value = "/params", method= RequestMethod.GET)
@ResponseBody
public String getEntryByParams(@RequestParam(value = "name", defaultValue = "林志强") String name, @RequestParam(value = "page", defaultValue = "0") Integer page, @RequestParam(value = "size", defaultValue = "15") Integer size) {
Sort sort = new Sort(Sort.Direction.DESC, "id");
Pageable pageable = new PageRequest(page, size, sort);
Page<User> pages=userDao.findByNameNot(name,pageable);
Iterator<User> it=pages.iterator();
while(it.hasNext()){
System.out.println("value:"+((User)it.next()).getId());
}
return "success...login....";
}

上面的代码一个是在dao层中的,一个是在controller中的。
dao层中添加一个返回值为Page,参数值为Pageable。controller层中通过实例化Pageable这个类,然后调用dao层这个分页方法。

通过这些步骤就可以轻轻松松的实现分页的效果啦,看起来是不是特别方便。

最后在给大家介绍一下JPA是如何实现事务操作的。其实因为SpringBoot中已经对事务做了很好的封装了,使用起来特别方便。下面看一下案例:

    @RequestMapping("/saveOrder")
@ResponseBody
@Transactional()
public String saveOrder(){
Order o1=new Order("11",2);
Order o2=new Order("22",2);
Order o3=new Order("33",2);
Order o4=new Order("44",2);
orderDao.save(o1);
orderDao.save(o2);
orderDao.save(o3);
orderDao.save(o4);
return "successfull....saveOrder......";
}

只要在方法的上面加上@Transaction 这个注解就可以轻轻松松的实现事务的操作了,是不是特别方便啊。
不过这里有几点需要注意的是:

1.这个注解实现的事务管理器是默认的,如果不想要默认是事务管理器,可以自己进行添加,我这里就不多介绍了。

2.事务的隔离级别也是可以自己设置的,具体请看这篇博客 点击打开链接

3.事务的传播行为也是可以自己设置的,具体请看这篇博客点击打开链接

到此到此关于JPA的一些常规方法就介绍完了,谢谢大家的阅读。

http://blog.csdn.net/linzhiqiang0316/article/details/52639265

SpringBoot JPA 专题的更多相关文章

  1. 补习系列(19)-springboot JPA + PostGreSQL

    目录 SpringBoot 整合 PostGreSQL 一.PostGreSQL简介 二.关于 SpringDataJPA 三.整合 PostGreSQL A. 依赖包 B. 配置文件 C. 模型定义 ...

  2. 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础授权权限

    上一篇<[原]无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限>介绍了实现Shiro的基础认证.本篇谈谈实现 ...

  3. 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限

    开发环境搭建参见<[原]无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页> 需求: ① 除了登录页面,在地址栏直接访问其他 ...

  4. 带着新人学springboot的应用08(springboot+jpa的整合)

    这一节的内容比较简单,是springboot和jpa的简单整合,jpa默认使用hibernate,所以本质就是springboot和hibernate的整合. 说实话,听别人都说spring data ...

  5. springboot+jpa+mysql+redis+swagger整合步骤

    springboot+jpa+MySQL+swagger框架搭建好之上再整合redis: 在电脑上先安装redis: 一.在pom.xml中引入redis 二.在application.yml里配置r ...

  6. springboot+jpa+mysql+swagger整合

    Springboot+jpa+MySQL+swagger整合 创建一个springboot web项目 <dependencies> <dependency>      < ...

  7. SpringBoot JPA + H2增删改查示例

    下面的例子是基于SpringBoot JPA以及H2数据库来实现的,下面就开始搭建项目吧. 首先看下项目的整体结构: 具体操作步骤: 打开IDEA,创建一个新的Spring Initializr项目, ...

  8. SpringBoot Jpa入门案例

    版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons) 我们先来了解一下是什么是springboot jpa,springboo ...

  9. SpringBoot JPA懒加载异常 - com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy

    问题与分析 某日忽然发现在用postman测试数据时报错如下: com.fasterxml.jackson.databind.JsonMappingException: could not initi ...

随机推荐

  1. h5背景

    1.背景属性复习: background-image background-color background-repeat background-position background-attachm ...

  2. php网页跳转无法获取session值

    今日编写项目,需要在跳转后的页面获取session值进行自动登录操作,但是明明在传输页面可以打印出session值,但在接受页面却显示session值为空,经确认脚本中的session_start() ...

  3. Docker 部署Dotnet Core MVC项目

    原文:Docker 部署Dotnet Core MVC项目 1.dotnet core创建项目 dotnet new mvc -o myweb cd myweb 然后就是业务代码的编辑,增删改查乱七八 ...

  4. Maven搭建Spring Security3.2项目详解

    本来是打算在上一篇SpringMVC+Hibernate上写的,结果发现上面那篇 一起整合的,结果发现上一篇内容实在是太长了,就另起一篇,这篇主要是采用 Maven搭建Spring+SpringMVC ...

  5. Stack overflow 编译能通过,运行时出现Stack overflow

    Stack overflow 编译能通过,运行时出现Stack overflow 大家都知道,Windows程序的内存机制大概是这样的,全局变量(局部的静态变量本质也属于此范围)存储于堆内存,该段内存 ...

  6. 学习笔记:Vue——混入

    前言: 到现在用Vue做了不少项目了,用到的都是初阶的功能,很多高阶能力都没有用到.仅用初级阶段也能做项目,甚至是复杂项目,可见vue之强大,果然是渐进式开发方式. 但是本着虚心学习的态度,还是要抽空 ...

  7. 【习题 3-7 UVA - 1368 】DNA Consensus String

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举每一位字母是什么. 从小到大枚举. 然后计算每一位的总贡献是多少. 取最小的那个输出. [代码] #include <bi ...

  8. 【CS Round #48 (Div. 2 only)】Dominant Free Sets

    [链接]h在这里写链接 [题意] 让你在n个点组成的集合里面选取不为空的集合s. 使得这里面的点没有出现某个点a和b,ax>=bx且ay>=by; 问你s的个数. [题解] 我们把这些点按 ...

  9. DB2学习总结(1)——DB2数据库基础入门

    DB2的特性 完全Web使能的:可以利用HTTP来发送询问给服务器. 高度可缩放和可靠:高负荷时可利用多处理器和大内存,可以跨服务器地分布数据库和数据负荷:能够以最小的数据丢失快速地恢复,提供多种备份 ...

  10. python的list和数组的区别

    list不是数组(额外安装Pynum) 1)可修改,list数据结构内容可以被程序修改 2)可动态增减,长度不固定 3)list里面的数据项可以是不同类型数据,也可以是list 4)两个list可“链 ...