-
Notifications
You must be signed in to change notification settings - Fork 942
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1615 from ywjno/jsonAdaptor
给Mysql跟Postgresql添加保存JSON内容时的设置格式的ValueAdaptor
- Loading branch information
Showing
16 changed files
with
374 additions
and
14 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
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
22 changes: 22 additions & 0 deletions
22
src/org/nutz/dao/impl/jdbc/mysql/MysqlJsonCompactAdaptor.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,22 @@ | ||
package org.nutz.dao.impl.jdbc.mysql; | ||
|
||
import org.nutz.json.JsonFormat; | ||
|
||
/** | ||
* 数据库中使用 {@code JsonFormat.compact()} 格式来保存JSON类型字段的值。 | ||
* <p /> | ||
* 使用时 {@code ColDefine} 注解的 {@code adaptor} 属性显示声明为 {@code MysqlJsonCompactAdaptor.class} | ||
* <p /> | ||
* <pre> | ||
* {@code | ||
* @ColDefine(customType = "json", type = ColType.MYSQL_JSON, adaptor = MysqlJsonCompactAdaptor.class) | ||
* private Information info; | ||
* } | ||
* </pre> | ||
*/ | ||
public class MysqlJsonCompactAdaptor extends MysqlJsonAdaptor { | ||
|
||
public MysqlJsonCompactAdaptor() { | ||
setJsonFormat(JsonFormat.compact()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/org/nutz/dao/impl/jdbc/mysql/MysqlJsonTidyAdaptor.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,22 @@ | ||
package org.nutz.dao.impl.jdbc.mysql; | ||
|
||
import org.nutz.json.JsonFormat; | ||
|
||
/** | ||
* 数据库中使用 {@code JsonFormat.tidy()} 格式来保存JSON类型字段的值。 | ||
* <p /> | ||
* 使用时 {@code ColDefine} 注解的 {@code adaptor} 属性显示声明为 {@code MysqlJsonTidyAdaptor.class} | ||
* <p /> | ||
* <pre> | ||
* {@code | ||
* @ColDefine(customType = "json", type = ColType.MYSQL_JSON, adaptor = MysqlJsonTidyAdaptor.class) | ||
* private Information info; | ||
* } | ||
* </pre> | ||
*/ | ||
public class MysqlJsonTidyAdaptor extends MysqlJsonAdaptor { | ||
|
||
public MysqlJsonTidyAdaptor() { | ||
setJsonFormat(JsonFormat.tidy()); | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
src/org/nutz/dao/impl/jdbc/psql/PsqlJsonCompactAdaptor.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,22 @@ | ||
package org.nutz.dao.impl.jdbc.psql; | ||
|
||
import org.nutz.json.JsonFormat; | ||
|
||
/** | ||
* 数据库中使用 {@code JsonFormat.compact()} 格式来保存JSON类型字段的值。 | ||
* <p /> | ||
* 使用时 {@code ColDefine} 注解的 {@code adaptor} 属性显示声明为 {@code PsqlJsonCompactAdaptor.class} | ||
* <p /> | ||
* <pre> | ||
* {@code | ||
* @ColDefine(customType = "json", type = ColType.PSQL_JSON, adaptor = PsqlJsonCompactAdaptor.class) | ||
* private Information info; | ||
* } | ||
* </pre> | ||
*/ | ||
public class PsqlJsonCompactAdaptor extends PsqlJsonAdaptor { | ||
|
||
public PsqlJsonCompactAdaptor() { | ||
setJsonFormat(JsonFormat.compact()); | ||
} | ||
} |
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,22 @@ | ||
package org.nutz.dao.impl.jdbc.psql; | ||
|
||
import org.nutz.json.JsonFormat; | ||
|
||
/** | ||
* 数据库中使用 {@code JsonFormat.tidy()} 格式来保存JSON类型字段的值。 | ||
* <p /> | ||
* 使用时 {@code ColDefine} 注解的 {@code adaptor} 属性显示声明为 {@code PsqlJsonTidyAdaptor.class} | ||
* <p /> | ||
* <pre> | ||
* {@code | ||
* @ColDefine(customType = "json", type = ColType.PSQL_JSON, adaptor = PsqlJsonTidyAdaptor.class) | ||
* private Information info; | ||
* } | ||
* </pre> | ||
*/ | ||
public class PsqlJsonTidyAdaptor extends PsqlJsonAdaptor { | ||
|
||
public PsqlJsonTidyAdaptor() { | ||
setJsonFormat(JsonFormat.tidy()); | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
test/org/nutz/dao/test/normal/mysql/MysqlJsonAdaptorTest.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,48 @@ | ||
package org.nutz.dao.test.normal.mysql; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.math.BigDecimal; | ||
|
||
import org.junit.Test; | ||
import org.nutz.dao.Cnd; | ||
import org.nutz.dao.test.DaoCase; | ||
import org.nutz.json.Json; | ||
import org.nutz.json.JsonFormat; | ||
|
||
public class MysqlJsonAdaptorTest extends DaoCase { | ||
|
||
@Override | ||
protected void before() { | ||
if (!dao.meta().isMySql()) { | ||
return; | ||
} | ||
dao.create(MysqlJsonAdaptorTestBean.class, true); | ||
} | ||
|
||
@Test | ||
public void adapotor() { | ||
if (!dao.meta().isMySql()) { | ||
return; | ||
} | ||
|
||
MysqlJsonAdaptorTestBean testBean = new MysqlJsonAdaptorTestBean(); | ||
StudentResult result = new StudentResult(); | ||
result.setPhysics(new BigDecimal("100")); | ||
testBean.setNoneAdaptor(result); | ||
testBean.setJsonAdaptor(result); | ||
testBean.setJsonCompactAdaptor(result); | ||
testBean.setJsonTidyAdaptor(result); | ||
|
||
int insertId = dao.insert(testBean).getId(); | ||
|
||
org.nutz.dao.entity.Record record = dao.fetch("t_mysql_json_adaptor_test_bean", Cnd.where("id","=",insertId)); | ||
// mysql 在保存 json 格式字段的时候会自动格式化该字段的值 | ||
// mariadb 的话就没问题 | ||
assertEquals(Json.toJson(result, JsonFormat.tidy()), record.getString("noneAdaptor")); | ||
assertEquals(Json.toJson(result, JsonFormat.tidy()), record.getString("noneAdaptor")); | ||
assertEquals(Json.toJson(result, JsonFormat.tidy()), record.getString("jsonAdaptor")); | ||
assertEquals(Json.toJson(result, JsonFormat.compact()), record.getString("jsonCompactAdaptor")); | ||
assertEquals(Json.toJson(result, JsonFormat.tidy()), record.getString("jsonTidyAdaptor")); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
test/org/nutz/dao/test/normal/mysql/MysqlJsonAdaptorTestBean.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,68 @@ | ||
package org.nutz.dao.test.normal.mysql; | ||
|
||
import org.nutz.dao.entity.annotation.ColDefine; | ||
import org.nutz.dao.entity.annotation.ColType; | ||
import org.nutz.dao.entity.annotation.Id; | ||
import org.nutz.dao.entity.annotation.Table; | ||
import org.nutz.dao.impl.jdbc.mysql.MysqlJsonAdaptor; | ||
import org.nutz.dao.impl.jdbc.mysql.MysqlJsonCompactAdaptor; | ||
import org.nutz.dao.impl.jdbc.mysql.MysqlJsonTidyAdaptor; | ||
|
||
@Table("t_mysql_json_adaptor_test_bean") | ||
public class MysqlJsonAdaptorTestBean { | ||
|
||
@Id | ||
private int id; | ||
|
||
@ColDefine(customType = "json", type = ColType.MYSQL_JSON) | ||
private StudentResult noneAdaptor; | ||
|
||
@ColDefine(customType = "json", type = ColType.MYSQL_JSON, adaptor = MysqlJsonAdaptor.class) | ||
private StudentResult jsonAdaptor; | ||
|
||
@ColDefine(customType = "json", type = ColType.MYSQL_JSON, adaptor = MysqlJsonCompactAdaptor.class) | ||
private StudentResult jsonCompactAdaptor; | ||
|
||
@ColDefine(customType = "json", type = ColType.MYSQL_JSON, adaptor = MysqlJsonTidyAdaptor.class) | ||
private StudentResult jsonTidyAdaptor; | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public StudentResult getNoneAdaptor() { | ||
return noneAdaptor; | ||
} | ||
|
||
public void setNoneAdaptor(StudentResult noneAdaptor) { | ||
this.noneAdaptor = noneAdaptor; | ||
} | ||
|
||
public StudentResult getJsonAdaptor() { | ||
return jsonAdaptor; | ||
} | ||
|
||
public void setJsonAdaptor(StudentResult jsonAdaptor) { | ||
this.jsonAdaptor = jsonAdaptor; | ||
} | ||
|
||
public StudentResult getJsonCompactAdaptor() { | ||
return jsonCompactAdaptor; | ||
} | ||
|
||
public void setJsonCompactAdaptor(StudentResult jsonCompactAdaptor) { | ||
this.jsonCompactAdaptor = jsonCompactAdaptor; | ||
} | ||
|
||
public StudentResult getJsonTidyAdaptor() { | ||
return jsonTidyAdaptor; | ||
} | ||
|
||
public void setJsonTidyAdaptor(StudentResult jsonTidyAdaptor) { | ||
this.jsonTidyAdaptor = jsonTidyAdaptor; | ||
} | ||
} |
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
47 changes: 47 additions & 0 deletions
47
test/org/nutz/dao/test/normal/psql/PsqlJsonAdaptorTest.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,47 @@ | ||
package org.nutz.dao.test.normal.psql; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.math.BigDecimal; | ||
|
||
import org.junit.Test; | ||
import org.nutz.dao.Cnd; | ||
import org.nutz.dao.test.DaoCase; | ||
import org.nutz.json.Json; | ||
import org.nutz.json.JsonFormat; | ||
|
||
public class PsqlJsonAdaptorTest extends DaoCase { | ||
|
||
@Override | ||
protected void before() { | ||
if (!dao.meta().isPostgresql()) { | ||
return; | ||
} | ||
dao.create(PsqlJsonAdaptorTestBean.class, true); | ||
} | ||
|
||
@Test | ||
public void adapotor() { | ||
if (!dao.meta().isPostgresql()) { | ||
return; | ||
} | ||
|
||
PsqlJsonAdaptorTestBean testBean = new PsqlJsonAdaptorTestBean(); | ||
org.nutz.dao.test.normal.psql.StudentResult result = new StudentResult(); | ||
result.setPhysics(new BigDecimal("100")); | ||
testBean.setNoneAdaptor(result); | ||
testBean.setJsonAdaptor(result); | ||
testBean.setJsonCompactAdaptor(result); | ||
testBean.setJsonTidyAdaptor(result); | ||
|
||
int insertId = dao.insert(testBean).getId(); | ||
|
||
org.nutz.dao.entity.Record record = dao.fetch("t_psql_json_adaptor_test_bean", Cnd.where("id","=",insertId)); | ||
// 设置成 jsonb 格式的时候会自动格式化该字段的值 | ||
assertEquals(Json.toJson(result, JsonFormat.tidy()), record.getString("noneAdaptor")); | ||
assertEquals(Json.toJson(result, JsonFormat.tidy()), record.getString("noneAdaptor")); | ||
assertEquals(Json.toJson(result, JsonFormat.tidy()), record.getString("jsonAdaptor")); | ||
assertEquals(Json.toJson(result, JsonFormat.compact()), record.getString("jsonCompactAdaptor")); | ||
assertEquals(Json.toJson(result, JsonFormat.tidy()), record.getString("jsonTidyAdaptor")); | ||
} | ||
} |
Oops, something went wrong.