mybatis3使用@Select等注解实现增删改查操作

1.需要的jar包

2.目录树

3.具体代码

一.需要的jar包

第一个:mybatis的jar包

第二个:mysql数据的驱动

二.目录树

三.具体代码

使用框架,配置文件先行!

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();
 }
}

效果:

已有数据插入到数据表

数据表的样式:

补充知识: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, configuration, 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(), null, 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, statementLog, 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, Method method, 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, statementLog, queryStack);
    return stmt;
   } else if ("prepareCall".equals(method.getName())) {
    if (isDebugEnabled()) {
     debug(" Preparing: " + removeBreakingWhitespace((String) params[0]), true);
    }
    PreparedStatement stmt = (PreparedStatement) method.invoke(connection, params);
    stmt = PreparedStatementLogger.newInstance(stmt, statementLog, queryStack);
    return stmt;
   } else if ("createStatement".equals(method.getName())) {
    Statement stmt = (Statement) method.invoke(connection, params);
    stmt = StatementLogger.newInstance(stmt, statementLog, queryStack);
    return stmt;
   } else {
    return method.invoke(connection, params);
   }
  } catch (Throwable t) {
   throw ExceptionUtil.unwrapThrowable(t);
  }
 }
public static PreparedStatement newInstance(PreparedStatement stmt, Log statementLog, int queryStack) {
  InvocationHandler handler = new PreparedStatementLogger(stmt, statementLog, 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, Method method, 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, statementLog, 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, statementLog, 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, resultMap, null, parentMapping);
    }
    rsw = getNextResultSet(stmt);
    cleanUpAfterHandlingResultSet();
    resultSetCount++;
   }
  }

  return collapseSingleResultList(multipleResults);
 }

此处调用stmt.getResultSet(),然后他又进了代理对象的invoke函数

@Override
 public Object invoke(Object proxy, Method method, 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, statementLog, 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, statementLog, 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);
  }
 }
 public static ResultSet newInstance(ResultSet rs, Log statementLog, int queryStack) {
  InvocationHandler handler = new ResultSetLogger(rs, statementLog, queryStack);
  ClassLoader cl = ResultSet.class.getClassLoader();
  return (ResultSet) Proxy.newProxyInstance(cl, new Class[]{ResultSet.class}, handler);
 }

可以看到这还是一个代理模式,创建了一个用来获取结果集的对象ResultSetLogger,看一下他的invoke函数

 @Override
 public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
  try {
   if (Object.class.equals(method.getDeclaringClass())) {
    return method.invoke(this, 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, resultMap, null, RowBounds.DEFAULT, parentMapping);
   } else {
    if (resultHandler == null) {
     DefaultResultHandler defaultResultHandler = new DefaultResultHandler(objectFactory);
     handleRowValues(rsw, resultMap, defaultResultHandler, rowBounds, null);
     multipleResults.add(defaultResultHandler.getResultList());
    } else {
     handleRowValues(rsw, resultMap, resultHandler, rowBounds, null);
    }
   }
  } finally {
   // issue #228 (close resultsets)
   closeResultSet(rsw.getResultSet());
  }
 }
public void handleRowValues(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler<?> resultHandler, RowBounds rowBounds, ResultMapping parentMapping) throws SQLException {
  if (resultMap.hasNestedResultMaps()) {
   ensureNoRowBounds();
   checkResultHandler();
   handleRowValuesForNestedResultMap(rsw, resultMap, resultHandler, rowBounds, parentMapping);
  } else {
   handleRowValuesForSimpleResultMap(rsw, resultMap, resultHandler, rowBounds, parentMapping);
  }
 }
 private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler<?> resultHandler, RowBounds rowBounds, 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(), resultMap, 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, resultMap, 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, resultMap, metaObject, null) || foundValues;
   }
   foundValues = applyPropertyMappings(rsw, resultMap, metaObject, lazyLoader, null) || foundValues;
   foundValues = lazyLoader.size() > 0 || foundValues;
   resultObject = foundValues ? resultObject : null;
   return resultObject;
  }
  return resultObject;
 }

这一步最核心的代码就是上面这个函数,可以看到他是由configuration生成一个MetaObject,在通过applyAutomaticMappings函数,将数据库结果中相应字段的值填充到MetaObject中,代码如下

 private boolean applyAutomaticMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, String columnPrefix) throws SQLException {
  List<UnMappedColumnAutoMapping> autoMapping = createAutomaticMappings(rsw, resultMap, metaObject, 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等注解实现增删改查操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • java jdbc连接mysql数据库实现增删改查操作

    jdbc相信大家都不陌生,只要是个搞java的,最初接触j2ee的时候都是要学习这么个东西的,谁叫程序得和数据库打交道呢!而jdbc就是和数据库打交道非常基础的一个知识,也是比较接近底层的,在实际的工作中大家用得更多的其实还是比较成熟的框架,例如Hibernate.Mybatis. 但是作为这些成熟框架的底层的jdbc却也是我们应该去掌握的,只有了解了jdbc的增删改查,这样在以后如果有兴趣去研究Hibernate或者Mybatis的源代码的时候才能更好的去理解这些成熟的框架是如何去实现增删改查

  • 解决mybatis plus 一对多分页查询问题

    最近用mybatis plus做项目,单表的增删改查都正常,做到 1对多表的分页时,用resultMap返回的时候发现返回的记录和总数对不上 返回的记录是 一 表的,二返回的总数是 多 表 查了一下,这个或者是PLUS的bug 大概的解决办法如下图:用collection,传参用column,我这里用了一个小技巧, 把外面传入的参数,作为主表的column传入到从表. 这里没找到其他方法,有其他方法可以评论告诉我 补充知识:解决Mybatis-plus利用collection查询一对多分页数据的

  • mybatis通过if语句实现增删改查操作

    有时候为了简化我们的代码. 1 举个例子 Student类: @Data public class Student { private Integer id; private Integer age; private Integer sno; } 有时候我们想通过age这个属性获取Student对象 有时候我们也想通过sno这个属性获取Student对象 难道我们在DAO层写两个接口? 比如这样子? Student getStudentByAge(Int age); Student getStu

  • mybatis3使用@Select等注解实现增删改查操作

    1.需要的jar包 2.目录树 3.具体代码 一.需要的jar包 第一个:mybatis的jar包 第二个:mysql数据的驱动 二.目录树 三.具体代码 使用框架,配置文件先行! conf.xml:(配置 登录数据库,映射文件) <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN

  • hibernate通过session实现增删改查操作实例解析

    今天学习了一下如何通过hibernate来实现对数据库的增删改查,下面来简单介绍一下: 首先创建个Student类: package com.hibernate.model; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.T

  • MyBatis存储过程、MyBatis分页、MyBatis一对多增删改查操作

    一.用到的实体类如下: Student.java package com.company.entity; import java.io.Serializable; import java.util.Date; public class Student implements Serializable{ private static final long serialVersionUID = 1L; private int id; private String name; private Date

  • Java语言实现对MySql数据库中数据的增删改查操作的代码

    简单说操作的步骤: 1.连接数据库 2.将SQL语句发送到数据库 3.执行SQL语句 这里举个例子: 在一个数据库中有个students表,表中有学号(Id),姓名(Name),性别(Sex),地址(Address),电话(Phone),专业(Dept). 这里把这个表写成一个学生信息类(Info_student) (请先确保看了例子说明,不然代码有的地方可能看不明白) 要实现操纵我们首先得连接数据库,因为每个操作都要进行连接操作,所以我们直接把连接的操作封装在一个类中,需要连接的时候直接调用可

  • python django 增删改查操作 数据库Mysql

    下面介绍一下django增删改查操作: 1.view.py # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.http import HttpResponse from polls.models import Test from django.shortcuts import render # Create your views here. # 解决乱码 import sys reload(sy

  • Python Sql数据库增删改查操作简单封装

    本文实例为大家分享了如何利用Python对数据库的增删改查进行简单的封装,供大家参考,具体内容如下 1.insert import mysql.connector import os import codecs #设置数据库用户名和密码 user='root';#用户名 pwd='root';#密码 host='localhost';#ip地址 db='mysql';#所要操作数据库名字 charset='UTF-8' cnx = mysql.connector.connect(user=use

  • php数据访问之增删改查操作

    增删改查操作小练习,大家练练手吧 一.查看新闻页面-----主页面 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>查看新闻</title> </head> <body> <h1>查看新闻</h1> <table widt

  • CodeIgniter框架基本增删改查操作示例

    本文实例讲述了CodeIgniter框架基本增删改查操作.分享给大家供大家参考,具体如下: 对于codeigniter的增删改,在此我用自己的一个例子来说明一下: 创建数据库: CREATE TABLE IF NOT EXISTS `users` ( `id` int(10) NOT NULL AUTO_INCREMENT, `username` varchar(50) NOT NULL, `password` varchar(50) NOT NULL, `email` varchar(100)

  • PHP连接数据库实现注册页面的增删改查操作

    本文实例为大家分享了PHP连接数据库实现注册页面的增删改查操作的方法,供大家参考,具体内容如下 1.连接数据库 <?php //本地测试 $host = '127.0.0.1'; $port = 3306; $user = "root"; $pwd = ""; $link = @mysql_connect("{$host}:{$port}",$user,$pwd,true); if(!$link) { die("Connect S

  • Hibernate中Session增删改查操作代码详解

    把三状态转换图放在这,方便分析方法的作用: 1.Session的save()方法 Session是Hibernate所有接口中最重要的接口,提供了对数据保存,更新,查询和删除的方法. Session的save()方法可以使临时态或游离态转换为持久态.例如,保存一个Customer对象: SessionFactory sessionFactory; Configuration configuration = new Configuration().configure(); sessionFacto

随机推荐