本教程對應視頻課程地址:http://edu.51cto.com/sd/3ec2cjava
延遲加載的意義在於,雖然是關聯查詢,可是不是及時將關聯的數據查詢出來,而是在須要的時候進行查詢sql
select * from tb_order where order_number='20180810001' select * from tb_user where userid=2
<!-- 開啓延遲加載 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 按須要加載 --> <setting name="aggressiveLazyLoading" value="false"/>
public List<Order> queryLazy(@Param("orderNum")String orderNum);
<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>
@Test public void testLazy()throws Exception{ List<Order> list = orderMapper.queryLazy("20180810001"); for (Order order : list) { System.out.println(order.getOid()); } }
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.
修改代碼,調用懶屬性操做,再次觀察日誌apache
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.
一、先來定義一個無參數的存儲過程mybatis
create or replace procedure mypro is begin insert into dept(deptno,dname,loc) values(60,'銷售部','北京'); commit; end mypro;
二、定義接口方法oracle
public void callPro();
三、定義mapper文件app
<select id = "callPro" statementType="CALLABLE"> {call mypro} </select>
四、編寫測試類dom
@Test public void callpro1()throws Exception{ orderMapper.callPro(); }
定義一個有參數的存儲過程ide
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.