Skip to content

Commit a0e476c

Browse files
committed
insert和update泛型增加<S extends T>
1 parent 60d3813 commit a0e476c

File tree

8 files changed

+53
-18
lines changed

8 files changed

+53
-18
lines changed

jpa/src/test/java/io/mybatis/provider/jpa/UserBaseMapperTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,26 @@
1616

1717
package io.mybatis.provider.jpa;
1818

19+
import io.mybatis.mapper.example.Example;
1920
import io.mybatis.provider.BaseTest;
2021
import org.apache.ibatis.session.SqlSession;
2122
import org.junit.Assert;
2223
import org.junit.Test;
2324

2425
public class UserBaseMapperTest extends BaseTest {
2526

27+
class UserExtend extends User {
28+
private String extend;
29+
30+
public String getExtend() {
31+
return extend;
32+
}
33+
34+
public void setExtend(String extend) {
35+
this.extend = extend;
36+
}
37+
}
38+
2639
@Test
2740
public void testSelectById() {
2841
try (SqlSession sqlSession = getSqlSession()) {
@@ -44,4 +57,26 @@ public void testSelectById() {
4457
}
4558
}
4659

60+
@Test
61+
public void testExtend() {
62+
try (SqlSession sqlSession = getSqlSession()) {
63+
UserBaseMapper userMapper = sqlSession.getMapper(UserBaseMapper.class);
64+
UserExtend userExtend = new UserExtend();
65+
userExtend.setId(999L);
66+
userExtend.setUsername("张无忌");
67+
userExtend.setExtend("扩展字段");
68+
Assert.assertEquals(1, userMapper.insert(userExtend));
69+
70+
Example<User> example = userMapper.example();
71+
example.createCriteria().andEqualTo(User::getId, userExtend.getId());
72+
userExtend.setUsername("test");
73+
Assert.assertEquals(1, userMapper.updateByExample(userExtend, example));
74+
75+
76+
int count = userMapper.deleteByPrimaryKey(userExtend.getId());
77+
Assert.assertEquals(1, count);
78+
sqlSession.rollback();
79+
}
80+
}
81+
4782
}

mapper/src/main/java/io/mybatis/mapper/BaseMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ default ExampleWrapper<T, I> wrapper() {
6060
*/
6161
@Lang(Caching.class)
6262
@UpdateProvider(type = FnProvider.class, method = "updateByPrimaryKeySelectiveWithForceFields")
63-
int updateByPrimaryKeySelectiveWithForceFields(@Param("entity") T entity, @Param("fns") Fn.Fns<T> forceUpdateFields);
63+
<S extends T> int updateByPrimaryKeySelectiveWithForceFields(@Param("entity") S entity, @Param("fns") Fn.Fns<T> forceUpdateFields);
6464

6565
/**
6666
* 根据指定字段集合查询:field in (fieldValueList)

mapper/src/main/java/io/mybatis/mapper/Mapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public interface Mapper<T, I extends Serializable> extends BaseMapper<T, I> {
4848
//@SelectKey(statement = "SELECT SEQ.NEXTVAL FROM DUAL", keyProperty = "id", before = true, resultType = long.class)
4949
@Options(useGeneratedKeys = true, keyProperty = "id")
5050
@InsertProvider(type = EntityProvider.class, method = "insert")
51-
int insert(T entity);
51+
<S extends T> int insert(S entity);
5252

5353
/**
5454
* 保存实体中不为空的字段,默认主键自增,并且名称为 id
@@ -63,6 +63,6 @@ public interface Mapper<T, I extends Serializable> extends BaseMapper<T, I> {
6363
//@SelectKey(statement = "SELECT SEQ.NEXTVAL FROM DUAL", keyProperty = "id", before = true, resultType = long.class)
6464
@Options(useGeneratedKeys = true, keyProperty = "id")
6565
@InsertProvider(type = EntityProvider.class, method = "insertSelective")
66-
int insertSelective(T entity);
66+
<S extends T> int insertSelective(S entity);
6767

6868
}

mapper/src/main/java/io/mybatis/mapper/base/EntityMapper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public interface EntityMapper<T, I> extends EntityInfoMapper<T> {
4040
*/
4141
@Lang(Caching.class)
4242
@InsertProvider(type = EntityProvider.class, method = "insert")
43-
int insert(T entity);
43+
<S extends T> int insert(S entity);
4444

4545
/**
4646
* 保存实体中不为空的字段
@@ -50,7 +50,7 @@ public interface EntityMapper<T, I> extends EntityInfoMapper<T> {
5050
*/
5151
@Lang(Caching.class)
5252
@InsertProvider(type = EntityProvider.class, method = "insertSelective")
53-
int insertSelective(T entity);
53+
<S extends T> int insertSelective(S entity);
5454

5555
/**
5656
* 根据主键删除
@@ -80,7 +80,7 @@ public interface EntityMapper<T, I> extends EntityInfoMapper<T> {
8080
*/
8181
@Lang(Caching.class)
8282
@UpdateProvider(type = EntityProvider.class, method = "updateByPrimaryKey")
83-
int updateByPrimaryKey(T entity);
83+
<S extends T> int updateByPrimaryKey(S entity);
8484

8585
/**
8686
* 根据主键更新实体中不为空的字段
@@ -90,7 +90,7 @@ public interface EntityMapper<T, I> extends EntityInfoMapper<T> {
9090
*/
9191
@Lang(Caching.class)
9292
@UpdateProvider(type = EntityProvider.class, method = "updateByPrimaryKeySelective")
93-
int updateByPrimaryKeySelective(T entity);
93+
<S extends T> int updateByPrimaryKeySelective(S entity);
9494

9595
/**
9696
* 根据主键查询实体

mapper/src/main/java/io/mybatis/mapper/example/ExampleMapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ default Example<T> example() {
6060
*/
6161
@Lang(Caching.class)
6262
@UpdateProvider(type = ExampleProvider.class, method = "updateByExample")
63-
int updateByExample(@Param("entity") T entity, @Param("example") E example);
63+
<S extends T> int updateByExample(@Param("entity") S entity, @Param("example") E example);
6464

6565
/**
6666
* 根据 Example 条件和 setValue 值更新字段
@@ -81,7 +81,7 @@ default Example<T> example() {
8181
*/
8282
@Lang(Caching.class)
8383
@UpdateProvider(type = ExampleProvider.class, method = "updateByExampleSelective")
84-
int updateByExampleSelective(@Param("entity") T entity, @Param("example") E example);
84+
<S extends T> int updateByExampleSelective(@Param("entity") S entity, @Param("example") E example);
8585

8686
/**
8787
* 根据 Example 条件批量查询

mapper/src/main/java/io/mybatis/mapper/fn/FnMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public interface FnMapper<T> {
4242
*/
4343
@Lang(Caching.class)
4444
@UpdateProvider(type = FnProvider.class, method = "updateByPrimaryKeySelectiveWithForceFields")
45-
int updateByPrimaryKeySelectiveWithForceFields(@Param("entity") T entity, @Param("fns") Fn.Fns<T> forceUpdateFields);
45+
<S extends T> int updateByPrimaryKeySelectiveWithForceFields(@Param("entity") S entity, @Param("fns") Fn.Fns<T> forceUpdateFields);
4646

4747
/**
4848
* 根据实体字段条件查询唯一的实体({@link io.mybatis.mapper.example.ExampleMapper} 可以实现一样的功能,当前方法只是示例)

mapper/src/main/java/io/mybatis/mapper/list/ListMapper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public interface ListMapper<T> {
4040
*/
4141
@Lang(Caching.class)
4242
@InsertProvider(type = ListProvider.class, method = "insertList")
43-
int insertList(@Param("entityList") List<? extends T> entityList);
43+
<S extends T> int insertList(@Param("entityList") List<S> entityList);
4444

4545
/**
4646
* 批量更新
@@ -50,7 +50,7 @@ public interface ListMapper<T> {
5050
*/
5151
@Lang(Caching.class)
5252
@UpdateProvider(type = ListProvider.class, method = "updateList")
53-
int updateList(@Param("entityList") List<? extends T> entityList);
53+
<S extends T> int updateList(@Param("entityList") List<S> entityList);
5454

5555

5656
/**
@@ -61,5 +61,5 @@ public interface ListMapper<T> {
6161
*/
6262
@Lang(Caching.class)
6363
@UpdateProvider(type = ListProvider.class, method = "updateListSelective")
64-
int updateListSelective(@Param("entityList") List<? extends T> entityList);
64+
<S extends T> int updateListSelective(@Param("entityList") List<S> entityList);
6565
}

mapper/src/main/java/io/mybatis/mapper/logical/LogicalMapper.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public interface LogicalMapper<T, I extends Serializable> extends BaseMapper<T,
2525
@Override
2626
@Lang(Caching.class)
2727
@UpdateProvider(type = LogicalProvider.class, method = "updateByPrimaryKeySelectiveWithForceFields")
28-
int updateByPrimaryKeySelectiveWithForceFields(@Param("entity") T entity, @Param("fns") Fn.Fns<T> forceUpdateFields);
28+
<S extends T> int updateByPrimaryKeySelectiveWithForceFields(@Param("entity") S entity, @Param("fns") Fn.Fns<T> forceUpdateFields);
2929

3030
/* BaseMapper --- */
3131

@@ -58,12 +58,12 @@ public interface LogicalMapper<T, I extends Serializable> extends BaseMapper<T,
5858
@Override
5959
@Lang(Caching.class)
6060
@UpdateProvider(type = LogicalProvider.class, method = "updateByPrimaryKey")
61-
int updateByPrimaryKey(T entity);
61+
<S extends T> int updateByPrimaryKey(S entity);
6262

6363
@Override
6464
@Lang(Caching.class)
6565
@UpdateProvider(type = LogicalProvider.class, method = "updateByPrimaryKeySelective")
66-
int updateByPrimaryKeySelective(T entity);
66+
<S extends T> int updateByPrimaryKeySelective(S entity);
6767

6868
@Override
6969
@Lang(Caching.class)
@@ -115,7 +115,7 @@ default Example<T> example() {
115115
@Override
116116
@Lang(Caching.class)
117117
@UpdateProvider(type = LogicalProvider.class, method = "updateByExample")
118-
int updateByExample(@Param("entity") T entity, @Param("example") Example<T> example);
118+
<S extends T> int updateByExample(@Param("entity") S entity, @Param("example") Example<T> example);
119119

120120
@Override
121121
@Lang(Caching.class)
@@ -125,7 +125,7 @@ default Example<T> example() {
125125
@Override
126126
@Lang(Caching.class)
127127
@UpdateProvider(type = LogicalProvider.class, method = "updateByExampleSelective")
128-
int updateByExampleSelective(@Param("entity") T entity, @Param("example") Example<T> example);
128+
<S extends T> int updateByExampleSelective(@Param("entity") S entity, @Param("example") Example<T> example);
129129

130130
@Override
131131
@Lang(Caching.class)

0 commit comments

Comments
 (0)