Skip to content

Commit ad54ef6

Browse files
committed
@Column注解增加insertableupdatable的支持
1 parent 661c915 commit ad54ef6

File tree

11 files changed

+342
-2
lines changed

11 files changed

+342
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ http://repo1.maven.org/maven2/javax/persistence/persistence-api/1.0/
123123
- 为了让扩展更方便,将`tk.mybatis.mapper.provider`包下所有的通用接口的实现方法改为了`String`形式。
124124
自己扩展单表操作的方法是非常容易的事情,建议有一定通用Mapper使用基础的自行扩展,扩展可以参考[如何扩展通用接口](http://git.oschina.net/free/Mapper/blob/master/wiki/mapper3/6.MyMapper.md)
125125
- 新增`SqlHelper`工具类,其中包含了大量可用的现成的SQL方法
126+
- `@Column`注解增加`insertable``updatable`的支持
126127

127128
###3.3.0 - 2015-11-01
128129

src/main/java/tk/mybatis/mapper/entity/EntityColumn.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ public class EntityColumn {
4545
private boolean uuid = false;
4646
private boolean identity = false;
4747
private String generator;
48+
//排序
4849
private String orderBy;
50+
//可插入
51+
private boolean insertable = true;
52+
//可更新
53+
private boolean updatable = true;
4954

5055
public EntityColumn() {
5156
}
@@ -150,6 +155,22 @@ public void setOrderBy(String orderBy) {
150155
this.orderBy = orderBy;
151156
}
152157

158+
public boolean isInsertable() {
159+
return insertable;
160+
}
161+
162+
public void setInsertable(boolean insertable) {
163+
this.insertable = insertable;
164+
}
165+
166+
public boolean isUpdatable() {
167+
return updatable;
168+
}
169+
170+
public void setUpdatable(boolean updatable) {
171+
this.updatable = updatable;
172+
}
173+
153174
/**
154175
* 返回格式如:colum = #{age,jdbcType=NUMERIC,typeHandler=MyTypeHandler}
155176
*

src/main/java/tk/mybatis/mapper/mapperhelper/EntityHelper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ private static void processField(EntityTable entityTable, Style style, EntityFie
248248
if (field.isAnnotationPresent(Column.class)) {
249249
Column column = field.getAnnotation(Column.class);
250250
columnName = column.name();
251+
entityColumn.setUpdatable(column.updatable());
252+
entityColumn.setInsertable(column.insertable());
251253
}
252254
//ColumnType
253255
if (field.isAnnotationPresent(ColumnType.class)) {

src/main/java/tk/mybatis/mapper/mapperhelper/SqlHelper.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,9 @@ public static String insertColumns(Class<?> entityClass, boolean skipId, boolean
339339
Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
340340
//当某个列有主键策略时,不需要考虑他的属性是否为空,因为如果为空,一定会根据主键策略给他生成一个值
341341
for (EntityColumn column : columnList) {
342+
if (!column.isInsertable()) {
343+
continue;
344+
}
342345
if (skipId && column.isId()) {
343346
continue;
344347
}
@@ -368,6 +371,9 @@ public static String insertValuesColumns(Class<?> entityClass, boolean skipId, b
368371
Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
369372
//当某个列有主键策略时,不需要考虑他的属性是否为空,因为如果为空,一定会根据主键策略给他生成一个值
370373
for (EntityColumn column : columnList) {
374+
if (!column.isInsertable()) {
375+
continue;
376+
}
371377
if (skipId && column.isId()) {
372378
continue;
373379
}
@@ -397,7 +403,7 @@ public static String updateSetColumns(Class<?> entityClass, String entityName, b
397403
Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
398404
//当某个列有主键策略时,不需要考虑他的属性是否为空,因为如果为空,一定会根据主键策略给他生成一个值
399405
for (EntityColumn column : columnList) {
400-
if (!column.isId()) {
406+
if (!column.isId() && column.isUpdatable()) {
401407
if (notNull) {
402408
sql.append(SqlHelper.getIfNotNull(entityName, column, column.getColumnEqualsHolder(entityName) + ",", notEmpty));
403409
} else {

src/main/java/tk/mybatis/mapper/provider/base/BaseInsertProvider.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public String insert(MappedStatement ms) {
7373
Boolean hasIdentityKey = false;
7474
//先处理cache或bind节点
7575
for (EntityColumn column : columnList) {
76+
if (!column.isInsertable()) {
77+
continue;
78+
}
7679
if (StringUtil.isNotEmpty(column.getSequenceName())) {
7780
} else if (column.isIdentity()) {
7881
//这种情况下,如果原先的字段有值,需要先缓存起来,否则就一定会使用自动增长
@@ -99,6 +102,9 @@ public String insert(MappedStatement ms) {
99102
sql.append(SqlHelper.insertColumns(entityClass, false, false, false));
100103
sql.append("<trim prefix=\"VALUES(\" suffix=\")\" suffixOverrides=\",\">");
101104
for (EntityColumn column : columnList) {
105+
if (!column.isInsertable()) {
106+
continue;
107+
}
102108
//优先使用传入的属性值,当原属性property!=null时,用原属性
103109
//自增的情况下,如果默认有值,就会备份到property_cache中,所以这里需要先判断备份的值是否存在
104110
if (column.isIdentity()) {
@@ -157,6 +163,9 @@ public String insertSelective(MappedStatement ms) {
157163
Boolean hasIdentityKey = false;
158164
//先处理cache或bind节点
159165
for (EntityColumn column : columnList) {
166+
if (!column.isInsertable()) {
167+
continue;
168+
}
160169
if (StringUtil.isNotEmpty(column.getSequenceName())) {
161170
//sql.append(column.getColumn() + ",");
162171
} else if (column.isIdentity()) {
@@ -183,6 +192,9 @@ public String insertSelective(MappedStatement ms) {
183192
sql.append(SqlHelper.insertIntoTable(entityClass, tableName(entityClass)));
184193
sql.append("<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">");
185194
for (EntityColumn column : columnList) {
195+
if (!column.isInsertable()) {
196+
continue;
197+
}
186198
if (StringUtil.isNotEmpty(column.getSequenceName()) || column.isIdentity() || column.isUuid()) {
187199
sql.append(column.getColumn() + ",");
188200
} else {
@@ -192,6 +204,9 @@ public String insertSelective(MappedStatement ms) {
192204
sql.append("</trim>");
193205
sql.append("<trim prefix=\"VALUES(\" suffix=\")\" suffixOverrides=\",\">");
194206
for (EntityColumn column : columnList) {
207+
if (!column.isInsertable()) {
208+
continue;
209+
}
195210
//优先使用传入的属性值,当原属性property!=null时,用原属性
196211
//自增的情况下,如果默认有值,就会备份到property_cache中,所以这里需要先判断备份的值是否存在
197212
if (column.isIdentity()) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014-2016 [email protected]
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package tk.mybatis.mapper.mapper;
26+
27+
import tk.mybatis.mapper.common.Mapper;
28+
import tk.mybatis.mapper.model.UserInfoAble;
29+
30+
/**
31+
* Created by liuzh on 2014/11/19.
32+
*/
33+
public interface UserInfoAbleMapper extends Mapper<UserInfoAble> {
34+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014-2016 [email protected]
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package tk.mybatis.mapper.model;
26+
27+
import javax.persistence.*;
28+
import java.io.Serializable;
29+
30+
/**
31+
* Created by liuzh on 2014/11/21.
32+
*/
33+
@Table(name = "user_info")
34+
public class UserInfoAble implements Serializable {
35+
36+
private static final long serialVersionUID = -7703830119762722918L;
37+
@Id
38+
@GeneratedValue(strategy = GenerationType.IDENTITY)
39+
private Integer id;
40+
private String username;
41+
private String password;
42+
private String usertype;
43+
private String realname;
44+
private String qq;
45+
private String email;
46+
private String address;
47+
private String tel;
48+
49+
public Integer getId() {
50+
return id;
51+
}
52+
53+
public void setId(Integer id) {
54+
this.id = id;
55+
}
56+
57+
public String getUsername() {
58+
return username;
59+
}
60+
61+
public void setUsername(String username) {
62+
this.username = username;
63+
}
64+
65+
public String getPassword() {
66+
return password;
67+
}
68+
69+
public void setPassword(String password) {
70+
this.password = password;
71+
}
72+
73+
public String getUsertype() {
74+
return usertype;
75+
}
76+
77+
public void setUsertype(String usertype) {
78+
this.usertype = usertype;
79+
}
80+
81+
public String getRealname() {
82+
return realname;
83+
}
84+
85+
public void setRealname(String realname) {
86+
this.realname = realname;
87+
}
88+
89+
public String getQq() {
90+
return qq;
91+
}
92+
93+
public void setQq(String qq) {
94+
this.qq = qq;
95+
}
96+
97+
public String getEmail() {
98+
return email;
99+
}
100+
101+
@Column(insertable = false)
102+
public void setEmail(String email) {
103+
this.email = email;
104+
}
105+
106+
public String getAddress() {
107+
return address;
108+
}
109+
110+
@Column(updatable = false)
111+
public void setAddress(String address) {
112+
this.address = address;
113+
}
114+
115+
public String getTel() {
116+
return tel;
117+
}
118+
119+
public void setTel(String tel) {
120+
this.tel = tel;
121+
}
122+
123+
@Override
124+
public String toString() {
125+
return "UserInfo{" +
126+
"id=" + id +
127+
", username='" + username + '\'' +
128+
", password='" + password + '\'' +
129+
", usertype='" + usertype + '\'' +
130+
", realname='" + realname + '\'' +
131+
", qq='" + qq + '\'' +
132+
", email='" + email + '\'' +
133+
", address='" + address + '\'' +
134+
", tel='" + tel + '\'' +
135+
'}';
136+
}
137+
}

0 commit comments

Comments
 (0)