根據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 {
}
點擊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之上的所有接口,由此更加方便我們進行開發
關鍵字 |
方法命名 |
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 |