根据springboot模板创建项目
访问Spring Initializr:https://start.spring.io/,填好后点击Generate - Ctrl ,下载后解压到工作目录,IDEA打开项目
打开之后配置Maven:File---Settings,搜索maven,选择所在maven目录
选择Jdk:打开Project Structure,选择jdk,我的是1.8
pom.xml文件导入Swagger和JPA的依赖,选择web
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.springdatajpa</groupId>
<artifactId>springdatajpa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springdatajpa</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
swagger 配置:springdatajpa下新建config文件夹,在里面创建个SwaggerConfig类
package com.example.springdatajpa.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("SpringBoot API Doc")
.description("This is a restful api document of Spring Data Jpa.")
.version("1.0")
.build();
}
}
在config里再新建个PageQuery类用于封装分页请求
package com.example.springdatajpa.config;
/**
* 分页请求
*/
public class PageQuery {
/**
* 当前页码
*/
private int pageNum;
/**
* 每页数量
*/
private int pageSize;
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}
配置数据源:把application.properties文件改名为application.yml
server:
port: 8080
spring:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springdatajpa?useUnicode=true&characterEncoding=utf-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
username: root
password: root
jpa:
show-sql: true # 默认false,在日志里显示执行的sql语句
database: mysql
hibernate.ddl-auto: update #指定为update,每次启动项目检测表结构有变化的时候会新增字段,表不存在时会新建,如果指定create,则每次启动项目都会清空数据并删除表,再新建
properties.hibernate.dialect: org.hibernate.diale ct.MySQL5Dialect
database-platform: org.hibernate.dialect.MySQL5Dialect
hibernate:
naming:
implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl #指定jpa的自动表生成策略,驼峰自动映射为下划线格式
#physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
创建目录结构:springdatajpa目录下新建model,controller,service,serviceImpl,dao目录结构,分别在底下新建对应的类和接口
model下SysUser
package com.example.springdatajpa.model;
import javax.persistence.*;
@Entity
@Table(name = "sys_user", indexes = {@Index(name = "id", columnList = "id", unique = true), @Index(name = "name ", columnList = "name", unique = true)})
public class SysUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
// @Column: 对应数据库列名,可选, nullable 是否可以为空, 默认true
@Column(name = "name", nullable = false)
private String name;
private String password;
private String email;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
controller下SysUserController
package com.example.springdatajpa.controller;
import com.example.springdatajpa.config.PageQuery;
import com.example.springdatajpa.model.SysUser;
import com.example.springdatajpa.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("user")
public class SysUserController {
@Autowired
private SysUserService sysUserService;
@PostMapping(value = "/save")
public Object save(@RequestBody SysUser sysUser) {
sysUserService.save(sysUser);
return 200;
}
@PostMapping(value = "/delete")
public Object delete(@RequestBody SysUser sysUser) {
sysUserService.delete(sysUser);
return 200;
}
@GetMapping(value = "/findAll")
public Object findAll() {
return sysUserService.findAll();
}
@PostMapping(value = "/findPage")
public Object findPage(@RequestBody PageQuery pageQuery) {
return sysUserService.findPage(pageQuery);
}
}
service下SysUserService
package com.example.springdatajpa.service;
import com.example.springdatajpa.config.PageQuery;
import com.example.springdatajpa.model.SysUser;
import java.util.List;
public interface SysUserService {
public void save(SysUser sysUser);
public void delete(SysUser sysUser);
public List<SysUser> findAll();
public Object findPage(PageQuery pageQuery);
}
serviceImpl下SysUserServiceImpl
package com.example.springdatajpa.service.serviceImpl;
import com.example.springdatajpa.config.PageQuery;
import com.example.springdatajpa.dao.SysUserDao;
import com.example.springdatajpa.model.SysUser;
import com.example.springdatajpa.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class SysUserServiceImpl implements SysUserService {
@Autowired
private SysUserDao sysUserDao;
@Override
public void save(SysUser sysUser){
sysUserDao.save(sysUser);
}
@Override
public void delete(SysUser sysUser){
sysUserDao.delete(sysUser);
}
@Override
public List<SysUser> findAll(){
return sysUserDao.findAll();
}
@Override
public Object findPage(PageQuery pageQuery){
return sysUserDao.findAll(PageRequest.of(pageQuery.getPageNum(),pageQuery.getPageSize()));
}
}
dao下SysUserDao,继承了JpaRepository,里面不用写任何方法
package com.example.springdatajpa.dao;
import com.example.springdatajpa.model.SysUser;
import org.springframework.data.jpa.repository.JpaRepository;
import java.io.Serializable;
public interface SysUserDao extends JpaRepository<SysUser, Long>, Serializable {
}
启动项目,访问http://localhost:8080/swagger-ui.html,如下界面
点击sys-user-controller下的save,展开后点击Try it out
输入参数信息,点击Execute
成功
其他的方法就不一一试了.....来了解一下Spring Data JPA
Spring Data JPA为Java Persistence API(JPA)提供了存储库支持。它简化了需要访问JPA数据源的应用程序的开发。
Spring Data存储库抽象中的中央接口是Repository
。它需要域类以及域类的ID类型作为类型参数来进行管理。该CrudRepository
规定对于正在管理的实体类复杂的CRUD功能。
CrudRepository
界面
public interface CrudRepository<T, ID> extends Repository<T, ID> {
<S extends T> S save(S entity);
Optional<T> findById(ID primaryKey);
Iterable<T> findAll();
long count();
void delete(T entity);
boolean existsById(ID primaryKey);
// … more functionality omitted.
}
save | 保存给定的实体。 |
findById | 返回由给定ID标识的实体。 |
findAll | 返回所有实体。 |
count | 返回实体数。 |
delete | 删除给定的实体。 |
existsById | 指示是否存在具有给定ID的实体。 |
还提供了特定于持久性技术的抽象,例如JpaRepository
或MongoRepository
。这些接口CrudRepository
除了扩展了与持久性技术无关的通用接口(例如)之外,还扩展并公开了基础持久性技术的功能CrudRepository
。
PagingAndSortingRepository
界面,实现了排序和分页的方法
public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> {
Iterable<T> findAll(Sort sort);
Page<T> findAll(Pageable pageable);
}
JpaRepository又在继承PagingAndSortingRepository的基础上,同时继承了QueryByExampleExecutor接口
JpaRepository界面
package org.springframework.data.jpa.repository;
import java.util.List;
import javax.persistence.EntityManager;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.QueryByExampleExecutor;
/**
* JPA specific extension of {@link org.springframework.data.repository.Repository}.
*
* @author Oliver Gierke
* @author Christoph Strobl
* @author Mark Paluch
*/
@NoRepositoryBean
public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findAll()
*/
List<T> findAll();
/*
* (non-Javadoc)
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Sort)
*/
List<T> findAll(Sort sort);
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findAll(java.lang.Iterable)
*/
List<T> findAllById(Iterable<ID> ids);
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Iterable)
*/
<S extends T> List<S> saveAll(Iterable<S> entities);
/**
* Flushes all pending changes to the database.
*/
void flush();
/**
* Saves an entity and flushes changes instantly.
*
* @param entity
* @return the saved entity
*/
<S extends T> S saveAndFlush(S entity);
/**
* Deletes the given entities in a batch which means it will create a single {@link Query}. Assume that we will clear
* the {@link javax.persistence.EntityManager} after the call.
*
* @param entities
*/
void deleteInBatch(Iterable<T> entities);
/**
* Deletes all entities in a batch call.
*/
void deleteAllInBatch();
/**
* Returns a reference to the entity with the given identifier. Depending on how the JPA persistence provider is
* implemented this is very likely to always return an instance and throw an
* {@link javax.persistence.EntityNotFoundException} on first access. Some of them will reject invalid identifiers
* immediately.
*
* @param id must not be {@literal null}.
* @return a reference to the entity with the given identifier.
* @see EntityManager#getReference(Class, Object) for details on when an exception is thrown.
*/
T getOne(ID id);
/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example)
*/
@Override
<S extends T> List<S> findAll(Example<S> example);
/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example, org.springframework.data.domain.Sort)
*/
@Override
<S extends T> List<S> findAll(Example<S> example, Sort sort);
}
SysUserDao继承了JpaRepository,因而可以使用JpaRepository之上的所有接口,由此更加方便我们进行开发
spring data jpa方法命名规则,直接在接口中定义查询方法,如果是符合规范的,可以不用写实现
关键字 | 方法命名 | sql where字句 |
And | findByNameAndPwd | where name= ? and pwd =? |
Or | findByNameOrSex | where name= ? or sex=? |
Is,Equals | findById,findByIdEquals | where id= ? |
Between | findByIdBetween | where id between ? and ? |
LessThan | findByIdLessThan | where id < ? |
LessThanEquals | findByIdLessThanEquals | where id <= ? |
GreaterThan | findByIdGreaterThan | where id > ? |
GreaterThanEquals | findByIdGreaterThanEquals | where id > = ? |
After | findByIdAfter | where id > ? |
Before | findByIdBefore | where id < ? |
IsNull | findByNameIsNull | where name is null |
isNotNull,NotNull | findByNameNotNull | where name is not null |
Like | findByNameLike | where name like ? |
NotLike | findByNameNotLike | where name not like ? |
StartingWith | findByNameStartingWith | where name like '?%' |
EndingWith | findByNameEndingWith | where name like '%?' |
Containing | findByNameContaining | where name like '%?%' |
OrderBy | findByIdOrderByXDesc | where id=? order by x desc |
Not | findByNameNot | where name <> ? |
In | findByIdIn(Collection<?> c) | where id in (?) |
NotIn | findByNameNot | where name <> ? |
True | findByAaaTue | where aaa = true |
False | findByAaaFalse | where aaa = false |
IgnoreCase | findByNameIgnoreCase | where UPPER(name)=UPPER(?) |
top | findTop100 | top 10/where ROWNUM <=10 |