get、set方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| public class FlagEntity {
private String flag;
private String flagIsServiceApp;
private String flagIsMobileApp;
private String flagIsWebApp;
private String flagIsTopApp;
public void initFlag() { StringBuilder flag = new StringBuilder(); if (this.flagIsServiceApp == null) { this.flagIsServiceApp = "0"; } if (this.flagIsMobileApp == null) { this.flagIsMobileApp = "0"; } if (this.flagIsWebApp == null) { this.flagIsWebApp = "0"; } if (this.flagIsTopApp == null) { this.flagIsTopApp = "0"; } flag.append(this.flagIsServiceApp).append(this.flagIsMobileApp).append(this.flagIsWebApp) .append(this.flagIsTopApp).append("000000"); setFlag(flag.toString()); } public String getFlag() { return flag; }
public void setFlag(String flag) { if (flag == null) { this.flag = flag; return; } if (flag.length() != 10) { throw new IllegalArgumentException("Illegal flag length"); } if (!flag.matches("^[0-9]*$")) { throw new IllegalArgumentException("Only numbers are allowed for flag"); } this.flag = flag; this.flagIsServiceApp = this.flag.substring(0, 1); this.flagIsMobileApp = this.flag.substring(1, 2); this.flagIsWebApp = this.flag.substring(2, 3); this.flagIsTopApp = this.flag.substring(3, 4); }
public String getFlagIsServiceApp() { if (StringUtils.isNotBlank(this.flagIsServiceApp)) { return this.flagIsServiceApp; } if (StringUtils.isNotBlank(this.flag)) { return this.flag.substring(0, 1); } return null; }
public void setFlagIsServiceApp(String flagIsServiceApp) { this.flagIsServiceApp = flagIsServiceApp; if (StringUtils.isNotBlank(this.flag)) { this.flag = flagIsServiceApp + this.flag.substring(1); } }
public String getFlagIsMobileApp() { if (StringUtils.isNotBlank(this.flagIsMobileApp)) { return this.flagIsMobileApp; } if (StringUtils.isNotBlank(this.flag)) { return this.flag.substring(1, 2); } return null; }
public void setFlagIsMobileApp(String flagIsMobileApp) { this.flagIsMobileApp = flagIsMobileApp; if (StringUtils.isNotBlank(this.flag)) { this.flag = this.flag.substring(0, 1) + flagIsMobileApp + this.flag.substring(2); } }
public String getFlagIsWebApp() { if (StringUtils.isNotBlank(this.flagIsWebApp)) { return this.flagIsWebApp; } if (StringUtils.isNotBlank(this.flag)) { return this.flag.substring(2, 3); } return null; }
public void setFlagIsWebApp(String flagIsWebApp) { this.flagIsWebApp = flagIsWebApp; if (StringUtils.isNotBlank(this.flag)) { this.flag = this.flag.substring(0, 2) + flagIsWebApp + this.flag.substring(3); } }
public String getFlagIsTopApp() { if (StringUtils.isNotBlank(this.flagIsTopApp)) { return this.flagIsTopApp; } if (StringUtils.isNotBlank(this.flag)) { return this.flag.substring(3, 4); } return null; }
public void setFlagIsTopApp(String flagIsTopApp) { this.flagIsTopApp = flagIsTopApp; if (StringUtils.isNotBlank(this.flag)) { this.flag = this.flag.substring(0, 3) + flagIsTopApp + this.flag.substring(4); } }
}
|
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
public class Test {
public static void main(String[] args) {
FlagEntity flagEntity1 = new FlagEntity(); System.out.println(flagEntity1.getFlagIsServiceApp()); System.out.println(flagEntity1.getFlagIsMobileApp()); System.out.println(flagEntity1.getFlagIsWebApp()); System.out.println(flagEntity1.getFlagIsTopApp());
FlagEntity flagEntity2 = new FlagEntity(); flagEntity2.setFlag("1010000000"); System.out.println(flagEntity2.getFlagIsServiceApp()); System.out.println(flagEntity2.getFlagIsMobileApp()); System.out.println(flagEntity2.getFlagIsWebApp()); System.out.println(flagEntity2.getFlagIsTopApp());
FlagEntity flagEntity3 = new FlagEntity(); flagEntity3.setFlag("1010000000"); flagEntity3.setFlagIsServiceApp("0"); flagEntity3.setFlagIsMobileApp("1"); flagEntity3.setFlagIsWebApp("0"); flagEntity3.setFlagIsTopApp("1"); System.out.println(flagEntity3.getFlagIsServiceApp()); System.out.println(flagEntity3.getFlagIsMobileApp()); System.out.println(flagEntity3.getFlagIsWebApp()); System.out.println(flagEntity3.getFlagIsTopApp());
FlagEntity flagEntity4 = new FlagEntity(); flagEntity4.setFlagIsServiceApp("0"); flagEntity4.setFlagIsMobileApp("1"); flagEntity4.setFlagIsWebApp("0"); flagEntity4.setFlagIsTopApp("1"); System.out.println(flagEntity3.getFlagIsServiceApp()); System.out.println(flagEntity3.getFlagIsMobileApp()); System.out.println(flagEntity3.getFlagIsWebApp()); System.out.println(flagEntity3.getFlagIsTopApp());
}
}
|
mybatis XML 通用 SQL
mapper
1 2 3 4 5 6
| @Mapper public interface FlagMapper extends BaseMapper<FlagEntity> {
List<FlagEntity> selectList(@Param("entityParam") FlagEntity entityParam);
}
|
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <sql id="flagProperty"> <if test="entityParam != null"> <if test="entityParam.flagIsServiceApp != null"> and substr(TableName.flag, 1, 1) = #{entityParam.flagIsServiceApp,jdbcType=VARCHAR} </if> <if test="entityParam.flagIsMobileApp != null"> and substr(TableName.flag, 2, 1) = #{entityParam.flagIsMobileApp,jdbcType=VARCHAR} </if> <if test="entityParam.flagIsWebApp != null"> and substr(TableName.flag, 3, 1) = #{entityParam.flagIsWebApp,jdbcType=VARCHAR} </if> <if test="entityParam.flagIsTopApp != null"> and substr(TableName.flag, 4, 1) = #{entityParam.flagIsTopApp,jdbcType=VARCHAR} </if> </if> </sql>
|
使用指南
flag
和 其他属性
间的影响
若 flag
初始化成功,就一定会初始化或修改 其他属性
;
若 其他属性
初始化成功,只会修改 flag,并不会初始化 flag;
综上所得:
flag
非 null,其他属性
一定非 null;
flag
为 null,其他属性
可能为 null;
所以在 mapper XML 中的 flagProperty
(通用条件 SQL) 只需判断 其他属性
是否为 null ( <if test="entityParam.flagIsServiceApp != null">
)
initFlag()
方法
若无 initFlag()
方法,向数据库插入带有 flag 的数据时,前端需先处理好复选框中的选项,然后拼接成完成的 flag,传给后台。
initFlag()
方法省去前端拼接处理,只需将 其他属性
传给后台,后台再调用此方法即可初始化 flag
。