烟台做网站优化,池州市网站建设,百度seo排名帝搜软件,免费自助设计网站MyBatis 面试题 26、Mapper 编写有哪几种方式#xff1f;
第一种#xff1a;接口实现类继承 SqlSessionDaoSupport#xff1a;使用此种方法需要编写mapper 接口#xff0c;mapper 接口实现类、mapper.xml 文件。
1、在 sqlMapConfig.xml 中配置 mapper.xml 的位置
m…MyBatis 面试题 26、Mapper 编写有哪几种方式
第一种接口实现类继承 SqlSessionDaoSupport使用此种方法需要编写mapper 接口mapper 接口实现类、mapper.xml 文件。
1、在 sqlMapConfig.xml 中配置 mapper.xml 的位置
mappersmapper resourcemapper.xml 文件的地址 /mapper resourcemapper.xml 文件的地址 //mappers2、定义 mapper 接口
3、实现类集成 SqlSessionDaoSupportmapper 方法中可以 this.getSqlSession()进行数据增删改查。
4、spring 配置
bean id classmapper 接口的实现property namesqlSessionFactory refsqlSessionFactory/property/bean第二种使用 org.mybatis.spring.mapper.MapperFactoryBean
1、在 sqlMapConfig.xml 中配置 mapper.xml 的位置如果 mapper.xml 和mappre 接口的名称相同且在同一个目录这里可以不用配置
mappersmapper resourcemapper.xml 文件的地址 /mapper resourcemapper.xml 文件的地址 //mappers2、定义 mapper 接口
1、mapper.xml 中的 namespace 为 mapper 接口的地址
2、mapper 接口中的方法名和 mapper.xml 中的定义的 statement 的 id 保持一致
3、Spring 中定义
bean id classorg.mybatis.spring.mapper.MapperFactoryBeanproperty namemapperInterface valuemapper 接口地址 /property namesqlSessionFactory refsqlSessionFactory //bean第三种使用 mapper 扫描器
1、mapper.xml 文件编写mapper.xml 中的 namespace 为 mapper 接口的地址
mapper 接口中的方法名和 mapper.xml 中的定义的 statement 的 id 保持一致
如果将 mapper.xml 和 mapper 接口的名称保持一致则不用在 sqlMapConfig.xml中进行配置。
2、定义 mapper 接口
注意 mapper.xml 的文件名和 mapper 的接口名称保持一致且放在同一个目录
3、配置 mapper 扫描器
bean classorg.mybatis.spring.mapper.MapperScannerConfigurerproperty namebasePackage valuemapper 接口包地址/propertyproperty namesqlSessionFactoryBeanNamevaluesqlSessionFactory//bean
4、使用扫描器后从 spring 容器中获取 mapper 的实现对象。
27、简述 Mybatis 的插件运行原理以及如何编写一个插件。
答Mybatis 仅可以编写针对 ParameterHandler、ResultSetHandler、StatementHandler、Executor 这 4 种接口的插件Mybatis 使用 JDK 的动态代理为需要拦截的接口生成代理对象以实现接口方法拦截功能每当执行这 4 种接口对象的方法时就会进入拦截方法具体就是 InvocationHandler 的 invoke()方法当然只会拦截那些你指定需要拦截的方法。
编写插件实现 Mybatis 的 Interceptor 接口并复写 intercept()方法然后在给插件编写注解指定要拦截哪一个接口的哪些方法即可记住别忘了在配置文件中配置你编写的插件。