From 86cfd2b61ba051b60656b980fd26fdd714034a06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E7=A5=A5?= <1366971433@qq.com> Date: Thu, 3 Jan 2019 18:03:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9readme.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +- spring-boot/spring-boot-data-jpa/pom.xml | 54 ++++++++++++++++++ .../SpringBootDataJpaApplication.java | 14 +++++ .../springboot/bean/Programmer.java | 52 +++++++++++++++++ .../springboot/dao/ProgRepository.java | 44 ++++++++++++++ .../src/main/resources/application.yml | 14 +++++ .../SpringBootDataJpaApplicationTests.java | 57 +++++++++++++++++++ 7 files changed, 237 insertions(+), 2 deletions(-) create mode 100644 spring-boot/spring-boot-data-jpa/pom.xml create mode 100644 spring-boot/spring-boot-data-jpa/src/main/java/com/heibaiying/springboot/SpringBootDataJpaApplication.java create mode 100644 spring-boot/spring-boot-data-jpa/src/main/java/com/heibaiying/springboot/bean/Programmer.java create mode 100644 spring-boot/spring-boot-data-jpa/src/main/java/com/heibaiying/springboot/dao/ProgRepository.java create mode 100644 spring-boot/spring-boot-data-jpa/src/main/resources/application.yml create mode 100644 spring-boot/spring-boot-data-jpa/src/test/java/com/heibaiying/springboot/SpringBootDataJpaApplicationTests.java diff --git a/README.md b/README.md index 3cdcd8e..357d076 100644 --- a/README.md +++ b/README.md @@ -53,9 +53,9 @@ spring-cloud:Finchley.SR2 | [spring-boot-servlet](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-servlet) | 整合servlet 3.0 | [Embedded Servlet Container Support](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-embedded-container) | | [spring-boot-tomcat](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-tomcat) | spring-boot 整合外部容器(tomcat) | [Use Another Web Server](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#howto-use-another-web-server) | | [spring-boot-jsp](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-jsp) | spring-boot 整合 jsp | [JSP Limitations](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-jsp-limitations) | -| spring-boot-jpa | spring-boot jpa 的使用 | [Spring Data JPA](https://docs.spring.io/spring-data/jpa/docs/2.1.3.RELEASE/reference/html/) | +| spring-boot-data-jpa | spring-boot data jpa 的使用 | [Spring Data JPA](https://docs.spring.io/spring-data/jpa/docs/2.1.3.RELEASE/reference/html/) | | spring-boot-mybatis | spring-boot 整合 mybatis
HikariDataSources数据源的配置 | [Mybatis-Spring](http://www.mybatis.org/spring/zh/index.html)
[Mybatis-Spring-Boot-Autoconfigure](http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/) | -| spring-boot-druid-mybtais | spring-boot 整合druid、mybatis | [Alibaba druid](https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98) | +| spring-boot-druid-mybtais | spring-boot 整合druid、mybatis | [Alibaba druid](https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98)
[druid-spring-boot-starter](https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter) | | [spring-boot-redis](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-redis) | spring-boot 整合 redis | [Working with NoSQL Technologies](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-nosql) | | [spring-boot-mongodb](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-mongodb) | spring-boot 整合 mongodb | [Working with NoSQL Technologies](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-nosql) | | [spring-boot-memcached](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-memcached) | spring-boot 整合 memcached | [Xmemcached](https://github.com/killme2008/xmemcached/wiki/Xmemcached%20%E4%B8%AD%E6%96%87%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97) | diff --git a/spring-boot/spring-boot-data-jpa/pom.xml b/spring-boot/spring-boot-data-jpa/pom.xml new file mode 100644 index 0000000..7431766 --- /dev/null +++ b/spring-boot/spring-boot-data-jpa/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.1.RELEASE + + + com.heibaiying + spring-boot-data-jpa + 0.0.1-SNAPSHOT + spring-boot-data-jpa + data jpa project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + + mysql + mysql-connector-java + 8.0.13 + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot/spring-boot-data-jpa/src/main/java/com/heibaiying/springboot/SpringBootDataJpaApplication.java b/spring-boot/spring-boot-data-jpa/src/main/java/com/heibaiying/springboot/SpringBootDataJpaApplication.java new file mode 100644 index 0000000..5d8b4b9 --- /dev/null +++ b/spring-boot/spring-boot-data-jpa/src/main/java/com/heibaiying/springboot/SpringBootDataJpaApplication.java @@ -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); + } + +} + diff --git a/spring-boot/spring-boot-data-jpa/src/main/java/com/heibaiying/springboot/bean/Programmer.java b/spring-boot/spring-boot-data-jpa/src/main/java/com/heibaiying/springboot/bean/Programmer.java new file mode 100644 index 0000000..c4b7fd7 --- /dev/null +++ b/spring-boot/spring-boot-data-jpa/src/main/java/com/heibaiying/springboot/bean/Programmer.java @@ -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; + } + +} + diff --git a/spring-boot/spring-boot-data-jpa/src/main/java/com/heibaiying/springboot/dao/ProgRepository.java b/spring-boot/spring-boot-data-jpa/src/main/java/com/heibaiying/springboot/dao/ProgRepository.java new file mode 100644 index 0000000..f599bf8 --- /dev/null +++ b/spring-boot/spring-boot-data-jpa/src/main/java/com/heibaiying/springboot/dao/ProgRepository.java @@ -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 { + + /* + *方法名遵循命名规范的查询 更多命名规范可以参考官方文档所列出的这张表格 + * https://docs.spring.io/spring-data/jpa/docs/2.1.3.RELEASE/reference/html/#jpa.query-methods.query-creation + */ + List findAllByName(String name); + + /* + *分页查询 + */ + List findByAndSort(String name, Sort sort); + + + /* + * 占位符查询 + */ + @Query(value = "select u from Programmer u where u.name = ?1 or u.salary = ?2") + List 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); +} diff --git a/spring-boot/spring-boot-data-jpa/src/main/resources/application.yml b/spring-boot/spring-boot-data-jpa/src/main/resources/application.yml new file mode 100644 index 0000000..131e815 --- /dev/null +++ b/spring-boot/spring-boot-data-jpa/src/main/resources/application.yml @@ -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: + # ʵ岻һʱ,ӦĸݿеıṹǴڱԭȵֵɾ + ddl-auto: update + # HibernateĬϴımyisam棬·ʽָΪĬʹinnodb + database-platform: org.hibernate.dialect.MySQL57Dialect + show-sql: true + diff --git a/spring-boot/spring-boot-data-jpa/src/test/java/com/heibaiying/springboot/SpringBootDataJpaApplicationTests.java b/spring-boot/spring-boot-data-jpa/src/test/java/com/heibaiying/springboot/SpringBootDataJpaApplicationTests.java new file mode 100644 index 0000000..1098526 --- /dev/null +++ b/spring-boot/spring-boot-data-jpa/src/test/java/com/heibaiying/springboot/SpringBootDataJpaApplicationTests.java @@ -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 programmers = repository.findAllByName("xiaominng"); + programmers.forEach(System.out::println); + // 传入参数名称 + Programmer param = repository.findByParam("xiaolan", 21); + System.out.println("findByParam:" + param); + // 占位符查询 + List byCondition = repository.findByCondition("xiaolan", 347.34f); + System.out.println("byCondition:" + byCondition); + //条件与分页查询 + List programmerList = repository.findByAndSort("xiaominng", Sort.by("salary")); + programmerList.forEach(System.out::println); + } + + @Test + public void delete() { + Optional programmer = repository.findById(4); + if (programmer.isPresent()) { + repository.deleteById(4); + } + } +} +