From 6c17a13f614c492086ddb410a2d05374cf27167d Mon Sep 17 00:00:00 2001 From: yihui Date: Wed, 18 Dec 2019 20:01:37 +0800 Subject: [PATCH] =?UTF-8?q?#77=20jpa=20=E9=94=99=E8=AF=AF=E4=BD=BF?= =?UTF-8?q?=E7=94=A8case?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring-boot/102-jpa-errorcase/pom.xml | 25 ++++++++++ .../com/git/hui/boot/jpacase/Application.java | 16 ++++++ .../boot/jpacase/app/ErrorApplication.java | 16 ++++++ .../app/ErrorJpaCaseAutoConfiguration.java | 13 +++++ .../boot/jpacase/app2/TrueApplication.java | 25 ++++++++++ .../app2/TrueJpaCaseAutoConfiguration.java | 14 ++++++ .../hui/boot/jpacase/entity/MetaGroupPO.java | 37 ++++++++++++++ .../boot/jpacase/manager/GroupManager.java | 50 +++++++++++++++++++ .../git/hui/boot/jpacase/package-info.java | 5 ++ .../repository/GroupJPARepository.java | 26 ++++++++++ .../src/main/resources/application.properties | 10 ++++ spring-boot/102-jpa/pom.xml | 5 ++ .../com/git/hui/boot/jpa/Application.java | 35 ++++++------- spring-boot/pom.xml | 1 + 14 files changed, 261 insertions(+), 17 deletions(-) create mode 100644 spring-boot/102-jpa-errorcase/pom.xml create mode 100644 spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/Application.java create mode 100644 spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/app/ErrorApplication.java create mode 100644 spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/app/ErrorJpaCaseAutoConfiguration.java create mode 100644 spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/app2/TrueApplication.java create mode 100644 spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/app2/TrueJpaCaseAutoConfiguration.java create mode 100644 spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/entity/MetaGroupPO.java create mode 100644 spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/manager/GroupManager.java create mode 100644 spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/package-info.java create mode 100644 spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/repository/GroupJPARepository.java create mode 100644 spring-boot/102-jpa-errorcase/src/main/resources/application.properties diff --git a/spring-boot/102-jpa-errorcase/pom.xml b/spring-boot/102-jpa-errorcase/pom.xml new file mode 100644 index 00000000..87e0cc80 --- /dev/null +++ b/spring-boot/102-jpa-errorcase/pom.xml @@ -0,0 +1,25 @@ + + + + spring-boot + com.git.hui.boot + 0.0.1-SNAPSHOT + + 4.0.0 + + 102-jpa-errorcase + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + mysql + mysql-connector-java + + + \ No newline at end of file diff --git a/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/Application.java b/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/Application.java new file mode 100644 index 00000000..60765da2 --- /dev/null +++ b/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/Application.java @@ -0,0 +1,16 @@ +package com.git.hui.boot.jpacase; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Created by @author yihui in 19:07 19/12/18. + */ +//@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class); + } + +} diff --git a/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/app/ErrorApplication.java b/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/app/ErrorApplication.java new file mode 100644 index 00000000..b079c6eb --- /dev/null +++ b/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/app/ErrorApplication.java @@ -0,0 +1,16 @@ +package com.git.hui.boot.jpacase.app; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Created by @author yihui in 19:07 19/12/18. + */ +@SpringBootApplication +public class ErrorApplication { + + public static void main(String[] args) { + SpringApplication.run(ErrorApplication.class); + } + +} diff --git a/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/app/ErrorJpaCaseAutoConfiguration.java b/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/app/ErrorJpaCaseAutoConfiguration.java new file mode 100644 index 00000000..bd7f7859 --- /dev/null +++ b/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/app/ErrorJpaCaseAutoConfiguration.java @@ -0,0 +1,13 @@ +package com.git.hui.boot.jpacase.app; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +/** + * Created by @author yihui in 18:31 19/12/18. + */ +@Configuration +@ComponentScan(basePackages = "com.git.hui.boot.jpacase") +public class ErrorJpaCaseAutoConfiguration { + +} diff --git a/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/app2/TrueApplication.java b/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/app2/TrueApplication.java new file mode 100644 index 00000000..e0c6caf3 --- /dev/null +++ b/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/app2/TrueApplication.java @@ -0,0 +1,25 @@ +package com.git.hui.boot.jpacase.app2; + +import com.git.hui.boot.jpacase.app.ErrorApplication; +import com.git.hui.boot.jpacase.entity.MetaGroupPO; +import com.git.hui.boot.jpacase.manager.GroupManager; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Created by @author yihui in 19:26 19/12/18. + */ +@SpringBootApplication +public class TrueApplication { + + public TrueApplication(GroupManager groupManager) { + int groupId = groupManager.addGroup("true-group", "dev", "正确写入!!!"); + System.out.println("add groupId: " + groupId); + MetaGroupPO po = groupManager.getOnlineGroup("true-group", "dev"); + System.out.println(po); + } + + public static void main(String[] args) { + SpringApplication.run(ErrorApplication.class); + } +} diff --git a/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/app2/TrueJpaCaseAutoConfiguration.java b/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/app2/TrueJpaCaseAutoConfiguration.java new file mode 100644 index 00000000..959ba1b7 --- /dev/null +++ b/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/app2/TrueJpaCaseAutoConfiguration.java @@ -0,0 +1,14 @@ +package com.git.hui.boot.jpacase.app2; + +import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; + +/** + * Created by @author yihui in 19:27 19/12/18. + */ +@Configuration +@EnableJpaRepositories("com.git.hui.boot.jpacase") +@EntityScan("com.git.hui.boot.jpacase.entity") +public class TrueJpaCaseAutoConfiguration { +} diff --git a/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/entity/MetaGroupPO.java b/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/entity/MetaGroupPO.java new file mode 100644 index 00000000..79bff823 --- /dev/null +++ b/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/entity/MetaGroupPO.java @@ -0,0 +1,37 @@ +package com.git.hui.boot.jpacase.entity; + +import lombok.Data; + +import javax.persistence.*; +import java.sql.Timestamp; + +/** + * Created by @author yihui in 18:28 19/12/18. + */ +@Data +@Entity +@Table(name = "meta_group") +public class MetaGroupPO { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + @Column(name = "`group`") + private String group; + + @Column(name = "`profile`") + private String profile; + + @Column(name = "`desc`") + private String desc; + + @Column(name = "`deleted`") + private Integer deleted; + + @Column(name = "create_time") + private Timestamp createTime; + + @Column(name = "update_time") + private Timestamp updateTime; +} + diff --git a/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/manager/GroupManager.java b/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/manager/GroupManager.java new file mode 100644 index 00000000..8053d206 --- /dev/null +++ b/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/manager/GroupManager.java @@ -0,0 +1,50 @@ +package com.git.hui.boot.jpacase.manager; + +import com.git.hui.boot.jpacase.entity.MetaGroupPO; +import com.git.hui.boot.jpacase.repository.GroupJPARepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +import java.sql.Timestamp; +import java.time.Instant; + +/** + * Created by @author yihui in 18:30 19/12/18. + */ +@Component +public class GroupManager { + @Autowired + private GroupJPARepository groupJPARepository; + + public GroupManager() { + System.out.println("group manager init!"); + } + + public MetaGroupPO getOnlineGroup(String group, String profile) { + return groupJPARepository.findByGroupAndProfileAndDeleted(group, profile, 0); + } + + public Integer addGroup(String group, String profile, String desc) { + MetaGroupPO jpa = new MetaGroupPO(); + jpa.setGroup(group); + jpa.setDesc(desc); + jpa.setProfile(profile); + jpa.setDeleted(0); + Timestamp timestamp = Timestamp.from(Instant.now()); + jpa.setCreateTime(timestamp); + jpa.setUpdateTime(timestamp); + MetaGroupPO res = groupJPARepository.save(jpa); + return res.getId(); + } + + @Transactional + public boolean updateGroup(Integer groupId, String desc) { + return groupJPARepository.updateDesc(groupId, desc) > 0; + } + + @Transactional + public boolean deleteGroup(Integer groupId) { + return groupJPARepository.logicDeleted(groupId) > 0; + } +} diff --git a/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/package-info.java b/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/package-info.java new file mode 100644 index 00000000..1e1edcbf --- /dev/null +++ b/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/package-info.java @@ -0,0 +1,5 @@ +/** + * jpa 易错case + * Created by @author yihui in 18:27 19/12/18. + */ +package com.git.hui.boot.jpacase; \ No newline at end of file diff --git a/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/repository/GroupJPARepository.java b/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/repository/GroupJPARepository.java new file mode 100644 index 00000000..755ab784 --- /dev/null +++ b/spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/repository/GroupJPARepository.java @@ -0,0 +1,26 @@ +package com.git.hui.boot.jpacase.repository; + +import com.git.hui.boot.jpacase.entity.MetaGroupPO; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; + +import java.util.List; + +/** + * Created by @author yihui in 19:12 19/12/16. + */ +public interface GroupJPARepository extends JpaRepository { + + List findByProfile(String profile); + + MetaGroupPO findByGroupAndProfileAndDeleted(String group, String profile, Integer deleted); + + @Modifying + @Query("update MetaGroupPO m set m.desc=?2 where m.id=?1") + int updateDesc(int groupId, String desc); + + @Modifying + @Query("update MetaGroupPO m set m.deleted=1 where m.id=?1") + int logicDeleted(int groupId); +} diff --git a/spring-boot/102-jpa-errorcase/src/main/resources/application.properties b/spring-boot/102-jpa-errorcase/src/main/resources/application.properties new file mode 100644 index 00000000..2526803d --- /dev/null +++ b/spring-boot/102-jpa-errorcase/src/main/resources/application.properties @@ -0,0 +1,10 @@ +## DataSource +spring.datasource.url=jdbc:mysql://127.0.0.1:3306/story?useUnicode=true&characterEncoding=UTF-8&useSSL=false +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +spring.datasource.username=root +spring.datasource.password= +spring.jpa.database=MYSQL +spring.jpa.hibernate.ddl-auto=none +spring.jpa.show-sql=true +spring.jackson.serialization.indent_output=true +spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl \ No newline at end of file diff --git a/spring-boot/102-jpa/pom.xml b/spring-boot/102-jpa/pom.xml index 21a9cc03..fe057d84 100644 --- a/spring-boot/102-jpa/pom.xml +++ b/spring-boot/102-jpa/pom.xml @@ -20,6 +20,11 @@ mysql mysql-connector-java + + com.git.hui.boot + 102-jpa-errorcase + 0.0.1-SNAPSHOT + \ No newline at end of file diff --git a/spring-boot/102-jpa/src/main/java/com/git/hui/boot/jpa/Application.java b/spring-boot/102-jpa/src/main/java/com/git/hui/boot/jpa/Application.java index da8c0a91..ac6149ee 100644 --- a/spring-boot/102-jpa/src/main/java/com/git/hui/boot/jpa/Application.java +++ b/spring-boot/102-jpa/src/main/java/com/git/hui/boot/jpa/Application.java @@ -1,11 +1,5 @@ package com.git.hui.boot.jpa; -import com.git.hui.boot.jpa.demo.JpaDeleteDemo; -import com.git.hui.boot.jpa.demo.JpaInsertDemo; -import com.git.hui.boot.jpa.demo.JpaQueryDemo; -import com.git.hui.boot.jpa.demo.JpaUpdateDemo; -import com.git.hui.boot.jpa.entity.MoneyPO; -import com.git.hui.boot.jpa.repository.MoneyDemoRepository; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -14,20 +8,27 @@ */ @SpringBootApplication public class Application { - public Application(MoneyDemoRepository moneyDemoRepository, JpaQueryDemo jpaQueryDemo, JpaInsertDemo jpaInsertDemo, - JpaUpdateDemo jpaUpdateDemo, JpaDeleteDemo jpaDeleteDemo) { - // MoneyPO moneyPO = moneyDemoRepository.findById(1).get(); - // System.out.println(moneyPO); - // System.out.println("-----------------------"); -// jpaQueryDemo.queryTest(); - - jpaInsertDemo.testInsert(); - // jpaUpdateDemo.testUpdate(); - -// jpaDeleteDemo.testDelete(); + public Application() { +// groupManager.addGroup("test", "desc", "weqr"); } +// public Application(MoneyDemoRepository moneyDemoRepository, + // JpaQueryDemo jpaQueryDemo, JpaInsertDemo jpaInsertDemo, + // JpaUpdateDemo jpaUpdateDemo, JpaDeleteDemo jpaDeleteDemo, + // GroupManager groupManager) { + // // MoneyPO moneyPO = moneyDemoRepository.findById(1).get(); + // // System.out.println(moneyPO); + // // System.out.println("-----------------------"); + // + // // jpaQueryDemo.queryTest(); + // + // // jpaInsertDemo.testInsert(); + // // jpaUpdateDemo.testUpdate(); + // + // // jpaDeleteDemo.testDelete(); + // } + public static void main(String[] args) { SpringApplication.run(Application.class); } diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index f3470abc..85059408 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -59,6 +59,7 @@ 211-web-servlet 212-web-listener 005-config-selector + 102-jpa-errorcase spring-boot