本教程对应视频课程地址:http://edu.51cto.com/sd/3ec2c

1、延迟加载

延迟加载的意义在于,虽然是关联查询,但是不是及时将关联的数据查询出来,而是在需要的时候进行查询

1.1、延迟加载的sql分析

select * from tb_order where order_number='20180810001'
select * from tb_user where userid=2

1.2、全局配置文件设置




1.3、定义接口方法

public List queryLazy(@Param("orderNum")String orderNum);

1.4、mapper.xml文件配置


    
    
  

  
  

1.5、测试方法

@Test
    public void testLazy()throws Exception{
        List list = orderMapper.queryLazy("20180810001");
        for (Order order : list) {
            System.out.println(order.getOid());

        }
    }

1.6、观察日志

DEBUG - Opening JDBC Connection
DEBUG - Created connection 80004644.
DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@4c4c624]
DEBUG - ==>  Preparing: select * from tb_order where order_number=? 
DEBUG - ==> Parameters: 20180810001(String)
DEBUG - <==      Total: 1
1
DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@4c4c624]
DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@4c4c624]
DEBUG - Returned connection 80004644 to pool.

修改代码,调用懒属性操作,再次观察日志

DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@12ac67ee]
DEBUG - ==>  Preparing: select * from tb_order where order_number=? 
DEBUG - ==> Parameters: 20180810001(String)
DEBUG - <==      Total: 1
1
DEBUG - Cache Hit Ratio [cn.org.kingdom.mapper.OrderMapper]: 0.0
DEBUG - ==>  Preparing: select * from tb_user where userid=? 
DEBUG - ==> Parameters: 2(BigDecimal)
DEBUG - <==      Total: 1
阿柯
DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@12ac67ee]
DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@12ac67ee]
DEBUG - Returned connection 313288686 to pool.

2、mybatis调用存储过程

2.1、mybatis调用无参的存储过程

1、先来定义一个无参数的存储过程

create or replace procedure mypro is
begin
  insert into dept(deptno,dname,loc) values(60,'销售部','北京');
  commit;
end mypro;

2、定义接口方法

public void callPro();

3、定义mapper文件

 

4、编写测试类

@Test
    public void callpro1()throws Exception{
        orderMapper.callPro();
    }

2.2、调用有参数的存储过程

定义一个有参数的存储过程

create or replace procedure pro_getUserNameById(v_userid in tb_user.userid%type,
v_username out tb_user.user_name%type) is
begin
           select user_name into v_username from tb_user where userid=v_userid;
end pro_getUserNameById;

接口方法定义

public void callPro2(User vo);

mapper.xml文件的配置


  

测试方法

@Test
    public void callpro2()throws Exception{
        User user = new User();
        user.setUserid(2);
        orderMapper.callPro2(user);
        System.out.println(user.getUserName());
    }

日志信息

DEBUG - Opening JDBC Connection
DEBUG - Created connection 1723550754.
DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@66bb4c22]
DEBUG - ==>  Preparing: {call pro_getusernamebyid( ?, ? )} 
DEBUG - ==> Parameters: 2(Integer)
阿柯
DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@66bb4c22]
DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@66bb4c22]
DEBUG - Returned connection 1723550754 to pool.