Mapper中的方法和DAO接口方法是怎么绑定到一起的

本文介绍MyBatis如何通过JDK动态代理机制实现DAO层接口与Mapper映射文件的绑定。具体而言,MyBatis在启动时根据配置文件生成DAO接口的实现类,并在调用session.getMapper()时创建代理对象。该代理对象能够拦截DAO接口方法调用并执行相应的SQL语句。

 

Mybatis中DAO层接口没有写实现类,Mapper中的方法和DAO接口方法是怎么绑定到一起的,其内部是怎么实现的

 

原理

 

再根据网上的一些知识点,讲一下原理:

mybatis通过JDK的动态代理方式,在启动加载配置文件时,根据配置mapper的xml去生成Dao的实现。

session.getMapper()使用了代理,当调用一次此方法,都会产生一个代理class的instance,看看这个代理class的实现.

public class MapperProxy implements InvocationHandler { 
... 
public static <T> T newMapperProxy(Class<T> mapperInterface, SqlSession sqlSession) { 
    ClassLoader classLoader = mapperInterface.getClassLoader(); 
    Class<?>[] interfaces = new Class[]{mapperInterface}; 
    MapperProxy proxy = new MapperProxy(sqlSession); 
    return (T) Proxy.newProxyInstance(classLoader, interfaces, proxy); 
  } 

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 
    if (!OBJECT_METHODS.contains(method.getName())) { 
      final Class<?> declaringInterface = findDeclaringInterface(proxy, method); 
      final MapperMethod mapperMethod = new MapperMethod(declaringInterface, method, sqlSession); 
      final Object result = mapperMethod.execute(args); 
      if (result == null && method.getReturnType().isPrimitive()) { 
        throw new BindingException("Mapper method '" + method.getName() + "' (" + method.getDeclaringClass() + ") attempted to return null from a method with a primitive return type (" +    method.getReturnType() + ")."); 
      } 
      return result; 
    } 
    return null; 
  } 

这里是用到了JDK的代理Proxy。 newMapperProxy()可以取得实现interfaces 的class的代理类的实例。

当执行interfaces中的方法的时候,会自动执行invoke()方法,其中public Object invoke(Object proxy, Method method, Object[] args)中 method参数就代表你要执行的方法.

MapperMethod类会使用method方法的methodName 和declaringInterface去取 sqlMapxml 取得对应的sql,也就是拿declaringInterface的类全名加上 sql-id..

总结: 
这个就是利用JDK的代理类实现的。

在MyBatis(一种常见的Java ORM框架)中,Mapper接口用于描述DAO(Data Access Object)层的方法,而Mapper XML文件或者注解则负责将这些接口方法与实际的数据库操作映射起来。 1. **创建Mapper接口**:首先,在Mapper接口中定义你需要执行的SQL操作,比如查询、插入、更新或删除数据。每个方法对应数据库中的一个操作,通常以`select*`, `insert`, `update`, 或 `delete` 开头。 ```java public interface UserMapper { List<User> selectUsers(); int insertUser(User user); } ``` 2. **编写Mapper XML文件**:在这个文件中,通过`<select>`、`<insert>`、`<update>` `<delete>` 标签,分别对应接口中的方法,并配置SQL语句参数绑定: ```xml <mapper namespace="com.example.mapper.UserMapper"> <select id="selectUsers" resultType="User"> SELECT * FROM users </select> <insert id="insertUser" parameterType="User"> INSERT INTO users (name, password) VALUES (?, ?) </insert> </mapper> ``` 3. **使用注解映射**:MyBatis还支持注解映射,比如`@Select`、`@Insert` 等。在这种模式下,可以在方法上直接使用注解,然后在Mapper接口中不再需要XML声明: ```java @Mapper public interface UserMapper { @Select("SELECT * FROM users") List<User> selectUsers(); @Insert("INSERT INTO users(name, password) VALUES(#{name}, #{password})") int insertUser(User user); } ``` 当你在Service或Controller层调用这些Mapper接口方法时,MyBatis会根据对应的映射执行相应的SQL并返回结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值