Spring SpringMVC JPA(SSJ)项目实战——环境搭建,工具准备

本文详细介绍SpringBoot智能销售系统实战,涵盖Spring、SpringMVC、JPA项目配置,包括依赖包、配置文件、SpringDataJpa应用及Jpa-spec插件使用。深入解析SpringDataJpa结构、CRUD操作、分页排序、条件查询及原生SQL运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring SpringMVC JPA项目实战

智能销售系统:项目前期准备

1.配置文件

导包

  1. utf-8编码
  2. Jdk1.8
  3. Spring支持包

     spring-core

     spring-context

     spring-context-support  

     spring-tx

     spring-jdbc

     spring-orm

     spring-test

  1. Springmvc

spring-web

spring-webmvc

spring-webmvc

  1. SpringMVC的json支持包

   jackson-core

   jackson-annotations

   jackson-databind

  1. hibernate的支持包

   hibernate-core

   hibernate-entitymanager

  1. SpringData的支持包

   spring-data-jpa

   SpringData的扩展展包:jpa-spec(把所有的依懒都去掉)

  1. 连接池,mysql

   commons-dbcp

   mysql-connector-java

还有几个支持包,以后要用到在详细解说

 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.itsource</groupId>
  <artifactId>aisell</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>aisell Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>

  <!-- UTF-8-->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  <!-- 配置jdk为1.8-->
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>

  <!-- 配置几个变量,下面可以直接掉用-->
    <org.springframework.version>4.2.5.RELEASE</org.springframework.version>
    <org.hibernate.version>4.3.8.Final</org.hibernate.version>
    <spring-data-jpa.version>1.9.0.RELEASE</spring-data-jpa.version>
    <com.fasterxml.jackson.version>2.5.0</com.fasterxml.jackson.version>
    <org.slf4j.version>1.6.1</org.slf4j.version>
  </properties>
  <dependencies>
    <!-- Spring的支持包 

 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${org.springframework.version}</version>
      <scope>test</scope>
    </dependency>
    <!-- 引入web前端的支持 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <!-- SpringMCV上传需要用到io包-->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-io</artifactId>
      <version>1.3.2</version>
    </dependency>
    <!-- 文件上传用到的包 -->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.2.2</version>
    </dependency>
    <!-- SpringMVC的json支持包-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>${com.fasterxml.jackson.version}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>${com.fasterxml.jackson.version}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>${com.fasterxml.jackson.version}</version>
    </dependency>
    <!-- hibernate的支持包 -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>${org.hibernate.version}</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>${org.hibernate.version}</version>
    </dependency>
    <!-- SpringData的支持包 -->
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-jpa</artifactId>
      <version>${spring-data-jpa.version}</version>
    </dependency>
    <!-- SpringData的扩展展包 -->
    <dependency>
      <groupId>com.github.wenhao</groupId>
      <artifactId>jpa-spec</artifactId>
      <version>3.1.1</version>
      <!-- 把所有的依賴都去掉 -->
      <exclusions>
        <exclusion>
          <groupId>*</groupId>
          <artifactId>*</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.2.2</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>
    </dependency>

    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.5</version>
    </dependency>
    <!-- 测试包 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <!-- 这个scope 只能作用在编译和测试时,同时没有传递性。表示在运行的时候不添加此jar文件 -->
      <scope>provided</scope>
    </dependency>
    <!-- 日志文件 -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${org.slf4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${org.slf4j.version}</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.14</version>
    </dependency>
    <!-- 代码生成器模版技术 -->
    <dependency>
      <groupId>org.apache.velocity</groupId>
      <artifactId>velocity</artifactId>
      <version>1.6</version>
    </dependency>
    <!-- shiro的支持包 -->
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-all</artifactId>
      <version>1.4.0</version>
      <type>pom</type>
    </dependency>
    <!-- shiro与Spring的集成包 -->
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-spring</artifactId>
      <version>1.4.0</version>
    </dependency>
    <!-- poi支持的jar包 -->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.11</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>3.11</version>
    </dependency>
    <!-- 图片压缩功能 -->
    <!-- 缩略图 -->
    <dependency>
      <groupId>net.coobird</groupId>
      <artifactId>thumbnailator</artifactId>
      <version>0.4.6</version>
    </dependency>
    <!-- 定时调度 -->
    <dependency>
      <groupId>quartz</groupId>
      <artifactId>quartz</artifactId>
      <version>1.5.2</version>
    </dependency>
    <!-- 邮件支持 -->
    <dependency>
      <groupId>javax.mail</groupId>
      <artifactId>mail</artifactId>
      <version>1.4.1</version>
    </dependency>
    <!--  jpa的SpecificationSpecification功能封装 -->
    <dependency>
      <groupId>com.github.wenhao</groupId>
      <artifactId>jpa-spec</artifactId>
      <version>3.1.1</version>
      <!-- 把所有依都过滤 -->
      <exclusions>
        <exclusion>
          <groupId>*</groupId>
          <artifactId>*</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

  </dependencies>

  <build>
    <finalName>aisell</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

配置文件

  1. Spring的配置文件
  1. 扫描包
  2. 配置连接池
  3. 集成hibernate的jpa功能
  4. Jpa 事务配置(注解声明式事务管理)
  5. Spring Data Jpa配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx.xsd
         http://www.springframework.org/schema/data/jpa
         http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <!-- 扫描service部分的包 -->
    <context:component-scan base-package="cn.itsource.aisell.service"/>
    <!-- 拿到连接数据库的数据 -->
    <context:property-placeholder location="classpath:jdbc.properties" />

    <!-- 配置连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <!--连接数据4个属性 -->
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <!--maxActive: 最大连接数量 -->
        <property name="maxActive" value="150" />
        <!--minIdle: 最小空闲连接 -->
        <property name="minIdle" value="5" />
        <!--maxIdle: 最大空闲连接 -->
        <property name="maxIdle" value="20" />
        <!--initialSize: 初始化连接 -->
        <property name="initialSize" value="30" />
        <!-- 用来配置数据库断开后自动连接的 -->
        <!-- 连接被泄露时是否打印 -->
        <property name="logAbandoned" value="true" />
        <!--removeAbandoned: 是否自动回收超时连接 -->
        <property name="removeAbandoned" value="true" />
        <!--removeAbandonedTimeout: 超时时间(以秒数为单位) -->
        <property name="removeAbandonedTimeout" value="10" />
        <!--maxWait: 超时等待时间以毫秒为单位 1000等于60秒 -->
        <property name="maxWait" value="1000" />
        <!-- 在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位. -->
        <property name="timeBetweenEvictionRunsMillis" value="10000" />
        <!-- 在每次空闲连接回收器线程(如果有)运行时检查的连接数量 -->
        <property name="numTestsPerEvictionRun" value="10" />
        <!-- 1000 * 60 * 30 连接在池中保持空闲而不被空闲连接回收器线程 -->
        <property name="minEvictableIdleTimeMillis" value="10000" />
        <property name="validationQuery" value="SELECT NOW() FROM DUAL" />
    </bean>

    <!-- 集成hibernate的jpa功能 -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!--待扫描的实体类包,不再需要persistence.xml了 -->
        <property name="packagesToScan" value="cn.itsource.aisell.domain" />
        <!-- 3.配置JPA的实现 -->
        <!-- private JpaVendorAdapter jpaVendorAdapter; -->
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <!-- org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter -->
                <!-- private boolean showSql = false;是否显示sql语句 -->
                <property name="showSql" value="true" />
                <!-- private boolean generateDdl = false;是否建表 -->
                <property name="generateDdl" value="false" />
                <!-- private String databasePlatform;原来方言 -->
                <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
            </bean>
        </property>
    </bean>

    <!-- Jpa 事务配置 -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <!-- 注解声明式事务管理 -->
    <tx:annotation-driven />

    <!-- Spring Data Jpa配置 -->
    <!-- base-package:扫描的包 -->
    <jpa:repositories base-package="cn.itsource.aisell.repository"
                      transaction-manager-ref="transactionManager"
                      entity-manager-factory-ref="entityManagerFactory"
                      factory-class="cn.itsource.aisell.repository.BaseRepositoryFactoryBean"/>
</beans>

 

  1. SpringMVc配置
  1. 静态资源放行
  2. 扫描包
  3. SpringMVC配置
  4. 视图解析器
  5. 文件上传下载

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc.xsd
    ">
            <!-- 1.静态资源进行放行 -->
            <mvc:default-servlet-handler />
            <!-- 2.扫描包-->
            <context:component-scan base-package="cn.itsource.aisell" />
                <!-- Spring MVC 配置 -->
                <mvc:annotation-driven>
                    <mvc:message-converters>
                        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                            <property name="supportedMediaTypes">
                                <list>
                                    <value>application/json; charset=UTF-8</value>
                                    <value>application/x-www-form-urlencoded; charset=UTF-8</value>
                                </list>
                            </property>
                            <!-- No serializer:配置 objectMapper 为我们自定义扩展后的 CustomMapper,解决了返回对象有关系对象的报错问题 -->
                            <property name="objectMapper">
                                <bean class="cn.itsource.aisell.common.CustomMapper"></bean>
                            </property>
                        </bean>
                    </mvc:message-converters>
                </mvc:annotation-driven>

            <!-- 视图解析器-->
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix" value="/WEB-INF/views/" />
                <property name="suffix" value=".jsp" />
            </bean>
            <!--文件上传下载-->
            <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
                <property name="maxUploadSize">
                    <value>1048576</value>
                </property>
            </bean>
</beans>

web.xml配置

  1. 读取SpringMvc
  2. 启动Spring的监听器
  3. 解决中文乱码问题
  4. 配置核心控制器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         id="WebApp_ID" version="3.1">

  <!-- 读取SpringMVC -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!-- 启动Spring的监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!--  配置解决中文乱码的问题 -->
  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!-- 配置核心控制器-->
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <!-- 告诉SpringMVC到哪里去找配置文件 -->
      <param-name>contextConfigLocation</param-name>
      <!-- 注意:这里只读取springmvc的xml -->
      <param-value>classpath:applicationContext-mvc.xml</param-value>
    </init-param>
    <!-- Servlet默认在每一次访问的时候创建 -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!-- 加上OpenEntityManager -->
  <filter>
    <filter-name>openEntity</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>openEntity</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

SpringDataJpa

ORM框架

MyBatis:Apache的一个开源项目(咱们第三个项目使用的框架),上手简单,开发灵活,现在市场占有率高; 但是开发时工作量比较大,需要做很多配置,并且自己要写SQL语句。

Hibernate:一个开放源代码的对象关系映射框架,是完全面向对象的。可以使用面向对象的思路来完成数据库操作。 Hibernate是完备的ORM框架(咱们之前学习JPA,底层就是使用Hibernate完成的)。

​​​​​​​SpringDataJpa

它是JPA规范的再次封装抽象,底层还是使用了Hibernate的JPA技术实现,引用JPQL的查询语句 ,是属于Spring的生成体系中的一部分。

Spring Data项目是从2010年开发发展起来的,是一个用于简化数据库访问,并支持云服务的开源框架。其主要目标是使得对数据的访问变得方便快捷,并支持map-reduce框架和云计算数据服务。 Spring Data 包含多个子项目:

Commons - 提供共享的基础框架,适合各个子项目使用,支持跨数据库持久化

JPA - 简化创建 JPA 数据访问层和跨存储的持久层功能

Hadoop - 基于 Spring 的 Hadoop 作业配置和一个 POJO 编程模型的 MapReduce 作业

Key-Value  - 集成了 Redis 和 Riak ,提供多个常用场景下的简单封装

Document - 集成文档数据库:CouchDB 和 MongoDB 并提供基本的配置映射和资料库支持

Graph - 集成 Neo4j 提供强大的基于 POJO 的编程模型

Graph Roo AddOn - Roo support for Neo4j

JDBC Extensions - 支持 Oracle RAD、高级队列和高级数据类型

Mapping - 基于 Grails 的提供对象映射框架,支持不同的数据库

Examples - 示例程序、文档和图数据库

Guidance - 高级文档

​​​​​​​Spring DataJPA结构

Repository(org.springframework.data.repository.Repository)

为所有实现,创建一个子类

该接口是一个标识接口,也是Spring dataRepository的核心接口,这个接口需要将领域类以及领域类ID的类型做为参数,主要的作用如下:

 

1.      能够让spring的基础设置识别出用户定义的所有Spring Data Repository。

 

2.      大幅简化了各种持久化存储的持久层实现

 

3.      捕获托管的领域类和实体的ID类型

CrudRepository(org.springframework.data.repository.CrudRepository)

     CrudRepository:完成所有基本的增删改查的方法

其中T是要操作的实体类,ID是实体类主键的类型。该接口提供了11个常用操作方法。

 

 

PagingAndSortingRepository(org.springframework.data.repository.PagingAndSortingRepository)

PagingAndSortingRepository 继承了CrudRepository接口,提供了分页与排序的功能。

   

     

     

JpaRepository (org.springframework.data.jpa.repository.JpaRepository)

  1. 进行扩展
  2. 返回值类型转化(上面的返回值都是iterable迭代。。。我们将他转化成list,是程序员更好的拿数据)

   

 

QueryByExampleExecutor(org.springframework.data.repository.query.QueryByExampleExecutor)

  

 

JpaSpecificationExecutor (org.springframework.data.jpa.repository.JpaSpecificationExecutor)

      JpaSpecificationExecutor(JPA规则执行者)是JPA2.0提供的Criteria [(批评、判断等的) 标准,准则( criterion的名词复数 )

Criteria 查询:是一种类型安全和更面向对象的查询 。]API的使用封装,可以用于动态生成Query来满足我们业务中的各种复杂场景。

Spring Data JPA为我们提供了JpaSpecificationExecutor接口,只要简单实现toPredicate方法就可以实现复杂的查询。

 

QueryDslPredicateExecutor (org.springframework.data.querydsl.QueryDslPredicateExecutor)

 

两大Repository实现类:

SimpleJpaRepository(org.springframework.data.jpa.repository.support.SimpleJpaRepository)

 

QueryDslJpaRepository(org.springframework.data.jpa.repository.support.QueryDslJpaRepository)

 

3.JpaRepository

3.1.基本CRUD

@Test
public void test01find(){
    //查询所有
    List<Employee> all = employeeRepository.findAll();
    all.forEach(employee -> System.out.println(employee));
    //查询一条数据
    Employee one = employeeRepository.findOne(1L);
    Employee one = employeeRepository.getOne(1L);//拿到的是代理对象
    //System.out.println(one);//没有web是要报异常
    System.out.println(one1);
}
@Test
public void test02save(){
    //save  update(有id,并且数据库有这条数据,就是修改)
    Employee e = new Employee();
    e.setAge(12);
    e.setEmail("1234@qq.com");
    e.setUsername("taijian");
    e.setPassword("1234567");
    employeeRepository.save(e);
}
@Test
public void test03delect(){
   employeeRepository.delete(1L);
}

 

​​​​​​​分页/排序查询

//分页
@Test
public void test04page(){
    /**
     * page:第一个参数 -> 第几页(从0开始算)
     * size:每页条数
     */
    //pageable:分页对象
    Pageable pageable = new PageRequest(13,10);
    //分页
    Page<Employee> page = employeeRepository.findAll(pageable);
    /*  page.getTotalElements(); //总条数
        page.getTotalPages(); // 总页数
        page.getContent();  //当前页的数据
        page.getNumber(); //当前在第几页
        page.getNumberOfElements(); //当前这一页有多少条数据
        page.getSize(); //每页条数
    */
    page.forEach(employee -> System.out.println(employee));
}
//排序
@Test
public void test05order(){
    /**
     *
     * new Sort(Sort.Direction.ASC,"age");
     *
     * 第一个参数:排序类型(升降)
     * 第二个参数: 根据什么字段排序
     */
    Sort sort = new Sort(Sort.Direction.ASC, "age");
    List<Employee> all = employeeRepository.findAll(sort);
    all.forEach(employee -> System.out.println(employee));

}
//分页又排序
@Test
public void test06orderAndpage(){
    //排序对象
    Sort sort = new Sort(Sort.Direction.DESC, "age");
    //分页对象
    Pageable pageable = new PageRequest(0, 5,sort);//将排序对象放到分页对象中
    Page<Employee> all = employeeRepository.findAll(pageable);
    all.forEach(employee -> System.out.println(employee));
}

 

3.3.根据条件进行查询

  按照规范创建查询方法,一般按照java驼峰式书写规范加一些特定关键字,例如我们想通过员工的名来获取到对应的员工的对象。

public interface EmployeeRepository extends JpaRepository<Employee, Long> {
    //根据名称模糊查询
    List<Employee> findByUsernameLike(String username);
    //根据名称进行查询
    List<Employee> findByUsername(String username);
}

 

规则如下:

表达式

例子

hql查询语句

And

findByLastnameAndFirstname

… where x.lastname = ?1 and x.firstname = ?2

Or

findByLastnameOrFirstname

… where x.lastname = ?1 or x.firstname = ?2

Is,Equals

findByFirstname,findByFirstnameIs,findByFirstnameEqual

… where x.firstname = 1?

Between

findByStartDateBetween

… where x.startDate between 1? and ?2

LessThan

findByAgeLessThan

… where x.age < ?1

LessThanEqual

findByAgeLessThanEqual

… where x.age ⇐ ?1

GreaterThan

findByAgeGreaterThan

… where x.age > ?1

GreaterThanEqual

findByAgeGreaterThanEqual

… where x.age >= ?1

After

findByStartDateAfter

… where x.startDate > ?1

Before

findByStartDateBefore

… where x.startDate < ?1

IsNull

findByAgeIsNull

… where x.age is null

IsNotNull,NotNull

findByAge(Is)NotNull

… where x.age not null

Like

findByFirstnameLike

… where x.firstname like ?1

NotLike

findByFirstnameNotLike

… where x.firstname not like ?1

StartingWith

findByFirstnameStartingWith

… where x.firstname like ?1 (parameter bound with appended %)

EndingWith

findByFirstnameEndingWith

… where x.firstname like ?1 (parameter bound with prepended %)

Containing

findByFirstnameContaining

… where x.firstname like ?1 (parameter bound wrapped in %)

OrderBy

findByAgeOrderByLastnameDesc

… where x.age = ?1 order by x.lastname desc

Not

findByLastnameNot

… where x.lastname <> ?1

In

findByAgeIn(Collection ages)

… where x.age in ?1

NotIn

findByAgeNotIn(Collection age)

… where x.age not in ?1

True

findByActiveTrue()

… where x.active = true

False

findByActiveFalse()

… where x.active = false

IgnoreCase

findByFirstnameIgnoreCase

… where UPPER(x.firstame) = UPPER(?1)

    1. 使用JPQL

//自己写JPQL查询
@Query("select o from Employee o where o.username=?1")
List<Employee> findByUsername(String username);
//通过顺序确定参数给谁
@Query("select o from Employee o where o.username =?1 and o.password=?2")
Employee login1(String username,String password);

//通过名称确定参数给谁
@Query("select o from Employee o where o.username =:username and o.password=:password")
Employee login2(@Param("username") String username, @Param("password") String password);

 

//自己写JPQL
@Test
public void test09(){
    List<Employee> byUsername = employeeRepository.findByUsername("admin811c5f");
    byUsername.forEach(e-> System.out.println(e));
}
@Test
public void test10(){
    Employee employee = employeeRepository.login1("admin811c5f", "123456");
    Employee employee2 = employeeRepository.login2("admin811c5f", "123456");
    System.out.println(employee);
    System.out.println(employee2);
}

​​​​​​​原生SQL

//原生sql语句
@Test
public void test11(){
    Long count = employeeRepository.getCount();
    System.out.println(count);
}

 

4.JpaSpecificationExecutor

JpaSpecificationExecutor(JPA规则执行者)是JPA2.0提供的Criteria [(批评、判断等的) 标准,准则( criterion的名词复数 )

Criteria 查询:是一种类型安全和更面向对象的查询 。]API的使用封装,可以用于动态生成Query来满足我们业务中的各种复杂场景。

Spring Data JPA为我们提供了JpaSpecificationExecutor接口,只要简单实现toPredicate方法就可以实现复杂的查询。

 @Test
    public void testFind01(){
        /**
         *官方解释:
         * Root<T> root:代表了可以查询和操作的实体对象的根,
         *              可以通过它的 Path<Y> get(String attributeName); 这个方法拿到我们要操作的字段
         *              注意:只可以拿到对应的T的字段(Employee)
         * CriteriaQuery<?> query:代表一个specific的顶层查询对象
         *              包含查询的各个部分,比如select,from,where,group by ,order by 等
         *              简单理解 就是它提供 了查询ROOT的方法(where,select,having)
         * CriteriaBuilder cb:用来构建CriteriaQuery的构建器对象(相当于条件或者说条件组合)
         *              构造好后以Predicate的形式返回
         */
/**
 * 非官方理解:
 * 查询的时候就需要给一个标准(规范)
 *  -》 根据规范(这个规范我们可以先简单理解为查询的条件)进行查询
 *
 *      Root:查询哪个表(定位到表和字段-> 用于拿到表中的字段)
 *            可以查询和操作的实体的根
 *              Root接口:代表Criteria查询的根对象,Criteria查询的查询根定义了实体类型,能为将来导航获得想要的结果,它与SQL查询中的FROM子句类似
 *             Root<Employee> 相当于 from Employee
 *             Root<Product> 相当于  from Product
 *      CriteriaQuery:查询哪些字段,排序是什么(主要是把多个查询的条件连系起来)
 *      CriteriaBuilder:字段之间是什么关系,如何生成一个查询条件,每一个查询条件都是什么方式
 *                      主要判断关系(和这个字段是相等,大于,小于like等)
 *      Predicate(Expression):单独每一条查询条件的详细描述 整个 where xxx=xx and yyy=yy ...
 */
        List<Employee> list=employeeRepository.findAll(new Specification<Employee>() {
            @Override
            public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                //获取字段
                Path username = root.get("username");
                //查询条件
                Predicate like = criteriaBuilder.like(username, "%1%");
                return like;
            }
        });
        list.forEach(employee -> System.out.println(employee));
    }
    //实现多个查询
    @Test
    public void testFindTo02(){
        /**
         * 我的理解:
         *     root:拿到要操作的字段
         *     criteriaBuilder:拿到我们要查询的条件
         *     criteriaQuery:建立关系:where order by
         */
        List<Employee> list=employeeRepository.findAll(new Specification<Employee>() {
            @Override
            public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                //拿到字段
                Path username = root.get("username");
                Path email = root.get("email");
                //查询条件
                Predicate like1 = criteriaBuilder.like(username, "%1%");
                Predicate like2 = criteriaBuilder.like(email, "%7%");

                //建立关系

                Predicate restriction = criteriaQuery.where(like1, like2).getRestriction();
                return restriction;
            }
        });
        list.forEach(employee -> System.out.println(employee));
    }
    //分页,排序,模糊查询
    @Test
    public void testFindther(){
        //获取排序对象
        Sort sort = new Sort(Sort.Direction.DESC,"age");
        //获取分页对象
        Pageable pageable = new PageRequest(0, 10,sort);
        Page<Employee> page=employeeRepository.findAll(new Specification<Employee>() {
            @Override
            public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                //获取查询字段
                Path username = root.get("username");
                //获取条件
                Predicate like = criteriaBuilder.like(username, "%1%");
                return like;
            }
        }, pageable);
        page.forEach(employee -> System.out.println(employee));
    }

 

5.jpa-spec插件

     这是一个对于咱们刚才的动态生成Query功能的一个封装版,如果我们使用这个插件,在完成查询与分页的时候功能会简单不少。

基于Spring Data Jpa的动态查询库 https://github.com/wenhao/jpa-spec

5.1.功能介绍

1.兼容Spring Data Jpa 和JPA2.1接口。

2.Equal/NotEqual/Like/NotLike/In/NotIn支持可变参数, Equal/NotEqual 支持空(Null)值。

3.每个条件支持关联查询。

4.支持自定义条件查询。

5.条件构建器。

6.支持分页和排序。

5.2.导包

<!-- SpringData的扩展展包 -->
<dependency>
  <groupId>com.github.wenhao</groupId>
  <artifactId>jpa-spec</artifactId>
  <version>3.1.1</version>
  <!-- 把所有的依賴都去掉 -->
  <exclusions>
    <exclusion>
      <groupId>*</groupId>
      <artifactId>*</artifactId>
    </exclusion>
  </exclusions>
</dependency>

​​​​​​​使用

@Test
public void testJPA_SPEC1(){
    Specification<Employee> spec= Specifications.<Employee>and().like("username","%1%").build();
    List<Employee> all = employeeRepository.findAll(spec);
    all.forEach(employee -> System.out.println(employee));
}
//方法的抽取
@Test
public void testJPA_SPEC2(){
    EmployeeQuery eq = new EmployeeQuery();
    eq.setUsername("1");
    eq.setCurrentPage(1);
    eq.setOrderName("age");
    eq.setOrderType("DESC");
    eq.setPageSize(3);
    //创建分页对象
    Pageable pageable = new PageRequest(eq.getCurrentPageto(), eq.getPageSize(), eq.createSort());//我们抽取的方法
    //创建规则
    Specification<Employee> spec = eq.createSpec();//抽取的方法
    //查询
    Page<Employee> all = employeeRepository.findAll(spec, pageable);
    all.forEach(employee -> System.out.println(employee));

}

 

6.Query抽取

  从前段会传回很多数据,我们需要去接受这些数据,一个一个 接受比较麻烦,我们定义一个类来装这些数据。

  1. 都会传回那些数据
  1. 当前页
  2. 每页的数据
  3. 排序名称
  4. 排序类型
  5. 表里面的数据
  1. 一个表就有一个类,而每一个类的数据都有很多的相同的,我们就可使用一个公共的借口,来装这些从前端传回来的公共数据。

  //当前页
private int currentPage = 1;
//每页条数
private int pageSize = 10;
//排序的名称
private String orderName;
//排序的类型    DESC/ASC
private String orderType = "ASC";

 3.前段页面的起始夜是1,而我们的起始也是0,所以我们需要改变一下:记住不要修改原代码,防止以后使用出错(我们以后就是需要一个当前页,灭有去减去1的数据,就会出错)

//前段页面的起始夜是1,而我们的起始也是0
public int getCurrentPageto() {
    return currentPage-1;
}

4,我们的查询会经常用到排序,所以我们要抽取排序方法到BaseQuery(父类)。

//抽取排序方法
public Sort createSort(){
    //创建排序对象
    Sort sort = null;
    //只有排序条件不会空,我们才创建Sort这个对象
    if(StringUtils.isNotBlank(orderName)){
        //默认类型
        Sort.Direction sortType = Sort.Direction.ASC;
        //toUpperCase() 防止输入小写
        if(orderType.toUpperCase().equals("DESC")){
            sortType = Sort.Direction.DESC;
        }
        sort = new Sort(sortType,orderName);
    }
    return sort;
}

5.每一表都有一些不同的数据,那么一个表就对应一个类

     同时,我们的查询条件也要抽取:  

public class EmployeeQuery extends BaseQuery{
    @Override
    public Specification createSpec() {//父类是有一个这个的抽象方法,子类这是实现
            Specification<Employee> spec = Specifications.<Employee>and()
                    .like(StringUtils.isNotBlank(username), "username", "%" + username + "%")
                    .like(StringUtils.isNotBlank(email), "email", "%" + email + "%")
                    .lt(age != null, "age", age)
                    .build();
            return spec;
        }
    private String username;
    private String email;
    private Integer age;

 

 

    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值