Skip to content

Commit 5c41144

Browse files
committed
support delete & update
1 parent db41527 commit 5c41144

File tree

16 files changed

+168
-29
lines changed

16 files changed

+168
-29
lines changed

springboot-starter-persistence/src/main/java/com/codingapi/springboot/persistence/DomainPersistence.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ public interface DomainPersistence {
66

77
<T> T get(Class<T> domainClass, Object id);
88

9+
void delete(Class<?> domainClass,Object id);
10+
11+
void update(Object domain);
912
}

springboot-starter-persistence/src/main/java/com/codingapi/springboot/persistence/jdbc/impl/JdbcDomainPersistence.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.codingapi.springboot.persistence.schema.SaveSchema;
66
import com.codingapi.springboot.persistence.schema.Schema;
77
import com.codingapi.springboot.persistence.schema.SearchSchema;
8+
import com.codingapi.springboot.persistence.schema.UpdateSchema;
89
import lombok.AllArgsConstructor;
910
import org.springframework.dao.EmptyResultDataAccessException;
1011
import org.springframework.jdbc.core.BeanPropertyRowMapper;
@@ -57,4 +58,22 @@ public <T> T get(Class<T> domainClass, Object id) {
5758
}
5859
return null;
5960
}
61+
62+
@Override
63+
public void delete(Class<?> domainClazz,Object id) {
64+
Schema schema = SchemaContext.getINSTANCE().getSchema(domainClazz);
65+
if (schema != null) {
66+
String sql = schema.deleteSchema().deleteSchema();
67+
jdbcTemplate.update(sql, id);
68+
}
69+
}
70+
71+
@Override
72+
public void update(Object domain) {
73+
Schema schema = SchemaContext.getINSTANCE().getSchema(domain.getClass());
74+
if (schema != null) {
75+
UpdateSchema updateSchema = schema.updateSchema();
76+
jdbcTemplate.update(updateSchema.updateSchema(), updateSchema.getUpdateValues(domain));
77+
}
78+
}
6079
}

springboot-starter-persistence/src/main/java/com/codingapi/springboot/persistence/jdbc/schema/JdbcBuildSchema.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.codingapi.springboot.persistence.jdbc.schema;
22

33
import com.codingapi.springboot.persistence.schema.BuildSchema;
4-
import com.codingapi.springboot.persistence.property.Property;
4+
import com.codingapi.springboot.persistence.property.BeanProperty;
55
import com.codingapi.springboot.persistence.schema.Schema;
66

77
import java.util.List;
@@ -19,12 +19,12 @@ public String createSchema() {
1919
sql.append(property.getSchemaName());
2020
sql.append(" (");
2121
sql.append("id INT PRIMARY KEY AUTO_INCREMENT,");
22-
List<Property> properties = property.getProperties(false);
22+
List<BeanProperty> properties = property.getProperties(false);
2323
for (int i = 0; i < properties.size(); i++) {
24-
Property property = properties.get(i);
25-
sql.append(property.getName());
24+
BeanProperty beanProperty = properties.get(i);
25+
sql.append(beanProperty.getName());
2626
sql.append(" ");
27-
sql.append(property.getJdbcType());
27+
sql.append(beanProperty.getJdbcType());
2828
if (i != properties.size() - 1) {
2929
sql.append(", ");
3030
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.codingapi.springboot.persistence.jdbc.schema;
2+
3+
import com.codingapi.springboot.persistence.schema.DeleteSchema;
4+
import com.codingapi.springboot.persistence.schema.Schema;
5+
6+
public class JdbcDeleteSchema extends DeleteSchema {
7+
8+
public JdbcDeleteSchema(Schema schema) {
9+
super(schema);
10+
}
11+
12+
@Override
13+
public String deleteSchema() {
14+
return "DELETE FROM " + property.getSchemaName() + " WHERE id = ?";
15+
}
16+
}

springboot-starter-persistence/src/main/java/com/codingapi/springboot/persistence/jdbc/schema/JdbcSaveSchema.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.codingapi.springboot.persistence.jdbc.schema;
22

3-
import com.codingapi.springboot.persistence.property.Property;
3+
import com.codingapi.springboot.persistence.property.BeanProperty;
44
import com.codingapi.springboot.persistence.schema.SaveSchema;
55
import com.codingapi.springboot.persistence.schema.Schema;
66

@@ -16,13 +16,13 @@ public String saveSchema(boolean hasId) {
1616
sql.append("INSERT INTO ");
1717
sql.append(property.getSchemaName());
1818
sql.append(" (");
19-
for (Property property : property.getProperties(hasId)) {
20-
sql.append(property.getName());
19+
for (BeanProperty beanProperty : property.getProperties(hasId)) {
20+
sql.append(beanProperty.getName());
2121
sql.append(", ");
2222
}
2323
sql.delete(sql.length() - 2, sql.length());
2424
sql.append(") VALUES (");
25-
for (Property property : property.getProperties(hasId)) {
25+
for (BeanProperty beanProperty : property.getProperties(hasId)) {
2626
sql.append("?, ");
2727
}
2828
sql.delete(sql.length() - 2, sql.length());

springboot-starter-persistence/src/main/java/com/codingapi/springboot/persistence/jdbc/schema/JdbcSchema.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.codingapi.springboot.persistence.jdbc.schema;
22

3-
import com.codingapi.springboot.persistence.schema.BuildSchema;
4-
import com.codingapi.springboot.persistence.schema.SaveSchema;
5-
import com.codingapi.springboot.persistence.schema.Schema;
6-
import com.codingapi.springboot.persistence.schema.SearchSchema;
3+
import com.codingapi.springboot.persistence.schema.*;
74

85
public class JdbcSchema extends Schema {
96

@@ -25,4 +22,14 @@ public SaveSchema insertSchema() {
2522
public SearchSchema getById() {
2623
return new JdbcSearchSchema(this);
2724
}
25+
26+
@Override
27+
public DeleteSchema deleteSchema() {
28+
return new JdbcDeleteSchema(this);
29+
}
30+
31+
@Override
32+
public UpdateSchema updateSchema() {
33+
return new JdbcUpdateSchema(this);
34+
}
2835
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.codingapi.springboot.persistence.jdbc.schema;
2+
3+
import com.codingapi.springboot.persistence.property.BeanProperty;
4+
import com.codingapi.springboot.persistence.schema.Schema;
5+
import com.codingapi.springboot.persistence.schema.UpdateSchema;
6+
7+
public class JdbcUpdateSchema extends UpdateSchema {
8+
9+
public JdbcUpdateSchema(Schema schema) {
10+
super(schema);
11+
}
12+
13+
@Override
14+
public String updateSchema() {
15+
StringBuilder sql = new StringBuilder();
16+
sql.append("UPDATE ").append(property.getSchemaName()).append(" SET ");
17+
for (BeanProperty property : property.getProperties(false)) {
18+
sql.append(property.getName()).append(" = ?, ");
19+
}
20+
sql.delete(sql.length() - 2, sql.length());
21+
sql.append(" WHERE id = ?");
22+
return sql.toString();
23+
}
24+
25+
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.codingapi.springboot.persistence.property;
22

33
import lombok.extern.slf4j.Slf4j;
4+
import org.yaml.snakeyaml.introspector.Property;
45

56
@Slf4j
6-
public class Property {
7+
public class BeanProperty {
78

8-
private final org.yaml.snakeyaml.introspector.Property property;
9+
private final Property property;
910

10-
public Property(org.yaml.snakeyaml.introspector.Property property) {
11+
public BeanProperty(Property property) {
1112
this.property = property;
1213
}
1314

springboot-starter-persistence/src/main/java/com/codingapi/springboot/persistence/property/SchemaProperty.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,41 @@
99

1010
public class SchemaProperty {
1111

12-
private final List<Property> properties;
12+
private final List<BeanProperty> properties;
1313
@Getter
1414
private final String schemaName;
1515
@Getter
16-
private final Property idProperty;
16+
private final BeanProperty idBeanProperty;
1717

1818
public SchemaProperty(Class<?> domainClazz) {
1919
PropertyUtils propertyUtils = new PropertyUtils();
2020
propertyUtils.setSkipMissingProperties(true);
2121
this.properties = propertyUtils.getProperties(domainClazz).stream()
22-
.map(Property::new).collect(Collectors.toList());
23-
this.idProperty = new Property(propertyUtils.getProperty(domainClazz, "id"));
22+
.map(BeanProperty::new).collect(Collectors.toList());
23+
this.idBeanProperty = new BeanProperty(propertyUtils.getProperty(domainClazz, "id"));
2424
this.schemaName = domainClazz.getSimpleName();
2525
}
2626

2727

28-
public List<Property> getProperties(boolean hasId) {
28+
public List<BeanProperty> getProperties(boolean hasId) {
2929
if(hasId){
3030
return properties;
3131
}else{
3232
return properties.stream()
33-
.filter(property -> !property.getName().equals("id"))
33+
.filter(beanProperty -> !beanProperty.getName().equals("id"))
3434
.collect(Collectors.toList());
3535
}
3636
}
3737

38-
public List<Property> getProperties() {
38+
public List<BeanProperty> getProperties() {
3939
return getProperties(true);
4040
}
4141

4242
public boolean hasIdValue(Object domain) {
43-
return idProperty.hasIdValue(domain);
43+
return idBeanProperty.hasIdValue(domain);
4444
}
4545

4646
public void setIdValue(Object domain, Number key) {
47-
idProperty.setIdValue(domain, key);
47+
idBeanProperty.setIdValue(domain, key);
4848
}
4949
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.codingapi.springboot.persistence.schema;
2+
3+
import com.codingapi.springboot.persistence.property.SchemaProperty;
4+
5+
public abstract class DeleteSchema {
6+
7+
public abstract String deleteSchema();
8+
9+
protected final SchemaProperty property;
10+
11+
public DeleteSchema(Schema schema) {
12+
this.property = schema.getSchemaProperty();
13+
}
14+
}

springboot-starter-persistence/src/main/java/com/codingapi/springboot/persistence/schema/SaveSchema.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.codingapi.springboot.persistence.schema;
22

3-
import com.codingapi.springboot.persistence.property.Property;
3+
import com.codingapi.springboot.persistence.property.BeanProperty;
44
import com.codingapi.springboot.persistence.property.SchemaProperty;
55
import lombok.Getter;
66

@@ -25,8 +25,8 @@ public String saveSchema() {
2525

2626
public Object[] getSaveValues(Object object, boolean hasId) {
2727
List<Object> values = new ArrayList<>();
28-
for (Property property : property.getProperties(hasId)) {
29-
values.add(property.get(object));
28+
for (BeanProperty beanProperty : property.getProperties(hasId)) {
29+
values.add(beanProperty.get(object));
3030
}
3131
return values.toArray();
3232
}

springboot-starter-persistence/src/main/java/com/codingapi/springboot/persistence/schema/Schema.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public Schema(Class<?> domainClass) {
2626

2727
public abstract SearchSchema getById();
2828

29+
public abstract DeleteSchema deleteSchema();
30+
31+
public abstract UpdateSchema updateSchema();
32+
2933
public boolean supports(Class<?> domainClass) {
3034
return this.domainClass.equals(domainClass);
3135
}

springboot-starter-persistence/src/main/java/com/codingapi/springboot/persistence/schema/SearchSchema.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public SearchSchema(Schema schema) {
1515
public abstract String getById();
1616

1717
public Object getByIdValue(Object domain) {
18-
return property.getIdProperty().get(domain);
18+
return property.getIdBeanProperty().get(domain);
1919
}
2020

2121
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.codingapi.springboot.persistence.schema;
2+
3+
import com.codingapi.springboot.persistence.property.BeanProperty;
4+
import com.codingapi.springboot.persistence.property.SchemaProperty;
5+
6+
public abstract class UpdateSchema {
7+
8+
public abstract String updateSchema();
9+
10+
protected final SchemaProperty property;
11+
12+
public UpdateSchema(Schema schema) {
13+
this.property = schema.getSchemaProperty();
14+
}
15+
16+
public Object[] getUpdateValues(Object object) {
17+
BeanProperty idBeanProperty = property.getIdBeanProperty();
18+
Object[] values = new Object[property.getProperties(true).size()];
19+
int i = 0;
20+
for (BeanProperty beanProperty : property.getProperties(false)) {
21+
values[i] = beanProperty.get(object);
22+
i++;
23+
}
24+
values[i] = idBeanProperty.get(object);
25+
return values;
26+
}
27+
28+
29+
}

springboot-starter-persistence/src/test/java/com/example/demo/repository/DemoRepository.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,12 @@ public Demo get(int id) {
2121
return domainPersistence.get(Demo.class, id);
2222
}
2323

24+
public void delete(int id) {
25+
domainPersistence.delete(Demo.class,id);
26+
}
27+
28+
public void update(Demo demo) {
29+
domainPersistence.update(demo);
30+
}
31+
2432
}

springboot-starter-persistence/src/test/java/com/example/demo/repository/DemoRepositoryTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,17 @@ void get() {
3838
log.info("demo: {}", demo);
3939
}
4040

41+
@Test
42+
void delete() {
43+
demoRepository.delete(100);
44+
}
45+
46+
47+
@Test
48+
void update() {
49+
Demo demo = new Demo();
50+
demo.setId(100);
51+
demo.setName("123456");
52+
demoRepository.update(demo);
53+
}
4154
}

0 commit comments

Comments
 (0)