Skip to content

Commit

Permalink
#77 jpa 错误使用case
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyueyi committed Dec 18, 2019
1 parent 190dd6d commit 6c17a13
Show file tree
Hide file tree
Showing 14 changed files with 261 additions and 17 deletions.
25 changes: 25 additions & 0 deletions spring-boot/102-jpa-errorcase/pom.xml
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>
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);
}

}
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);
}

}
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 {

}
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);
}
}
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 {
}
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;
}

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;
}
}
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;
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);
}
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
5 changes: 5 additions & 0 deletions spring-boot/102-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.git.hui.boot</groupId>
<artifactId>102-jpa-errorcase</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions spring-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<module>211-web-servlet</module>
<module>212-web-listener</module>
<module>005-config-selector</module>
<module>102-jpa-errorcase</module>
</modules>

<artifactId>spring-boot</artifactId>
Expand Down

0 comments on commit 6c17a13

Please sign in to comment.