首页 > 编程语言 > 详细

java的Mybatis动态代理方式(二)

时间:2020-04-26 15:53:55      阅读:63      评论:0      收藏:0      [点我收藏+]

上期介绍了Mybatis基础的使用方式,这次介绍下Mybatis动态代理方式(接口开发)也是官方推荐的方式

mapper动态代理方式的 总体思想是: 约定由于配置

--------------------

1.硬编码方式:

  abc.java 

    Configuration conf = new Configuration();

    conf.setName("myProject");

2.配置方式:

  abc.xml

    <name>myProject</name>

3.约定 : 默认值就是 myProject

 

--------------------------------------

那mapper动态代理 是怎么约定的呢?

具体实现步骤:

1.导包(mybatis.jar , jdbc.jar , 和建立 配置文件 config.xml 及 xxMapper.xml)

2.(不同之处) 约定目标,省略掉statement(简写 stm,其实就是一个字符串,映射文件xxMapper.xml中的id值 ). 最终可以直接定位到sql.

  1.建立接口 , com.cc8w.mapper.TeacherMapper.java

    1)第一个约定:接口的方法名和xxMapper.xml文件中的标签id相同

    2)第二个约定:接口的方法参数类型 和 xxMapper.xml文件中标签parameterType 类型一致

    3)第三个约定:接口的方法返回类型 和 xxMapper.xml文件中标签 resultType 类型一致  

  2. 之后xxMapper.xml文件中的namespace就写,相应接口的全类名.

习惯: 一般接口 com.cc8w.mapper.TeacherMapper.java 和 TeacherMapper.xml在一个文件

匹配过程: 

  1.根据 接口名 找到 xxMapper.xml文件 (namespace=接口全类名)

  2.根据 接口方法 找到xxMapper.xml文件中的sql标签 (方法名=sql标签id值)

其实最终的目的是:通过接口方法找到sql语句

 

例子 : (实体类 数据库 等配置就忽略了,主要记录 接口建立和对应的xxMapper.xml书写) 

1.新建接口 TeacherMapper.java

package com.cc8w.mapper;

import com.cc8w.entity.Teacher;

public interface TeacherMapper {
    
    Teacher queryOneTeacher(int id);//查询一个数据

}

2.建立与之对应的TeacherMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cc8w.mapper.TeacherMapper">
    <select id="queryOneTeacher" resultType="com.cc8w.entity.Teacher">
    select * from teacher where id = #{id}
    </select>
</mapper>

 

上面最重要的两步好了, 然后就是 在配置文件conf.xml中 引入新建的这个 xxMapper.xml文件

    <!-- 映射器:指定映射文件或者映射类 -->
    <mappers>
        <mapper resource="mybatis/TestMapper.xml"/>
        <mapper resource="com/cc8w/mapper/TeacherMapper.xml"/>
    </mappers>

 

开始写测试类:

package com.cc8w.test;

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

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 com.cc8w.entity.Teacher;
import com.cc8w.mapper.TeacherMapper;

public class Test {
    
    //基础的mybatis查询
    public static void demo01() throws IOException{
        //1.加载配置文件
        String resource = "mybatis/config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =
        new SqlSessionFactoryBuilder().build(inputStream);
        
        //2.获取SqlSession-->类似于数据库连接 Connection
        SqlSession session = sqlSessionFactory.openSession();
        
        //3.执行XXMapper.xml中映射的sql语句
        String stm = "com.cc8w.entity.Teacher.queryOneTeacher";
        Teacher t = session.selectOne(stm, 2);
        System.out.println(t);        
    }
    
    //动态代理的mybatis查询
    public static void demo02() throws IOException{
        //1.加载配置文件
        String resource = "mybatis/config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =
        new SqlSessionFactoryBuilder().build(inputStream);
        
        //2.获取SqlSession-->类似于数据库连接 Connection
        SqlSession session = sqlSessionFactory.openSession();
        
        //3.通过接口映射到XXMapper.xml中映射的sql语句
        TeacherMapper tm = session.getMapper(TeacherMapper.class);
        Teacher t = tm.queryOneTeacher(1);
        System.out.println(t);        
    }    
    
    public static void main(String[] args) throws IOException {
        //demo01();//基础的mybatis使用
        demo02();//使用动态代理方式
        
        
    }

}

 

 

结果:

 

技术分享图片

 

java的Mybatis动态代理方式(二)

原文:https://www.cnblogs.com/fps2tao/p/12779380.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!