Skip to content

Commit

Permalink
去掉主键自增自动赋值,麻烦搞不定
Browse files Browse the repository at this point in the history
  • Loading branch information
deng-hb committed Aug 25, 2016
1 parent a2d4299 commit 434a76d
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 20 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.denghb</groupId>
<artifactId>dbhelper</artifactId>
<version>1.7</version>
<version>1.8</version>
<packaging>jar</packaging>

<name>dbhelper</name>
Expand Down
19 changes: 0 additions & 19 deletions src/main/java/com/denghb/dbhelper/DbHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ public boolean insert(Object object) {
// 计数器
int count = 0;

// 主键字段
Field idField = null;
boolean idAuto = false;
try {
// 分析列
Field[] fields = object.getClass().getDeclaredFields();
Expand All @@ -69,12 +66,6 @@ public boolean insert(Object object) {
if ("serialVersionUID".equals(fieldName)) {
continue;
}
// ID
Id id = field.getAnnotation(Id.class);
if (null != id) {
idField = field;
idAuto = id.auto();
}
Column column = field.getAnnotation(Column.class);
if (null == column) {
// 没有列注解的直接跳过
Expand Down Expand Up @@ -108,16 +99,6 @@ public boolean insert(Object object) {
// 执行insert
boolean res = 1 == execute(sql.toString(), objects);

// TODO MySql 成功了获取自动生成的ID
if (res && idAuto) {
try {
Long id = queryForObject("select LAST_INSERT_ID() as id", Long.class);
idField.setAccessible(true);
idField.set(object, id);
} catch (Exception e) {
log.warn("id only MySql ..." + e.getMessage(), e);
}
}
return res;

} catch (Exception e) {
Expand Down

0 comments on commit 434a76d

Please sign in to comment.