修改readme.md
This commit is contained in:
54
spring-boot/spring-boot-data-jpa/pom.xml
Normal file
54
spring-boot/spring-boot-data-jpa/pom.xml
Normal file
@ -0,0 +1,54 @@
|
||||
<?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>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.1.1.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.heibaiying</groupId>
|
||||
<artifactId>spring-boot-data-jpa</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-boot-data-jpa</name>
|
||||
<description>data jpa project for Spring Boot</description>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--引入mysql驱动-->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.13</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,14 @@
|
||||
package com.heibaiying.springboot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootDataJpaApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootDataJpaApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,52 @@
|
||||
package com.heibaiying.springboot.bean;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
/*
|
||||
* 需要注意是的 这里导入的与JPA相关注解全部是 javax.persistence 包下面的
|
||||
* 尤其是 @Id不要导成org.springframework.data.annotation.Id
|
||||
* 不然会抛出 No identifier specified for entity 异常
|
||||
*/
|
||||
import javax.persistence.*;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
*/
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Entity // 表示该类是一个数据库表映射实体
|
||||
@Table(name = "programmer") // 指明对应的数据库表的名称 不指定的默认就是类名
|
||||
public class Programmer {
|
||||
|
||||
/* AUTO: 默认值,主键由程序控制
|
||||
* IDENTITY: 数据库自动增长, Oracle不支持这种方式 ORACLE 靠序列来提供类似自增长的功能
|
||||
* SEQUENCE: 通过数据库的序列产生主键, MYSQL不支持 MYSQL没有序列的概念
|
||||
* Table: 提供特定的数据库产生主键
|
||||
*/
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private int id;
|
||||
|
||||
@Column(length = 200)
|
||||
private String name;
|
||||
|
||||
private int age;
|
||||
|
||||
@Column(scale = 2)
|
||||
private float salary;
|
||||
|
||||
private Date birthday;
|
||||
|
||||
public Programmer(String name, int age, float salary, Date birthday) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.salary = salary;
|
||||
this.birthday = birthday;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,44 @@
|
||||
package com.heibaiying.springboot.dao;
|
||||
|
||||
import com.heibaiying.springboot.bean.Programmer;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 查询接口 继承自CrudRepository CrudRepository 默认实现了部分增删改查方法
|
||||
*/
|
||||
public interface ProgRepository extends CrudRepository<Programmer, Integer> {
|
||||
|
||||
/*
|
||||
*方法名遵循命名规范的查询 更多命名规范可以参考官方文档所列出的这张表格
|
||||
* https://docs.spring.io/spring-data/jpa/docs/2.1.3.RELEASE/reference/html/#jpa.query-methods.query-creation
|
||||
*/
|
||||
List<Programmer> findAllByName(String name);
|
||||
|
||||
/*
|
||||
*分页查询
|
||||
*/
|
||||
List<Programmer> findByAndSort(String name, Sort sort);
|
||||
|
||||
|
||||
/*
|
||||
* 占位符查询
|
||||
*/
|
||||
@Query(value = "select u from Programmer u where u.name = ?1 or u.salary = ?2")
|
||||
List<Programmer> findByCondition(String name, float salary);
|
||||
|
||||
|
||||
/*
|
||||
* 传入参数名称
|
||||
*/
|
||||
@Query("select u from Programmer u where u.name = :name or u.age = :age")
|
||||
Programmer findByParam(@Param("name") String name,
|
||||
@Param("age") int age);
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:mysql://127.0.0.1:3306/mysql?useUnicode=true&characterEncoding=utf-8
|
||||
username: root
|
||||
password: root
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
jpa:
|
||||
hibernate:
|
||||
# <20><><EFBFBD><EFBFBD>ʵ<EFBFBD>岻һ<E5B2BB><D2BB>ʱ<EFBFBD><CAB1>,<2C><><EFBFBD><EFBFBD>Ӧ<EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF>еı<D0B5><C4B1>ṹ<EFBFBD><E1B9B9><EFBFBD><EFBFBD><EFBFBD>Ǵ<EFBFBD><C7B4>ڱ<EFBFBD><DAB1><EFBFBD>ԭ<EFBFBD>ȵ<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE>
|
||||
ddl-auto: update
|
||||
# HibernateĬ<65>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>myisam<61><6D><EFBFBD>棬<EFBFBD><E6A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD>ʽָ<CABD><D6B8>ΪĬ<CEAA><C4AC>ʹ<EFBFBD><CAB9>innodb<64><62><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
database-platform: org.hibernate.dialect.MySQL57Dialect
|
||||
show-sql: true
|
||||
|
@ -0,0 +1,57 @@
|
||||
package com.heibaiying.springboot;
|
||||
|
||||
import com.heibaiying.springboot.bean.Programmer;
|
||||
import com.heibaiying.springboot.dao.ProgRepository;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.JpaSort;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class SpringBootDataJpaApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private ProgRepository repository;
|
||||
|
||||
@Test
|
||||
public void save() {
|
||||
repository.save(new Programmer("xiaoxiao", 12, 2121.34f, new Date()));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void get() {
|
||||
// 遵循命名规范的查询
|
||||
List<Programmer> programmers = repository.findAllByName("xiaominng");
|
||||
programmers.forEach(System.out::println);
|
||||
// 传入参数名称
|
||||
Programmer param = repository.findByParam("xiaolan", 21);
|
||||
System.out.println("findByParam:" + param);
|
||||
// 占位符查询
|
||||
List<Programmer> byCondition = repository.findByCondition("xiaolan", 347.34f);
|
||||
System.out.println("byCondition:" + byCondition);
|
||||
//条件与分页查询
|
||||
List<Programmer> programmerList = repository.findByAndSort("xiaominng", Sort.by("salary"));
|
||||
programmerList.forEach(System.out::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delete() {
|
||||
Optional<Programmer> programmer = repository.findById(4);
|
||||
if (programmer.isPresent()) {
|
||||
repository.deleteById(4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user