1 Mapper映射器是什么

  是符合映射文件要求的接口

    接口要求

      a. 方法名要与sql的id一致。
      b. 方法的参数类型要与parameterType一致。
      c. 方法的返回类型要与resultType一致。

    映射文件要求

      namespace必须等于接口名(包含包名)

   增加笔记(2017年5月19日09:19:46)

    Mapper映射器的作用:如果我们不使用Mapper映射器,那么我们就必须调用SqlSession的相应方法区执行相应的增、删、改、查操作;如果我们使用了Mapper映射器,那么我们执行增、删、改、查这些操作时使用的就是Mapper映射器中的相应方法;但是要想使用Mapper映射器中的方法必须要有一个Mapper映射器对象,这个对象可以通过SqlSession的getMapper方法得到,例如:AdminDao ad = sqlSession.getMapper(AdminDao.class);当然也有得到Mapper对象的简便方法,那就是使用spring去集成mybatis,通过在spring配置文件中配置MapperScannerConfigurer就可以帮相应的Mapper映射器配置一个bean,以后我们就可以通过spring容器来使用这个bean啦

2 如何使用Mapper映射器

  2.1 导包

    MyBatis02 MyBatis基础知识之Mapper映射器

  2.2 添加mybatis配置文件

 1 <?xml version="1.0" encoding="UTF-8" ?>  
 2 <!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" 
 3     "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
 4 <configuration>
 5     <!-- 和spring整合后,environment配置将被废除 -->
 6     <environments default="environment">
 7         <environment id="environment">
 8             <!-- 使用jdbc事务管理,事务控制由mybatis实现 -->
 9             <transactionManager type="JDBC" />
10             <!-- 数据库连接池,由mybatis进行管理 -->
11             <dataSource type="POOLED">
12                 <property name="driver" value="com.mysql.jdbc.Driver" />
13                 <property name="url"
14                     value="jdbc:mysql://127.0.0.1:3306/xiangxu" />
15                 <property name="username" value="root" />
16                 <property name="password" value="182838" />
17             </dataSource>
18         </environment>
19     </environments>
20     
21     <!--  指定映射文件的位置(即:加载映射文件)  -->        
22     <mappers>
23         <mapper resource="cn/xiangxu/telecom/login/entity/AdminMapper.xml" /> <!-- 注意:包名的 . 全部换成 / -->
24     </mappers>
25     
26 </configuration> 
配置好的SqlMapConfig.xml

相关文章:

  • 2022-12-23
  • 2021-07-11
  • 2021-07-11
  • 2021-09-13
  • 2022-12-23
  • 2022-12-23
  • 2021-10-14
  • 2021-12-20
猜你喜欢
  • 2021-05-17
  • 2022-12-23
  • 2022-12-23
  • 2021-04-24
  • 2022-12-23
  • 2021-09-06
  • 2022-12-23
相关资源
相似解决方案