MyBatis學習(八)

本教程對應視頻課程地址: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、全局配置文件設置

<!-- 開啓延遲加載 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 按需要加載 -->
<setting name="aggressiveLazyLoading" value="false"/>

1.3、定義接口方法

public List<Order> queryLazy(@Param("orderNum")String orderNum);

1.4、mapper.xml文件配置

<resultMap type="Order" id="lazyOrderMap" autoMapping="true">
    <id column="oid" property="oid"/>
    <association property="user" javaType="User" column="user_id" select="selectById"/>
  </resultMap>

  <select id = "selectById" resultType="User">
    select * from tb_user where userid=#{userid}
  </select>
  <select id = "queryLazy" resultMap="lazyOrderMap">
        select * from tb_order where order_number=#{orderNum}
  </select>

1.5、測試方法

@Test
    public void testLazy()throws Exception{
        List<Order> 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文件

 <select id = "callPro" statementType="CALLABLE">
    {call mypro}
  </select>

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文件的配置

<!-- 調用有參數的 
    useCache="false"
    定義入參和出參
    jdbcType:大家參考org.apache.ibatis.type.JdbcType
  -->
  <select id = "callPro2" useCache="false" statementType="CALLABLE">

    {call pro_getusernamebyid(
        #{userid,mode=IN,jdbcType=INTEGER,javaType=int},
        #{userName,mode=OUT,jdbcType=VARCHAR,javaType=string}
    )}
  </select>

測試方法

@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.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章