• Mapper 编写有哪几种方式?


     

    第一种接口实现类继承 SqlSessionDaoSupport:使用此种方法需要编写 

    mapper 接口,mapper 接口实现类、mapper.xml 文件

    1、 sqlMapConfig.xml 中配置 mapper.xml 的位置 

    <mappers>

    <mapper resource="mapper.xml 文件的地址" />

    <mapper resource="mapper.xml 文件的地址" />

    </mappers>

    1、定义 mapper 接口 

    3、实现类集成 SqlSessionDaoSupport

    mapper 方法中可以 this.getSqlSession()进行数据增删改查

    4、spring 配置 

    <bean id=" " class="mapper 接口的实现">

    <property name="sqlSessionFactory"

    ref="sqlSessionFactory"></property>

    </bean>

    第二种使用 org.mybatis.spring.mapper.MapperFactoryBean

    1、 sqlMapConfig.xml 中配置 mapper.xml 的位置如果 mapper.xml  

    mappre 接口的名称相同且在同一个目录这里可以不用配置 

    <mappers>

    <mapper resource="mapper.xml 文件的地址" />

    <mapper resource="mapper.xml 文件的地址" />

    </mappers>

    2、定义 mapper 接口

     

    1、mapper.xml 中的 namespace  mapper 接口的地址 

    2、mapper 接口中的方法名和 mapper.xml 中的定义的 statement  id 保持一 

    3、Spring 中定义 

    <bean id="" class="org.mybatis.spring.mapper.MapperFactoryBean">

    <property name="mapperInterface"

    value="mapper 接口地址" />

    <property name="sqlSessionFactory" ref="sqlSessionFactory" />

    </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 class="org.mybatis.spring.mapper.MapperScannerConfigurer">

    <property name="basePackage" value="mapper 接口包地址 

    "></property>

    <property name="sqlSessionFactoryBeanName"

    value="sqlSessionFactory"/>

    </bean>4、使用扫描器后从 spring 容器中获取 mapper 的实现对象

    Java Program!
  • 相关阅读:
    安卓:线性布局
    安卓文件的资源访问
    关于安卓的一些初步认识
    haayaGuardV0---3---手机与pc基于p2p通信
    【无中生有】---13---系统发布
    【无中生有】---12---运维与监控
    【无中生有】---11---服务器集群部署
    【无中生有】---7---分布式消息队列
    使用消息队列的 10 个理由
    【无中生有】---10---API接口日志存储设计与实现设计
  • 原文地址:https://www.cnblogs.com/programb/p/12996211.html
Copyright © 2020-2023  润新知