Skip to content

Commit 2dafa13

Browse files
committed
解决 @keysql GenId 覆盖已有 ID 的问题 fixed abel533#482
1 parent 59bebcb commit 2dafa13

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

base/src/test/java/tk/mybatis/mapper/base/genid/InsertGenIdTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,22 @@ public void testGenId(){
8787
}
8888
}
8989

90+
@Test
91+
public void testGenIdWithExistsId(){
92+
SqlSession sqlSession = getSqlSession();
93+
try {
94+
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
95+
Country country = new Country("test", "T");
96+
country.setId(9999L);
97+
Assert.assertEquals(1, mapper.insert(country));
98+
Assert.assertNotNull(country.getId());
99+
Assert.assertEquals(new Long(9999), country.getId());
100+
System.out.println(country.getId());
101+
} finally {
102+
sqlSession.close();
103+
}
104+
}
105+
90106

91107
@Test
92108
public void testUUID(){

core/src/main/java/tk/mybatis/mapper/genid/GenIdUtil.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ public static void genId(Object target, String property, Class<? extends GenId>
6868
LOCK.unlock();
6969
}
7070
}
71-
Object id = genId.genId(table, column);
72-
//赋值
7371
MetaObject metaObject = MetaObjectUtil.forObject(target);
74-
metaObject.setValue(property, id);
72+
if (metaObject.getValue(property) == null) {
73+
Object id = genId.genId(table, column);
74+
metaObject.setValue(property, id);
75+
}
7576
} catch (Exception e) {
7677
throw new MapperException("生成 ID 失败!", e);
7778
}

0 commit comments

Comments
 (0)