1. 程式人生 > 程式設計 >mybatis3使用@Select等註解實現增刪改查操作

mybatis3使用@Select等註解實現增刪改查操作

1.需要的jar包

2.目錄樹

3.具體程式碼

一.需要的jar包

mybatis3使用@Select等註解實現增刪改查操作

第一個:mybatis的jar包

第二個:mysql資料的驅動

二.目錄樹

mybatis3使用@Select等註解實現增刪改查操作

三.具體程式碼

使用框架,配置檔案先行!

conf.xml:(配置 登入資料庫,對映檔案)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
 <environments default="mysql">
  <environment id="mysql">
  <transactionManager type="JDBC"/>
  <dataSource type="POOLED">
   <property name="driver" value="com.mysql.jdbc.Driver"/>
   <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
   <property name="username" value="root"/>
   <property name="password" value="zhujunwen"/>
  </dataSource> 
  </environment>
 </environments>
 
 <!-- 配置對映-->
 <mappers>
 <!-- 若對映檔案mapper 是xml,則<mapper recourse...>,若對映檔案為java檔案,則<mapper class.....> -->
 <mapper class="com.UserMapper"/> 
 </mappers>
</configuration>

對映檔案:

UserMapper.java:(用於對映SQL語句)

package com; 
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
public interface UserMapper {
 
 /*
 這個一個介面,但不需要實現它,用於 函式與SQL語句 的對映
 * */
 
 @Insert("insert into tb_user(name,sex) values(#{name},#{sex})")
 public void insertT(User user);
 
 @Delete("delete from tb_user where id=#{id}")
 public void deleteById(int id);
 
 @Update("update tb_user set name=#{name},sex=#{sex} where id=#{id}")
 public void updateT(User user);
 
 @Select("select * from tb_user where id=#{id}")
 public User getUser(int id);
 
 @Select("select * from tb_user")
 public List<User> getAllUsers();
}

持久類:

User.java:(持久類中的成員變數要與資料表中的欄位名字一致)

package com;
 
public class User {
 private Integer id;
 private String name;
 private String sex;
 public Integer getId() {
 return id;
 }
 public void setId(Integer id) {
 this.id = id;
 }
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public String getSex() {
 return sex;
 }
 public void setSex(String sex) {
 this.sex = sex;
 }
 
 public String toString() {
 return "User [id=" + id + ",name=" + name + ",sex=" + sex + "]";
 }
}

必要變數的快速獲取:(獲取Session)

FKSqlSessionFactory.java:

package com; 
import java.io.IOException;
import java.io.InputStream; 
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 
public class FKSqlSessionFactory {
 private static SqlSessionFactory sqlSessionFactory = null;
 static{
 InputStream input;
 try {
 input = Resources.getResourceAsStream("conf.xml");
 sqlSessionFactory = new SqlSessionFactoryBuilder().build(input);
 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 }
 
 public static SqlSession getSqlSession(){
 return sqlSessionFactory.openSession();
 }
 
 public static SqlSessionFactory getSqlSessionFactory(){
 return sqlSessionFactory;
 }
}

測試檔案(只是演示對資料庫的插入)

package com; 
import java.util.ArrayList;
import java.util.List; 
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
 
public class Test1 { 
 public static void main(String[] args) {
 SqlSessionFactory factory= FKSqlSessionFactory.getSqlSessionFactory();
 SqlSession session = factory.openSession();
 UserMapper mapper = session.getMapper(UserMapper.class);//獲取對映器例項
 
 User user = new User();
 user.setName("zhujunwen");
 user.setSex("m"); 
 mapper.insertT(user); //呼叫對映器中的insertT()方法進行資料庫插入 
 session.commit();
 session.close();
 }
}

效果:

mybatis3使用@Select等註解實現增刪改查操作

已有資料插入到資料表

資料表的樣式:

mybatis3使用@Select等註解實現增刪改查操作

補充知識:mybatis 一次select操作執行流程分析

1.測試程式碼

package com.testmybatis; 
import java.io.IOException;
import java.io.InputStream;
import java.util.List; 
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger; 
import com.alibaba.fastjson.JSON;
import com.testmybatis.dao.TestMapper;
import com.testmybatis.model.Test;
 
public class testlanjie {
 
 private static Logger log=Logger.getLogger(testlanjie.class);
 
 public static void main(String args[]){
 List<Test> tests=null;
 try {
  String resource = "com/testmybatis/mybatis-config.xml";
  InputStream inputStream = Resources.getResourceAsStream(resource);
  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  SqlSession session = sqlSessionFactory.openSession();
  try {
  TestMapper mapper=session.getMapper(TestMapper.class);
  tests=mapper.test();
  session.commit();
  } finally {
  session.close();
  }
 
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 if(tests!=null)
  log.info(JSON.toJSONString(tests));
 }
 
}

2.流程分析

第一步構造SqlSessionFactory

String resource = "com/testmybatis/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

SqlSessionFactoryBuilder下的build函式

public SqlSessionFactory build(InputStream inputStream) {
  return build(inputStream,null,null);
 }
public SqlSessionFactory build(InputStream inputStream,String environment,Properties properties) {
  try {
   XMLConfigBuilder parser = new XMLConfigBuilder(inputStream,environment,properties);
   return build(parser.parse());
  } catch (Exception e) {
   throw ExceptionFactory.wrapException("Error building SqlSession.",e);
  } finally {
   ErrorContext.instance().reset();
   try {
    inputStream.close();
   } catch (IOException e) {
    // Intentionally ignore. Prefer previous error.
   }
  }
 }
public SqlSessionFactory build(Configuration config) {
  return new DefaultSqlSessionFactory(config);
 }

先用配置檔案的檔案流物件構造一個XMLConfigBuilder物件,在呼叫parse函式得到Configuration物件

public Configuration parse() {
  if (parsed) {
   throw new BuilderException("Each XMLConfigBuilder can only be used once.");
  }
  parsed = true;
  parseConfiguration(parser.evalNode("/configuration"));
  return configuration;
 }
 
 private void parseConfiguration(XNode root) {
  try {
   Properties settings = settingsAsPropertiess(root.evalNode("settings"));
   //issue #117 read properties first
   propertiesElement(root.evalNode("properties"));
   loadCustomVfs(settings);
   typeAliasesElement(root.evalNode("typeAliases"));
   pluginElement(root.evalNode("plugins"));
   objectFactoryElement(root.evalNode("objectFactory"));
   objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
   reflectorFactoryElement(root.evalNode("reflectorFactory"));
   settingsElement(settings);
   // read it after objectFactory and objectWrapperFactory issue #631
   environmentsElement(root.evalNode("environments"));
   databaseIdProviderElement(root.evalNode("databaseIdProvider"));
   typeHandlerElement(root.evalNode("typeHandlers"));
   mapperElement(root.evalNode("mappers"));
  } catch (Exception e) {
   throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e,e);
  }

其中mybatis主要的配置都在這裡完成,存放在configuration中,本次分析會用到的兩個函式是environmentsElement和mapperElement,一個是構造datasource,一個是構造存放所有MapperProxyFactory的MapperRegistry,相應的原始碼如下

private void environmentsElement(XNode context) throws Exception {
  if (context != null) {
   if (environment == null) {
    environment = context.getStringAttribute("default");
   }
   for (XNode child : context.getChildren()) {
    String id = child.getStringAttribute("id");
    if (isSpecifiedEnvironment(id)) {
     TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
     DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
     DataSource dataSource = dsFactory.getDataSource();
     Environment.Builder environmentBuilder = new Environment.Builder(id)
       .transactionFactory(txFactory)
       .dataSource(dataSource);
     configuration.setEnvironment(environmentBuilder.build());
    }
   }
  }
 }
private void mapperElement(XNode parent) throws Exception {
  if (parent != null) {
   for (XNode child : parent.getChildren()) {
    if ("package".equals(child.getName())) {
     String mapperPackage = child.getStringAttribute("name");
     configuration.addMappers(mapperPackage);
    } else {
     String resource = child.getStringAttribute("resource");
     String url = child.getStringAttribute("url");
     String mapperClass = child.getStringAttribute("class");
     if (resource != null && url == null && mapperClass == null) {
      ErrorContext.instance().resource(resource);
      InputStream inputStream = Resources.getResourceAsStream(resource);
      XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream,configuration,resource,configuration.getSqlFragments());
      mapperParser.parse();
     } else if (resource == null && url != null && mapperClass == null) {
      ErrorContext.instance().resource(url);
      InputStream inputStream = Resources.getUrlAsStream(url);
      XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream,url,configuration.getSqlFragments());
      mapperParser.parse();
     } else if (resource == null && url == null && mapperClass != null) {
      Class<?> mapperInterface = Resources.classForName(mapperClass);
      configuration.addMapper(mapperInterface);
     } else {
      throw new BuilderException("A mapper element may only specify a url,resource or class,but not more than one.");
     }
    }
   }
  }
 }

其中MapperRegistry物件中存放所有MapperProxyFactory物件的容器是一個hashmap

private final Map<Class<?>,MapperProxyFactory<?>> knownMappers = new HashMap<Class<?>,MapperProxyFactory<?>>();
 
public <T> void addMapper(Class<T> type) {
  if (type.isInterface()) {
   if (hasMapper(type)) {
    throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
   }
   boolean loadCompleted = false;
   try {
    knownMappers.put(type,new MapperProxyFactory<T>(type));
    // It's important that the type is added before the parser is run
    // otherwise the binding may automatically be attempted by the
    // mapper parser. If the type is already known,it won't try.
    MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config,type);
    parser.parse();
    loadCompleted = true;
   } finally {
    if (!loadCompleted) {
     knownMappers.remove(type);
    }
   }
  }
 }

在構造factory的最後返回了一個DefaultSqlSessionFactory物件,並將建立好的Configuration物件當作引數傳給了該物件,成為了他的成員變數。

第二步得到SqlSession物件

SqlSession session = sqlSessionFactory.openSession();

跟蹤獲取session的過程

 @Override
 public SqlSession openSession() {
  return openSessionFromDataSource(configuration.getDefaultExecutorType(),false);
 }
 private SqlSession openSessionFromDataSource(ExecutorType execType,TransactionIsolationLevel level,boolean autoCommit) {
  Transaction tx = null;
  try {
   final Environment environment = configuration.getEnvironment();
   final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
   tx = transactionFactory.newTransaction(environment.getDataSource(),level,autoCommit);
   final Executor executor = configuration.newExecutor(tx,execType);
   return new DefaultSqlSession(configuration,executor,autoCommit);
  } catch (Exception e) {
   closeTransaction(tx); // may have fetched a connection so lets call close()
   throw ExceptionFactory.wrapException("Error opening session. Cause: " + e,e);
  } finally {
   ErrorContext.instance().reset();
  }
 }

最終得到的DefaultSqlSession中包含兩個重要物件,一個是從configuration裡來的Enviroment,另外一個是包含TransactionFactroy物件的Executor物件。

其中獲得Executor物件的函式如下

public Executor newExecutor(Transaction transaction,ExecutorType executorType) {
  executorType = executorType == null ? defaultExecutorType : executorType;
  executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
  Executor executor;
  if (ExecutorType.BATCH == executorType) {
   executor = new BatchExecutor(this,transaction);
  } else if (ExecutorType.REUSE == executorType) {
   executor = new ReuseExecutor(this,transaction);
  } else {
   executor = new SimpleExecutor(this,transaction);
  }
  if (cacheEnabled) {
   executor = new CachingExecutor(executor);
  }
  executor = (Executor) interceptorChain.pluginAll(executor);
  return executor;
 }

這其中應用的包裝模式(將SimpleExecutor物件包裝成CachingExecutor)和責任鏈模式(給CachingExecutro物件裝配上plugin)

第三步 根據傳入的介面資訊生成動態代理

TestMapper mapper=session.getMapper(TestMapper.class);

繼續跟蹤程式碼

 @Override
 public <T> T getMapper(Class<T> type) {
  return configuration.<T>getMapper(type,this);
 }
 public <T> T getMapper(Class<T> type,SqlSession sqlSession) {
  return mapperRegistry.getMapper(type,sqlSession);
 }
@SuppressWarnings("unchecked")
 public <T> T getMapper(Class<T> type,SqlSession sqlSession) {
  final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
  if (mapperProxyFactory == null) {
   throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
  }
  try {
   return mapperProxyFactory.newInstance(sqlSession);
  } catch (Exception e) {
   throw new BindingException("Error getting mapper instance. Cause: " + e,e);
  }
 }
 public T newInstance(SqlSession sqlSession) {
  final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession,mapperInterface,methodCache);
  return newInstance(mapperProxy);
 }
@SuppressWarnings("unchecked")
 protected T newInstance(MapperProxy<T> mapperProxy) {
  return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(),new Class[] { mapperInterface },mapperProxy);
 }

大致流程就是先從configuration中找mapperRegistry,再從mapperRegistry中找當初存放mapperfactory的hashmap,再從這個hashmap中找到相應的mapperfactory,最後mapperfactory構造了一個mapperproxy物件,並呼叫了java的Proxy類,構造了一個動態代理物件的例項。

看一下MapperProxy的程式碼

/**
 *  Copyright 2009-2015 the original author or authors.
 *
 *  Licensed under the Apache License,Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,software
 *  distributed under the License is distributed on an "AS IS" BASIS,*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.ibatis.binding;
 
import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;
 
import org.apache.ibatis.reflection.ExceptionUtil;
import org.apache.ibatis.session.SqlSession;
 
/**
 * @author Clinton Begin
 * @author Eduardo Macarron
 */
public class MapperProxy<T> implements InvocationHandler,Serializable {
 
 private static final long serialVersionUID = -6424540398559729838L;
 private final SqlSession sqlSession;
 private final Class<T> mapperInterface;
 private final Map<Method,MapperMethod> methodCache;
 
 public MapperProxy(SqlSession sqlSession,Class<T> mapperInterface,Map<Method,MapperMethod> 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);
 }
 
 private MapperMethod cachedMapperMethod(Method method) {
  MapperMethod mapperMethod = methodCache.get(method);
  if (mapperMethod == null) {
   mapperMethod = new MapperMethod(mapperInterface,method,sqlSession.getConfiguration());
   methodCache.put(method,mapperMethod);
  }
  return mapperMethod;
 }
 
}

該class實現了InvocationHandler介面,我們知道java中的動態代理模式需要代理class來實現InvocationHandler介面,

而最後動態得到的例項呼叫函式實際上呼叫的是代理物件的invoke的函式,所以我們第四步的入口就在這個class裡

第四步 呼叫介面函式test,執行sql語句

由代理物件中的inove函式知道,函式最後執行的其實是MapperMethod類的execute函式。

一路跟蹤程式碼到SimpleExecutor下的doQuery函式中

 @Override
 public <E> List<E> doQuery(MappedStatement ms,Object parameter,RowBounds rowBounds,ResultHandler resultHandler,BoundSql boundSql) throws SQLException {
  Statement stmt = null;
  try {
   Configuration configuration = ms.getConfiguration();
   StatementHandler handler = configuration.newStatementHandler(wrapper,ms,parameter,rowBounds,resultHandler,boundSql);
   stmt = prepareStatement(handler,ms.getStatementLog());
   return handler.<E>query(stmt,resultHandler);
  } finally {
   closeStatement(stmt);
  }
 }

此處生成了一個StatementHandler物件和一個Statement物件

先看看Statement的生成過程

private Statement prepareStatement(StatementHandler handler,Log statementLog) throws SQLException {
  Statement stmt;
  Connection connection = getConnection(statementLog);
  stmt = handler.prepare(connection,transaction.getTimeout());
  handler.parameterize(stmt);
  return stmt;
 }

先生成Connection物件 ,用的也是代理模式

 protected Connection getConnection(Log statementLog) throws SQLException {
  Connection connection = transaction.getConnection();
  if (statementLog.isDebugEnabled()) {
   return ConnectionLogger.newInstance(connection,statementLog,queryStack);
  } else {
   return connection;
  }
 }
public static Connection newInstance(Connection conn,Log statementLog,int queryStack) {
  InvocationHandler handler = new ConnectionLogger(conn,queryStack);
  ClassLoader cl = Connection.class.getClassLoader();
  return (Connection) Proxy.newProxyInstance(cl,new Class[]{Connection.class},handler);
 }

然後生成preparestatement

 @Override
 public Statement prepare(Connection connection,Integer transactionTimeout) throws SQLException {
  ErrorContext.instance().sql(boundSql.getSql());
  Statement statement = null;
  try {
   statement = instantiateStatement(connection);
   setStatementTimeout(statement,transactionTimeout);
   setFetchSize(statement);
   return statement;
  } catch (SQLException e) {
   closeStatement(statement);
   throw e;
  } catch (Exception e) {
   closeStatement(statement);
   throw new ExecutorException("Error preparing statement. Cause: " + e,e);
  }
 }
 @Override
 protected Statement instantiateStatement(Connection connection) throws SQLException {
  String sql = boundSql.getSql();
  if (mappedStatement.getKeyGenerator() instanceof Jdbc3KeyGenerator) {
   String[] keyColumnNames = mappedStatement.getKeyColumns();
   if (keyColumnNames == null) {
    return connection.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
   } else {
    return connection.prepareStatement(sql,keyColumnNames);
   }
  } else if (mappedStatement.getResultSetType() != null) {
   return connection.prepareStatement(sql,mappedStatement.getResultSetType().getValue(),ResultSet.CONCUR_READ_ONLY);
  } else {
   return connection.prepareStatement(sql);
  }
 }

最後呼叫了connec tion的prepareStatement函式,而因為connection其實是一個代理物件,他實際呼叫的函式是

@Override
 public Object invoke(Object proxy,Object[] params)
   throws Throwable {
  try {
   if (Object.class.equals(method.getDeclaringClass())) {
    return method.invoke(this,params);
   }  
   if ("prepareStatement".equals(method.getName())) {
    if (isDebugEnabled()) {
     debug(" Preparing: " + removeBreakingWhitespace((String) params[0]),true);
    }    
    PreparedStatement stmt = (PreparedStatement) method.invoke(connection,params);
    //實際呼叫建立prepareedstatement物件的地方
    stmt = PreparedStatementLogger.newInstance(stmt,queryStack);
    return stmt;
   } else if ("prepareCall".equals(method.getName())) {
    if (isDebugEnabled()) {
     debug(" Preparing: " + removeBreakingWhitespace((String) params[0]),params);
    stmt = PreparedStatementLogger.newInstance(stmt,queryStack);
    return stmt;
   } else if ("createStatement".equals(method.getName())) {
    Statement stmt = (Statement) method.invoke(connection,params);
    stmt = StatementLogger.newInstance(stmt,queryStack);
    return stmt;
   } else {
    return method.invoke(connection,params);
   }
  } catch (Throwable t) {
   throw ExceptionUtil.unwrapThrowable(t);
  }
 }
public static PreparedStatement newInstance(PreparedStatement stmt,int queryStack) {
  InvocationHandler handler = new PreparedStatementLogger(stmt,queryStack);
  ClassLoader cl = PreparedStatement.class.getClassLoader();
  return (PreparedStatement) Proxy.newProxyInstance(cl,new Class[]{PreparedStatement.class,CallableStatement.class},handler);
 }

發現原來這個pareparedStatement物件還是個代理物件

繼續跟蹤程式碼

@Override
 public <E> List<E> query(Statement statement,ResultHandler resultHandler) throws SQLException {
  PreparedStatement ps = (PreparedStatement) statement;
  ps.execute();
  return resultSetHandler.<E> handleResultSets(ps);
 }

呼叫了之前生成的preparedStatement物件的execute函式,其實也就是代理物件的inovke函式

@Override
 public Object invoke(Object proxy,Object[] params) throws Throwable {
  try {
   if (Object.class.equals(method.getDeclaringClass())) {
    return method.invoke(this,params);
   }     
   if (EXECUTE_METHODS.contains(method.getName())) {
    if (isDebugEnabled()) {
     debug("Parameters: " + getParameterValueString(),true);
    }
    clearColumnInfo();
    if ("executeQuery".equals(method.getName())) {
     ResultSet rs = (ResultSet) method.invoke(statement,params);
     return rs == null ? null : ResultSetLogger.newInstance(rs,queryStack);
    } else {
     return method.invoke(statement,params);
    }
   } else if (SET_METHODS.contains(method.getName())) {
    if ("setNull".equals(method.getName())) {
     setColumn(params[0],null);
    } else {
     setColumn(params[0],params[1]);
    }
    return method.invoke(statement,params);
   } else if ("getResultSet".equals(method.getName())) {
    ResultSet rs = (ResultSet) method.invoke(statement,params);
    return rs == null ? null : ResultSetLogger.newInstance(rs,queryStack);
   } else if ("getUpdateCount".equals(method.getName())) {
    int updateCount = (Integer) method.invoke(statement,params);
    if (updateCount != -1) {
     debug("  Updates: " + updateCount,false);
    }
    return updateCount;
   } else {
    return method.invoke(statement,params);
   }
  } catch (Throwable t) {
   throw ExceptionUtil.unwrapThrowable(t);
  }
 }

此時sql語句執行完成,之後是獲取結果集

第五步 獲取結果集

@Override
 public List<Object> handleResultSets(Statement stmt) throws SQLException {
  ErrorContext.instance().activity("handling results").object(mappedStatement.getId());
 
  final List<Object> multipleResults = new ArrayList<Object>();
 
  int resultSetCount = 0;
  ResultSetWrapper rsw = getFirstResultSet(stmt);
 
  List<ResultMap> resultMaps = mappedStatement.getResultMaps();
  int resultMapCount = resultMaps.size();
  validateResultMapsCount(rsw,resultMapCount);
  while (rsw != null && resultMapCount > resultSetCount) {
   ResultMap resultMap = resultMaps.get(resultSetCount);
   handleResultSet(rsw,resultMap,multipleResults,null);
   rsw = getNextResultSet(stmt);
   cleanUpAfterHandlingResultSet();
   resultSetCount++;
  }
 
  String[] resultSets = mappedStatement.getResultSets();
  if (resultSets != null) {
   while (rsw != null && resultSetCount < resultSets.length) {
    ResultMapping parentMapping = nextResultMaps.get(resultSets[resultSetCount]);
    if (parentMapping != null) {
     String nestedResultMapId = parentMapping.getNestedResultMapId();
     ResultMap resultMap = configuration.getResultMap(nestedResultMapId);
     handleResultSet(rsw,parentMapping);
    }
    rsw = getNextResultSet(stmt);
    cleanUpAfterHandlingResultSet();
    resultSetCount++;
   }
  }
 
  return collapseSingleResultList(multipleResults);
 }

此處呼叫stmt.getResultSet(),然後他又進了代理物件的invoke函式

@Override
 public Object invoke(Object proxy,params);
   }
  } catch (Throwable t) {
   throw ExceptionUtil.unwrapThrowable(t);
  }
 }
 public static ResultSet newInstance(ResultSet rs,int queryStack) {
  InvocationHandler handler = new ResultSetLogger(rs,queryStack);
  ClassLoader cl = ResultSet.class.getClassLoader();
  return (ResultSet) Proxy.newProxyInstance(cl,new Class[]{ResultSet.class},handler);
 }

可以看到這還是一個代理模式,建立了一個用來獲取結果集的物件ResultSetLogger,看一下他的invoke函式

 @Override
 public Object invoke(Object proxy,params);
   }  
   Object o = method.invoke(rs,params);
   if ("next".equals(method.getName())) {
    if (((Boolean) o)) {
     rows++;
     if (isTraceEnabled()) {
      ResultSetMetaData rsmd = rs.getMetaData();
      final int columnCount = rsmd.getColumnCount();
      if (first) {
       first = false;
       printColumnHeaders(rsmd,columnCount);
      }
      printColumnValues(columnCount);
     }
    } else {
     debug("   Total: " + rows,false);
    }
   }
   clearColumnInfo();
   return o;
  } catch (Throwable t) {
   throw ExceptionUtil.unwrapThrowable(t);
  }
 }

從ResultSetLogger物件遍歷出結果集還要經過一步處理得到最終需要返回型別的結果集

第六步 封裝結果集

private void handleResultSet(ResultSetWrapper rsw,ResultMap resultMap,List<Object> multipleResults,ResultMapping parentMapping) throws SQLException {
  try {
   if (parentMapping != null) {
    handleRowValues(rsw,RowBounds.DEFAULT,parentMapping);
   } else {
    if (resultHandler == null) {
     DefaultResultHandler defaultResultHandler = new DefaultResultHandler(objectFactory);
     handleRowValues(rsw,defaultResultHandler,null);
     multipleResults.add(defaultResultHandler.getResultList());
    } else {
     handleRowValues(rsw,null);
    }
   }
  } finally {
   // issue #228 (close resultsets)
   closeResultSet(rsw.getResultSet());
  }
 }
public void handleRowValues(ResultSetWrapper rsw,ResultHandler<?> resultHandler,ResultMapping parentMapping) throws SQLException {
  if (resultMap.hasNestedResultMaps()) {
   ensureNoRowBounds();
   checkResultHandler();
   handleRowValuesForNestedResultMap(rsw,parentMapping);
  } else {
   handleRowValuesForSimpleResultMap(rsw,parentMapping);
  }
 }
 private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw,ResultMapping parentMapping)
   throws SQLException {
  DefaultResultContext<Object> resultContext = new DefaultResultContext<Object>();
  skipRows(rsw.getResultSet(),rowBounds);
  while (shouldProcessMoreRows(resultContext,rowBounds) && rsw.getResultSet().next()) {
   ResultMap discriminatedResultMap = resolveDiscriminatedResultMap(rsw.getResultSet(),null);
   Object rowValue = getRowValue(rsw,discriminatedResultMap);
   storeObject(resultHandler,resultContext,rowValue,parentMapping,rsw.getResultSet());
  }
 }
private Object getRowValue(ResultSetWrapper rsw,ResultMap resultMap) throws SQLException {
  final ResultLoaderMap lazyLoader = new ResultLoaderMap();
  Object resultObject = createResultObject(rsw,lazyLoader,null);
  if (resultObject != null && !hasTypeHandlerForResultObject(rsw,resultMap.getType())) {
   final MetaObject metaObject = configuration.newMetaObject(resultObject);
   boolean foundValues = !resultMap.getConstructorResultMappings().isEmpty();
   if (shouldApplyAutomaticMappings(resultMap,false)) {
    foundValues = applyAutomaticMappings(rsw,metaObject,null) || foundValues;
   }
   foundValues = applyPropertyMappings(rsw,null) || foundValues;
   foundValues = lazyLoader.size() > 0 || foundValues;
   resultObject = foundValues ? resultObject : null;
   return resultObject;
  }
  return resultObject;
 }

這一步最核心的程式碼就是上面這個函式,可以看到他是由configuration生成一個MetaObject,在通過applyAutomaticMappings函式,將資料庫結果中相應欄位的值填充到MetaObject中,程式碼如下

 private boolean applyAutomaticMappings(ResultSetWrapper rsw,MetaObject metaObject,String columnPrefix) throws SQLException {
  List<UnMappedColumnAutoMapping> autoMapping = createAutomaticMappings(rsw,columnPrefix);
  boolean foundValues = false;
  if (autoMapping.size() > 0) {
   for (UnMappedColumnAutoMapping mapping : autoMapping) {
    final Object value = mapping.typeHandler.getResult(rsw.getResultSet(),mapping.column);
    if (value != null) {
     foundValues = true;
    }
    if (value != null || (configuration.isCallSettersOnNulls() && !mapping.primitive)) {
     // gcode issue #377,call setter on nulls (value is not 'found')
     metaObject.setValue(mapping.property,value);
    }
   }
  }
  return foundValues;
 }

最後將得到的封裝好的物件放入一個list中返回結果,關閉資料庫連線,一次完整的sql語句執行流程至此完結 。

以上這篇mybatis3使用@Select等註解實現增刪改查操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。