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
}
Lines changed: 16 additions & 0 deletions
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
}
Lines changed: 25 additions & 0 deletions
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+
}

springboot-starter-persistence/src/main/java/com/codingapi/springboot/persistence/property/Property.java renamed to springboot-starter-persistence/src/main/java/com/codingapi/springboot/persistence/property/BeanProperty.java

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
}
Lines changed: 14 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)