MyBatis mappers元素标签及其属性、配置

mappers:映射器,以最佳的方式是告诉 MyBatis 到哪里去找映射文件。可以使用相对于类路径的资源引用, 或完全限定资源定位符(包括 file:/// 的 URL),或类名和包名等。

例如:

<!-- 使用相对于类路径的资源引用 -->
<mappers>
  <mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
  <mapper resource="org/mybatis/builder/BlogMapper.xml"/>
  <mapper resource="org/mybatis/builder/PostMapper.xml"/>
</mappers>
<!-- 使用完全限定资源定位符(URL) -->
<mappers>
  <mapper url="file:///var/mappers/AuthorMapper.xml"/>
  <mapper url="file:///var/mappers/BlogMapper.xml"/>
  <mapper url="file:///var/mappers/PostMapper.xml"/>
</mappers>

 

<!-- 使用映射器接口实现类的完全限定类名 -->
<mappers>
  <mapper class="org.mybatis.builder.AuthorMapper"/>
  <mapper class="org.mybatis.builder.BlogMapper"/>
  <mapper class="org.mybatis.builder.PostMapper"/>
</mappers>
<!-- 将包内的映射器接口实现全部注册为映射器 -->
<mappers>
  <package name="org.mybatis.builder"/>
</mappers>

这些配置会告诉了 MyBatis 去哪里找映射文件。

注意:

1、当采用class、package方式时,映射文件(Mapper.xml)和接口必须命名相同,并且放在与接口同一目录下。(尽量不要采用这种方法)

2、当采用class方式时,没有SQL映射文件,所有的SQL都是利用注解写在接口上,这样就可以避免注意1的事情发生,提高维护性,不是很重要的SQL语句可以采用注解的方式,这样可以提高开发速度,重要和复杂的接口、SQL建议还是采用SQL映射文件的方式。(尽量采用这种方法)

例如:

<mappers>
        <mapper resource="EmployeeMapper.xml"/>
        <mapper class="com.mybatis.dao"></mapper>
    </mappers>
package com.mybatis.dao;

import com.mybatis.employee.Employee;
import org.apache.ibatis.annotations.Select;

public interface mapperTestInter {

    @Select("select * from table_employee where id = #{id}")
    public Employee getEmpByid(Integer id);
}
package com.mybatis.employee.test;

import java.io.IOException;
import java.io.InputStream;

import com.mybatis.dao.EMPInter;
import com.mybatis.dao.EmployeeMapper;
import com.mybatis.employee.EMP;
import com.mybatis.employee.Employee;
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.junit.Test;

import java.io.InputStream;

public class MyBatisTest {

    public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
         return new SqlSessionFactoryBuilder().build(inputStream);
    }

    /*
    * 根据配置文件(全局配置文件)创建一个SqlSessionFactory对象
    *并且获取SqlSession实例,直接执行映射的SQL语句
    * */
    @Test
    public void test() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        /*
        * 获取SqlSession实例,能直接执行映射的SQL语句
        * */
        SqlSession session = sqlSessionFactory.openSession();

        try {
            Employee employee = session.selectOne("com.mybatis.employee.selectEmp",1);
            System.out.println(employee);
        } finally {
            session.close();
        }
    }

    @Test
    public void testInter() throws IOException {
        //获取SqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();

        //获取SQLSession对象
        SqlSession session = sqlSessionFactory.openSession();

        try {
            //获取接口的实现类对象
            //会为接口创建一个代理对象,代理对象去执行增删改查方法
            mapperTestInter  employeeMapper = session.getMapper(mapperTestInter.class);
            Employee employee = employeeMapper.getEmpById(1);

            System.out.println(employeeMapper.getClass());

            System.out.println(employee);
        } finally {
            session.close();
        }
    }

}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值