支付宝二面:Mybatis接口Mapper内的方法为啥不能重载吗?我直接懵逼了...
共 4541字,需浏览 10分钟
·
2020-09-10 23:33
阅读本文大概需要 4 分钟。
动态代理的功能:通过拦截器方法回调,对目标target方法进行增强。
1. 自定义JDK动态代理之投鞭断流实现自动映射器Mapper
public class User {
private Integer id;
private String name;
private int age;
public User(Integer id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
// getter setter
}
public interface UserMapper {
public User getUserById(Integer id);
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class MapperProxy implements InvocationHandler {
@SuppressWarnings("unchecked")
publicT newInstance(Class clz) {
return (T) Proxy.newProxyInstance(clz.getClassLoader(), new Class[] { clz }, this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (Object.class.equals(method.getDeclaringClass())) {
try {
// 诸如hashCode()、toString()、equals()等方法,将target指向当前对象this
return method.invoke(this, args);
} catch (Throwable t) {
}
}
// 投鞭断流
return new User((Integer) args[0], "zhangsan", 18);
}
}
public static void main(String[] args) {
MapperProxy proxy = new MapperProxy();
UserMapper mapper = proxy.newInstance(UserMapper.class);
User user = mapper.getUserById(1001);
System.out.println("ID:" + user.getId());
System.out.println("Name:" + user.getName());
System.out.println("Age:" + user.getAge());
System.out.println(mapper.toString());
}
ID:1001
Name:zhangsan
Age:18
x.y.MapperProxy@6bc7c054
2. Mybatis自动映射器Mapper的源码分析
public static void main(String[] args) {
SqlSession sqlSession = MybatisSqlSessionFactory.openSession();
try {
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
Liststudents = studentMapper.findAllStudents();
for (Student student : students) {
System.out.println(student);
}
} finally {
sqlSession.close();
}
}
public interface StudentMapper {
ListfindAllStudents();
Student findStudentById(Integer id);
void insertStudent(Student student);
}
public class MapperProxy
implements InvocationHandler, Serializable {
private static final long serialVersionUID = -6424540398559729838L;
private final SqlSession sqlSession;
private final ClassmapperInterface;
private final MapmethodCache;
public MapperProxy(SqlSession sqlSession, ClassmapperInterface, Map methodCache) {
this.sqlSession = sqlSession;
this.mapperInterface = mapperInterface;
this.methodCache = methodCache;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (Object.class.equals(method.getDeclaringClass())) {
try {
return method.invoke(this, args);
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
}
// 投鞭断流
final MapperMethod mapperMethod = cachedMapperMethod(method);
return mapperMethod.execute(sqlSession, args);
}
// ...
public class MapperProxyFactory
{
private final ClassmapperInterface;
@SuppressWarnings("unchecked")
protected T newInstance(MapperProxymapperProxy) {
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}
3. 接口Mapper内的方法能重载(overLoad)吗?(重要)
public User getUserById(Integer id);
public User getUserById(Integer id, String name);
推荐阅读:
微信扫描二维码,关注我的公众号
朕已阅