Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

一个Mapper中支持多个不同类型的provider #908

Merged
merged 3 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package tk.mybatis.mapper.helper;

import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.session.SqlSession;
import org.junit.Assert;
import org.junit.Test;
import tk.mybatis.mapper.annotation.RegisterMapper;
import tk.mybatis.mapper.mapper.CountryMultipleMapper;
import tk.mybatis.mapper.mapper.MybatisHelper;
import tk.mybatis.mapper.model.Country;
import tk.mybatis.mapper.provider.base.BaseInsertProvider;
import tk.mybatis.mapper.provider.base.BaseSelectProvider;

import java.util.List;

/**
* @author yuanhao
*/
public class MultipleMapperProviderTest {
@Test
public void test() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMultipleMapper mapper = sqlSession.getMapper(CountryMultipleMapper.class);
Country country = new Country();
country.setId(200);
country.setCountrycode("AB");
mapper.insert(country);
List<Country> countryList = mapper.select(country);
Assert.assertEquals("AB", countryList.get(0).getCountrycode());
} finally {
sqlSession.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package tk.mybatis.mapper.mapper;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.base.insert.InsertMapper;
import tk.mybatis.mapper.common.base.select.SelectMapper;
import tk.mybatis.mapper.model.Country;

public interface CountryMultipleMapper
extends MultipleCommonMapper<Country> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package tk.mybatis.mapper.mapper;

import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.SelectProvider;
import tk.mybatis.mapper.annotation.RegisterMapper;
import tk.mybatis.mapper.provider.base.BaseInsertProvider;
import tk.mybatis.mapper.provider.base.BaseSelectProvider;

import java.util.List;

@RegisterMapper
public interface MultipleCommonMapper<T> {
@SelectProvider(type = BaseSelectProvider.class, method = "dynamicSQL")
List<T> select(T record);
@InsertProvider(type = BaseInsertProvider.class, method = "dynamicSQL")
int insert(T record);
}
1 change: 1 addition & 0 deletions base/src/test/resources/mybatis-java.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@

<mappers>
<mapper resource="CachedCountryMapper.xml"/>
<mapper class="tk.mybatis.mapper.mapper.CountryMultipleMapper"/>
<mapper class="tk.mybatis.mapper.mapper.CountryMapper"/>
<mapper class="tk.mybatis.mapper.mapper.Country2Mapper"/>
<mapper class="tk.mybatis.mapper.mapper.CountryTMapper"/>
Expand Down
69 changes: 29 additions & 40 deletions core/src/main/java/tk/mybatis/mapper/mapperhelper/MapperHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class MapperHelper {
/**
* 注册的通用Mapper接口
*/
private Map<Class<?>, MapperTemplate> registerMapper = new ConcurrentHashMap<Class<?>, MapperTemplate>();
private Map<Class<?>, Collection<MapperTemplate>> registerMapper = new ConcurrentHashMap<Class<?>, Collection<MapperTemplate>>();

/**
* 通用Mapper配置
Expand All @@ -87,64 +87,51 @@ public MapperHelper(Properties properties) {
this();
setProperties(properties);
}

/**
* 通过通用Mapper接口获取对应的MapperTemplate
*
* @param mapperClass
* @return
* @throws Exception
*/
private MapperTemplate fromMapperClass(Class<?> mapperClass) {
private Collection<MapperTemplate> fromMapperClasses(Class<?> mapperClass) {
Map<Class<?>,MapperTemplate> templateMap = new ConcurrentHashMap<Class<?>, MapperTemplate>();
Method[] methods = mapperClass.getDeclaredMethods();
Class<?> templateClass = null;
Class<?> tempClass = null;
Set<String> methodSet = new HashSet<String>();
for (Method method : methods) {
Class<?> templateClass = null;
if (method.isAnnotationPresent(SelectProvider.class)) {
SelectProvider provider = method.getAnnotation(SelectProvider.class);
tempClass = provider.type();
methodSet.add(method.getName());
templateClass = provider.type();
} else if (method.isAnnotationPresent(InsertProvider.class)) {
InsertProvider provider = method.getAnnotation(InsertProvider.class);
tempClass = provider.type();
methodSet.add(method.getName());
templateClass = provider.type();
} else if (method.isAnnotationPresent(DeleteProvider.class)) {
DeleteProvider provider = method.getAnnotation(DeleteProvider.class);
tempClass = provider.type();
methodSet.add(method.getName());
templateClass = provider.type();
} else if (method.isAnnotationPresent(UpdateProvider.class)) {
UpdateProvider provider = method.getAnnotation(UpdateProvider.class);
tempClass = provider.type();
methodSet.add(method.getName());
templateClass = provider.type();
}
if (templateClass == null) {
templateClass = tempClass;
} else if (templateClass != tempClass) {
log.error("一个通用Mapper中只允许存在一个MapperTemplate子类!");
throw new MapperException("一个通用Mapper中只允许存在一个MapperTemplate子类!");
if (templateClass == null || !MapperTemplate.class.isAssignableFrom(templateClass)) {
templateClass = EmptyProvider.class;
}
}
if (templateClass == null || !MapperTemplate.class.isAssignableFrom(templateClass)) {
templateClass = EmptyProvider.class;
}
MapperTemplate mapperTemplate = null;
try {
mapperTemplate = (MapperTemplate) templateClass.getConstructor(Class.class, MapperHelper.class).newInstance(mapperClass, this);
} catch (Exception e) {
log.error("实例化MapperTemplate对象失败:" + e, e);
throw new MapperException("实例化MapperTemplate对象失败:" + e.getMessage());
}
//注册方法
for (String methodName : methodSet) {
MapperTemplate mapperTemplate;
try {
mapperTemplate.addMethodMap(methodName, templateClass.getMethod(methodName, MappedStatement.class));
mapperTemplate = templateMap.getOrDefault(templateClass,(MapperTemplate) templateClass.getConstructor(Class.class, MapperHelper.class).newInstance(mapperClass, this));;
templateMap.put(templateClass, mapperTemplate);
} catch (Exception e) {
log.error("实例化MapperTemplate对象失败:" + e, e);
throw new MapperException("实例化MapperTemplate对象失败:" + e.getMessage());
}
//注册方法
try {
mapperTemplate.addMethodMap(method.getName(), templateClass.getMethod(method.getName(), MappedStatement.class));
} catch (NoSuchMethodException e) {
log.error(templateClass.getName() + "中缺少" + methodName + "方法!", e);
throw new MapperException(templateClass.getName() + "中缺少" + methodName + "方法!");
log.error(templateClass.getName() + "中缺少" + method.getName() + "方法!", e);
throw new MapperException(templateClass.getName() + "中缺少" + method.getName() + "方法!");
}
}
return mapperTemplate;
return templateMap.values();
}

/**
Expand All @@ -155,7 +142,7 @@ private MapperTemplate fromMapperClass(Class<?> mapperClass) {
public void registerMapper(Class<?> mapperClass) {
if (!registerMapper.containsKey(mapperClass)) {
registerClass.add(mapperClass);
registerMapper.put(mapperClass, fromMapperClass(mapperClass));
registerMapper.put(mapperClass,fromMapperClasses(mapperClass));
}
//自动注册继承的接口
Class<?>[] interfaces = mapperClass.getInterfaces();
Expand Down Expand Up @@ -209,9 +196,11 @@ public MapperTemplate isMapperMethod(String msId) {
* @return
*/
public MapperTemplate getMapperTemplateByMsId(String msId) {
for (Map.Entry<Class<?>, MapperTemplate> entry : registerMapper.entrySet()) {
if (entry.getValue().supportMethod(msId)) {
return entry.getValue();
for (Map.Entry<Class<?>, Collection<MapperTemplate>> entry : registerMapper.entrySet()) {
for (MapperTemplate t : entry.getValue()) {
if (t.supportMethod(msId)) {
return t;
}
}
}
return null;
Expand Down
Loading