diff --git a/spring-boot-demo-jpa/README.md b/spring-boot-demo-jpa/README.md new file mode 100644 index 0000000..5432f41 --- /dev/null +++ b/spring-boot-demo-jpa/README.md @@ -0,0 +1,148 @@ +# spring-boot-demo-jpa + +依赖 [spring-boot-demo-helloworld](../spring-boot-demo-parent)、`spring-boot-starter-data-jpa`、`druid-spring-boot-starter` + +### pom.xml + +```xml + + + 4.0.0 + + spring-boot-demo-jpa + 0.0.1-SNAPSHOT + jar + + spring-boot-demo-jpa + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo-parent + 0.0.1-SNAPSHOT + ../spring-boot-demo-parent + + + + 1.1.5 + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.alibaba + druid-spring-boot-starter + ${druid.starter.version} + + + + + spring-boot-demo-jpa + + + +``` + +### application.yml + +```yml +server: + port: 8080 + context-path: /demo +spring: + datasource: + continue-on-error: true + druid: + url: jdbc:mysql://localhost:3306/spring-boot-demo?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false + username: root + password: root + driver-class-name: com.mysql.jdbc.Driver + # 连接池配置 + # 初始化大小,最小,最大 + initialSize: 5 + minIdle: 5 + maxActive: 20 + # 配置获取连接等待超时的时间 + maxWait: 60000 + # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 + timeBetweenEvictionRunsMillis: 60000 + # 配置一个连接在池中最小生存的时间,单位是毫秒 + minEvictableIdleTimeMillis: 300000 + validationQuery: SELECT 1 FROM DUAL + testWhileIdle: true + testOnBorrow: false + testOnReturn: false + # 打开PSCache,并且指定每个连接上PSCache的大小 + poolPreparedStatements: true + maxPoolPreparedStatementPerConnectionSize: 20 + # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 + filters: stat,wall,log4j + # 监控配置 + web-stat-filter: + enabled: true + url-pattern: /* + exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*" + stat-view-servlet: + enabled: true + url-pattern: /sys/druid/* + reset-enable: fasle + login-username: xkcoding + login-password: 123456 + filter: + stat: + log-slow-sql: true + slow-sql-millis: 5000 + merge-sql: true + jpa: + hibernate: + ddl-auto: create-drop + show-sql: true +``` + +### JpaUser.java + +```java +@Entity +@Data +@DynamicUpdate +public class JpaUser { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "name") + private String name; + + @Column(name = "create_time") + private Date createTime; +} +``` + +### JpaUserRepository.java + +```java +public interface JpaUserRepository extends JpaRepository { + + /** + * 自定义一个查询,HQL,根据姓名查询 + * + * @param name 名称 + * @return JpaUser + */ + @Query("from JpaUser u where u.name like :name") + JpaUser findJpaUser(@Param("name") String name); + + JpaUser findJpaUserByName(String name); + + List findJpaUsersByIdIn(List ids); + +} +``` + +### 其余代码 + +详情请参见本demo。 \ No newline at end of file diff --git a/spring-boot-demo-jpa/pom.xml b/spring-boot-demo-jpa/pom.xml new file mode 100644 index 0000000..142d26f --- /dev/null +++ b/spring-boot-demo-jpa/pom.xml @@ -0,0 +1,40 @@ + + + 4.0.0 + + spring-boot-demo-jpa + 0.0.1-SNAPSHOT + jar + + spring-boot-demo-jpa + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo-parent + 0.0.1-SNAPSHOT + ../spring-boot-demo-parent + + + + 1.1.5 + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.alibaba + druid-spring-boot-starter + ${druid.starter.version} + + + + + spring-boot-demo-jpa + + + \ No newline at end of file diff --git a/spring-boot-demo-jpa/src/main/java/com/xkcoding/springbootdemojpa/SpringBootDemoJpaApplication.java b/spring-boot-demo-jpa/src/main/java/com/xkcoding/springbootdemojpa/SpringBootDemoJpaApplication.java new file mode 100644 index 0000000..6c2cb36 --- /dev/null +++ b/spring-boot-demo-jpa/src/main/java/com/xkcoding/springbootdemojpa/SpringBootDemoJpaApplication.java @@ -0,0 +1,12 @@ +package com.xkcoding.springbootdemojpa; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootDemoJpaApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootDemoJpaApplication.class, args); + } +} diff --git a/spring-boot-demo-jpa/src/main/java/com/xkcoding/springbootdemojpa/controller/JpaUserController.java b/spring-boot-demo-jpa/src/main/java/com/xkcoding/springbootdemojpa/controller/JpaUserController.java new file mode 100644 index 0000000..8681492 --- /dev/null +++ b/spring-boot-demo-jpa/src/main/java/com/xkcoding/springbootdemojpa/controller/JpaUserController.java @@ -0,0 +1,86 @@ +package com.xkcoding.springbootdemojpa.controller; + +import com.google.common.collect.Lists; +import com.xkcoding.springbootdemojpa.entity.JpaUser; +import com.xkcoding.springbootdemojpa.service.JpaUserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.web.bind.annotation.*; + +import java.util.Date; +import java.util.List; + +@RestController +@RequestMapping("/user") +public class JpaUserController { + @Autowired + private JpaUserService jpaUserService; + + @GetMapping("/{id}") + public JpaUser findById(@PathVariable Long id) { + return jpaUserService.findById(id); + } + + @GetMapping({"", "/", "/index"}) + public List findAll() { + return jpaUserService.findAll(); + } + + @GetMapping("/save") + public JpaUser insert() { + JpaUser user = new JpaUser(); + user.setName("xkcoding"); + user.setCreateTime(new Date()); + return jpaUserService.insert(user); + } + + @GetMapping("/update") + public JpaUser update() { + JpaUser user = jpaUserService.findById(1L); + user.setName("修改后的姓名"); + return jpaUserService.update(user); + } + + @GetMapping("/delete/{id}") + public String delete(@PathVariable Integer id) { + if (id == null) { + return "Id 不能为空"; + } + JpaUser jpaUser = jpaUserService.findById(id.longValue()); + if (jpaUser == null) { + return "用户不存在"; + } + jpaUserService.delete(jpaUser); + return jpaUser.getName() + "删除成功"; + } + + @GetMapping("/saveList") + public List insertList() { + List userList = Lists.newArrayList(); + for (int i = 0; i < 20; i++) { + JpaUser jpaUser = new JpaUser(); + jpaUser.setName("xkcoding" + i); + jpaUser.setCreateTime(new Date()); + userList.add(jpaUser); + } + return jpaUserService.insertList(userList); + } + + @GetMapping("/find") + public JpaUser findJpaUser(@RequestParam String name) { + return jpaUserService.findJpaUser(name); + } + + @GetMapping("/find/in") + public List findJpaUsersByIdIn() { + List ids = Lists.newArrayList(1L, 2L, 3L); + return jpaUserService.findJpaUsersByIdIn(ids); + } + + @GetMapping("/page") + public Page findByPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) { + PageRequest pageRequest = new PageRequest(pageNum - 1, pageSize); + return jpaUserService.findByPage(pageRequest); + } +} diff --git a/spring-boot-demo-jpa/src/main/java/com/xkcoding/springbootdemojpa/entity/JpaUser.java b/spring-boot-demo-jpa/src/main/java/com/xkcoding/springbootdemojpa/entity/JpaUser.java new file mode 100644 index 0000000..836f83c --- /dev/null +++ b/spring-boot-demo-jpa/src/main/java/com/xkcoding/springbootdemojpa/entity/JpaUser.java @@ -0,0 +1,22 @@ +package com.xkcoding.springbootdemojpa.entity; + +import lombok.Data; +import org.hibernate.annotations.DynamicUpdate; + +import javax.persistence.*; +import java.util.Date; + +@Entity +@Data +@DynamicUpdate +public class JpaUser { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "name") + private String name; + + @Column(name = "create_time") + private Date createTime; +} diff --git a/spring-boot-demo-jpa/src/main/java/com/xkcoding/springbootdemojpa/repository/JpaUserRepository.java b/spring-boot-demo-jpa/src/main/java/com/xkcoding/springbootdemojpa/repository/JpaUserRepository.java new file mode 100644 index 0000000..72e5083 --- /dev/null +++ b/spring-boot-demo-jpa/src/main/java/com/xkcoding/springbootdemojpa/repository/JpaUserRepository.java @@ -0,0 +1,25 @@ +package com.xkcoding.springbootdemojpa.repository; + +import com.xkcoding.springbootdemojpa.entity.JpaUser; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import java.util.List; + +public interface JpaUserRepository extends JpaRepository { + + /** + * 自定义一个查询,HQL,根据姓名查询 + * + * @param name 名称 + * @return JpaUser + */ + @Query("from JpaUser u where u.name like :name") + JpaUser findJpaUser(@Param("name") String name); + + JpaUser findJpaUserByName(String name); + + List findJpaUsersByIdIn(List ids); + +} diff --git a/spring-boot-demo-jpa/src/main/java/com/xkcoding/springbootdemojpa/service/JpaUserService.java b/spring-boot-demo-jpa/src/main/java/com/xkcoding/springbootdemojpa/service/JpaUserService.java new file mode 100644 index 0000000..964ba2b --- /dev/null +++ b/spring-boot-demo-jpa/src/main/java/com/xkcoding/springbootdemojpa/service/JpaUserService.java @@ -0,0 +1,27 @@ +package com.xkcoding.springbootdemojpa.service; + +import com.xkcoding.springbootdemojpa.entity.JpaUser; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; + +import java.util.List; + +public interface JpaUserService { + JpaUser findById(Long id); + + List findAll(); + + JpaUser insert(JpaUser user); + + JpaUser update(JpaUser user); + + void delete(JpaUser user); + + List insertList(List userList); + + JpaUser findJpaUser(String name); + + List findJpaUsersByIdIn(List ids); + + Page findByPage(Pageable pageable); +} diff --git a/spring-boot-demo-jpa/src/main/java/com/xkcoding/springbootdemojpa/service/impl/JpaUserServiceImpl.java b/spring-boot-demo-jpa/src/main/java/com/xkcoding/springbootdemojpa/service/impl/JpaUserServiceImpl.java new file mode 100644 index 0000000..8e3508f --- /dev/null +++ b/spring-boot-demo-jpa/src/main/java/com/xkcoding/springbootdemojpa/service/impl/JpaUserServiceImpl.java @@ -0,0 +1,62 @@ +package com.xkcoding.springbootdemojpa.service.impl; + +import com.xkcoding.springbootdemojpa.entity.JpaUser; +import com.xkcoding.springbootdemojpa.repository.JpaUserRepository; +import com.xkcoding.springbootdemojpa.service.JpaUserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class JpaUserServiceImpl implements JpaUserService { + @Autowired + private JpaUserRepository jpaUserRepository; + + @Override + public JpaUser findById(Long id) { + return jpaUserRepository.findOne(id); + } + + @Override + public List findAll() { + return jpaUserRepository.findAll(); + } + + @Override + public JpaUser insert(JpaUser user) { + return jpaUserRepository.save(user); + } + + @Override + public JpaUser update(JpaUser user) { + return jpaUserRepository.save(user); + } + + @Override + public void delete(JpaUser user) { + jpaUserRepository.delete(user); + } + + @Override + public List insertList(List userList) { + return jpaUserRepository.save(userList); + } + + @Override + public JpaUser findJpaUser(String name) { + return jpaUserRepository.findJpaUser(name); + } + + @Override + public List findJpaUsersByIdIn(List ids) { + return jpaUserRepository.findJpaUsersByIdIn(ids); + } + + @Override + public Page findByPage(Pageable pageable) { + return jpaUserRepository.findAll(pageable); + } +} diff --git a/spring-boot-demo-jpa/src/main/resources/application.yml b/spring-boot-demo-jpa/src/main/resources/application.yml new file mode 100644 index 0000000..9cadf45 --- /dev/null +++ b/spring-boot-demo-jpa/src/main/resources/application.yml @@ -0,0 +1,51 @@ +server: + port: 8080 + context-path: /demo +spring: + datasource: + continue-on-error: true + druid: + url: jdbc:mysql://localhost:3306/spring-boot-demo?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false + username: root + password: root + driver-class-name: com.mysql.jdbc.Driver + # 连接池配置 + # 初始化大小,最小,最大 + initialSize: 5 + minIdle: 5 + maxActive: 20 + # 配置获取连接等待超时的时间 + maxWait: 60000 + # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 + timeBetweenEvictionRunsMillis: 60000 + # 配置一个连接在池中最小生存的时间,单位是毫秒 + minEvictableIdleTimeMillis: 300000 + validationQuery: SELECT 1 FROM DUAL + testWhileIdle: true + testOnBorrow: false + testOnReturn: false + # 打开PSCache,并且指定每个连接上PSCache的大小 + poolPreparedStatements: true + maxPoolPreparedStatementPerConnectionSize: 20 + # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 + filters: stat,wall,log4j + # 监控配置 + web-stat-filter: + enabled: true + url-pattern: /* + exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*" + stat-view-servlet: + enabled: true + url-pattern: /sys/druid/* + reset-enable: fasle + login-username: xkcoding + login-password: 123456 + filter: + stat: + log-slow-sql: true + slow-sql-millis: 5000 + merge-sql: true + jpa: + hibernate: + ddl-auto: create-drop + show-sql: true diff --git a/spring-boot-demo-jpa/src/test/java/com/xkcoding/springbootdemojpa/SpringBootDemoJpaApplicationTests.java b/spring-boot-demo-jpa/src/test/java/com/xkcoding/springbootdemojpa/SpringBootDemoJpaApplicationTests.java new file mode 100644 index 0000000..979f482 --- /dev/null +++ b/spring-boot-demo-jpa/src/test/java/com/xkcoding/springbootdemojpa/SpringBootDemoJpaApplicationTests.java @@ -0,0 +1,16 @@ +package com.xkcoding.springbootdemojpa; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class SpringBootDemoJpaApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/spring-boot-demo-parent/pom.xml b/spring-boot-demo-parent/pom.xml index d2df339..215037e 100644 --- a/spring-boot-demo-parent/pom.xml +++ b/spring-boot-demo-parent/pom.xml @@ -16,6 +16,7 @@ ../spring-boot-demo-properties ../spring-boot-demo-actuator ../spring-boot-demo-logback + ../spring-boot-demo-jpa ../spring-boot-demo-mybatis @@ -30,6 +31,10 @@ UTF-8 UTF-8 1.8 + 3.2.0 + 3.5 + 3.2.2 + 1.10 @@ -52,10 +57,25 @@ mysql-connector-java + + org.apache.commons + commons-lang3 + ${commons.lang3.version} + + + commons-collections + commons-collections + ${commons.collections.version} + + + commons-codec + commons-codec + ${commons.codec.version} + com.xiaoleilu hutool-all - 3.1.2 + ${hutool.version} com.google.guava