MyBatis and Spring Why write only when combined Dao layer does not write its implementation class

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/zhangyong01245/article/details/91376962

Preface:

The need to advance understanding of knowledge:

  1. JDK dynamic proxy: https://mp.csdn.net/mdeditor/90598309#
  2. Mybatis simple Demo Learning: https://www.mybatis.org/mybatis-3/zh/getting-started.html
  3. Sprng and Mybatis Demo build simple: http://www.mybatis.org/spring/zh/getting-started.html

Code is structured:

  1. Dao, write omitted Mapper, spring-mybatis.xml file:
  2. Create a test class MybatisSpringTest:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:/spring-mybatis.xml"})
@Slf4j
public class MybatisSpringTest {

    @Autowired
    private ClassInfoDao classInfoDao;

    @Test
    public void testSrpingMybatis() throws IOException {
        ClassInfo classInfo = classInfoDao.selectClassByClassId(1);
        System.err.println("classInfo : " + JSONObject.toJSONString(classInfo));
        
        // -------- 将代理类反编译到文件中  ($Proxy17 需要根据Debug中显示定义,可能会不同)----------------
        byte[] classFile = ProxyGenerator.generateProxyClass("$Proxy17", new Class[]{ClassInfoDao.class});
        String path = "F:/mycode/Git/spring-mvc/learn/mybatis/mybatis-learn/src/test/java/com/lot/Proxy17.class";
        try {
            FileOutputStream fos = new FileOutputStream(path);
            fos.write(classFile);
            fos.flush();
            System.out.println("代理类写入成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Code flow analysis:

  1. When we run Debug can classInfoDao defined, as shown below:
    Here Insert Picture Description
    When we call ClassInfoDao actually $ Proxy17 (JDK dynamic proxy object generated), wherein h is the proxy class MapperProxy, the $ Proxy17 generated by ProxyGenerator.generateProxyClass class file, class reads as follows:
public final class $Proxy17 extends Proxy implements ClassInfoDao {
    private static Method m1;
    private static Method m2;
    private static Method m3;
    private static Method m4;
    private static Method m0;

    public $Proxy17(InvocationHandler var1) throws  {
        super(var1);
    }

    static {
        try {
        m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
        m2 = Class.forName("java.lang.Object").getMethod("toString");
        m3 = Class.forName("com.lot.dao.ClassInfoDao").getMethod("selectClassByClassId", Class.forName("java.lang.Integer"));
        m0 = Class.forName("java.lang.Object").getMethod("hashCode");
        } catch (NoSuchMethodException var2) {
        throw new NoSuchMethodError(var2.getMessage());
        } catch (ClassNotFoundException var3) {
        throw new NoClassDefFoundError(var3.getMessage());
        }
        }

    public final ClassInfo selectClassByClassId(Integer var1) throws  {
        try {
            return (ClassInfo)super.h.invoke(this, m3, new Object[]{var1});
        } catch (RuntimeException | Error var3) {
            throw var3;
        } catch (Throwable var4) {
            throw new UndeclaredThrowableException(var4);
        }
    }
    // 省略  hashCode  toString  equals方法
    
}
  1. Oh roar, it is clear that when we code execution classInfoDao.selectClassByClassId actual call is JDK dynamic proxy objects generated $ Proxy17. SelectClassByClassId method, and super.h.invoke () is executed org.apache.ibatis.binding.MapperProxy # invoke method, this step has toggled by our own code to Spring in the.
  2. Finally, when we will find a step by step Debug final call is Mybatis of sqlSession.selectOne (), the final implementation of sql. Since then, Spring work has been completed, the back is necessary to Mybatis to deal with.

to sum up:

  1. Mybatis Dao Why do not realize?
    Jane: When we started the project, Sql as well as classes and methods will Mybatis our Mapper file (or notes) configuration consisting of only Key saved, and by the way Spring and MapperProxy JDK dynamic proxies to help us generate proxy classes, when we call for us. We greatly simplify the development process.

Guess you like

Origin blog.csdn.net/zhangyong01245/article/details/91376962