-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
261 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?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"> | ||
<parent> | ||
<artifactId>spring-boot</artifactId> | ||
<groupId>com.git.hui.boot</groupId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>102-jpa-errorcase</artifactId> | ||
|
||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jpa</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
16 changes: 16 additions & 0 deletions
16
spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
...g-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/app/ErrorApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...a-errorcase/src/main/java/com/git/hui/boot/jpacase/app/ErrorJpaCaseAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...g-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/app2/TrueApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...a-errorcase/src/main/java/com/git/hui/boot/jpacase/app2/TrueJpaCaseAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
} |
37 changes: 37 additions & 0 deletions
37
spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/entity/MetaGroupPO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
50 changes: 50 additions & 0 deletions
50
...g-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/manager/GroupManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
spring-boot/102-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* jpa 易错case | ||
* Created by @author yihui in 18:27 19/12/18. | ||
*/ | ||
package com.git.hui.boot.jpacase; |
26 changes: 26 additions & 0 deletions
26
...2-jpa-errorcase/src/main/java/com/git/hui/boot/jpacase/repository/GroupJPARepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<MetaGroupPO, Integer> { | ||
|
||
List<MetaGroupPO> 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); | ||
} |
10 changes: 10 additions & 0 deletions
10
spring-boot/102-jpa-errorcase/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters