let’s say we need now we need a method
is all the information in the query table
we’ll write
@Override
public List<User> selectAll() {
SqlSession sqlSession = factory.openSession();
List<User> userList = sqlSession.selectList("com.tubai.dao.UserDao.selectAll");
sqlSession.close();
return userList;
}
is mainly through the sqlSession selectList to achieve the function
so how does Mybatis do this work for us?
pursue source
let’s start
in debug mode and step into
when this sentence is executed
userDao = sqlSession.getMapper(UserDao.class);
to p>
public <T> T getMapper(Class<T> type) {
return this.configuration.getMapper(type, this);
}
next->
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
return this.mapperRegistry.getMapper(type, sqlSession);
}
->
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory)this.knownMappers.get(type);
if (mapperProxyFactory == null) {
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
} else {
try {
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception var5) {
throw new BindingException("Error getting mapper instance. Cause: " + var5, var5);
}
}
}
we found that
was executed
return mapperProxyFactory.newInstance(sqlSession);
continue into ->
public T newInstance(SqlSession sqlSession) {
MapperProxy<T> mapperProxy = new MapperProxy(sqlSession, this.mapperInterface, this.methodCache);
return this.newInstance(mapperProxy);
}
->
protected T newInstance(MapperProxy<T> mapperProxy) {
return Proxy.newProxyInstance(this.mapperInterface.getClassLoader(), new Class[]{this.mapperInterface}, mapperProxy);
}
here we find that the newProxyInstance method that calls the dynamic proxy
so let’s go straight to the third parameter
open the class MapperProxy
find the invoke method inside
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, args);
}
if (this.isDefaultMethod(method)) {
return this.invokeDefaultMethod(proxy, method, args);
}
} catch (Throwable var5) {
throw ExceptionUtil.unwrapThrowable(var5);
}
MapperMethod mapperMethod = this.cachedMapperMethod(method);
return mapperMethod.execute(this.sqlSession, args);
}
finds that it finally calls
mapperMethod.execute(this.sqlSession, args);
open the source code for this method and continue
public Object execute(SqlSession sqlSession, Object[] args) {
Object param;
Object result;
switch(this.command.getType()) {
case INSERT:
param = this.method.convertArgsToSqlCommandParam(args);
result = this.rowCountResult(sqlSession.insert(this.command.getName(), param));
break;
case UPDATE:
param = this.method.convertArgsToSqlCommandParam(args);
result = this.rowCountResult(sqlSession.update(this.command.getName(), param));
break;
case DELETE:
param = this.method.convertArgsToSqlCommandParam(args);
result = this.rowCountResult(sqlSession.delete(this.command.getName(), param));
break;
case SELECT:
if (this.method.returnsVoid() && this.method.hasResultHandler()) {
this.executeWithResultHandler(sqlSession, args);
result = null;
} else if (this.method.returnsMany()) {
result = this.executeForMany(sqlSession, args);
} else if (this.method.returnsMap()) {
result = this.executeForMap(sqlSession, args);
} else if (this.method.returnsCursor()) {
result = this.executeForCursor(sqlSession, args);
} else {
param = this.method.convertArgsToSqlCommandParam(args);
result = sqlSession.selectOne(this.command.getName(), param);
}
break;
case FLUSH:
result = sqlSession.flushStatements();
break;
default:
throw new BindingException("Unknown execution method for: " + this.command.getName());
}
if (result == null && this.method.getReturnType().isPrimitive() && !this.method.returnsVoid()) {
throw new BindingException("Mapper method '" + this.command.getName() + " attempted to return null from a method with a primitive return type (" + this.method.getReturnType() + ").");
} else {
return result;
}
}
in Case:SELECT we find
result = this.executeForMany(sqlSession, args);
let’s do
private <E> Object executeForMany(SqlSession sqlSession, Object[] args) {
Object param = this.method.convertArgsToSqlCommandParam(args);
List result;
if (this.method.hasRowBounds()) {
RowBounds rowBounds = this.method.extractRowBounds(args);
result = sqlSession.selectList(this.command.getName(), param, rowBounds);
} else {
result = sqlSession.selectList(this.command.getName(), param);
}
if (!this.method.getReturnType().isAssignableFrom(result.getClass())) {
return this.method.getReturnType().isArray() ? this.convertToArray(result) : this.convertToDeclaredCollection(sqlSession.getConfiguration(), result);
} else {
return result;
}
}~
and finally we found
sqlSession.selectList
div>
Read More:
- When using CSS to write the registration page, we found that the editing effect is not effective
- When we crawl to the HTTPS website, the SSL certificate error is solved
- In thinkphp5, we encountered the problem of class’ phpoffice / phpspredsheet / spreadsheet ‘not found
- To solve this problem, we can’t find the solution of / storage / emulated / 0 / file. I can’t. You can try this
- Error failed to build IOS project. We ran “xcodebuild” command but it exited wit
- Python bug: cannot install ‘Django’. It is a distutils installed project and thus we cannot
- How do I change the default background color of all FIGURE objects created in MATLAB
- After the installation of vs2017, we can’t find the source files such as windows. H, stdio. H, etc
- Do you want to get fired? Let’s take a look at the programmer’s resignation tips
- The @ Autowired annotation in springboot is invalid in ordinary classes. How to solve and use the null pointer exception java.lang.nullpointerexception
- Mybatis passes in multiple parameters to mapper. And uses @param details to report an error
- Using code to create control objects, set the event listening method does not execute
- Error attempting to get column ‘xxxxx’ from result set — after Lombok is annotated with builder, mybatis cannot recognize the correct type of field
- Why not manage controller in spring container
- Module build failed Error Plugin/Preset files are not allowed to export objects, only functions (How to Fix)
- Mmdetection reports an error when running its own coco dataset. Does not match the length of \ ` classes \ ` 80) in cocodataset
- Could not write JSON: write javaBean error, fastjson version x.x.x, class
- How to Fix Sklearn ValueError: This solver needs samples of at least 2 classes in the data, but the data
- Three ways to get form data in struct2
- Generating equals/hashCode implementation but without a call to superclass