This tutorial corresponds to the video course address: http://edu.51cto.com/sd/3ec2c
1. Delayed loading
The significance of lazy loading is that although it is an associative query, it is not timely to query the associated data, but to query when needed
1.1. Lazy Load SQL Analysis
select * from tb_order where order_number=‘20180810001‘select * from tb_user where userid=2
1.2. Global configuration File Settings
<!-- 开启延迟加载 --><setting name="lazyLoadingEnabled" value="true"/><!-- 按需要加载 --><setting name="aggressiveLazyLoading" value="false"/>
1.3. Define the interface method
public List<Order> queryLazy(@Param("orderNum")String orderNum);
1.4. mapper.xml file Configuration
<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 method
@Test public void testLazy()throws Exception{ List<Order> list = orderMapper.queryLazy("20180810001"); for (Order order : list) { System.out.println(order.getOid()); } }
1.6. Observation Log
DEBUG - Opening JDBC ConnectionDEBUG - Created connection 80004644.DEBUG - Setting autocommit to false on JDBC Connection [[email protected]]DEBUG - ==> Preparing: select * from tb_order where order_number=? DEBUG - ==> Parameters: 20180810001(String)DEBUG - <== Total: 11DEBUG - Resetting autocommit to true on JDBC Connection [[email protected]]DEBUG - Closing JDBC Connection [[email protected]]DEBUG - Returned connection 80004644 to pool.
Modify the code, call the Lazy property operation, observe the log again
DEBUG - Setting autocommit to false on JDBC Connection [[email protected]]DEBUG - ==> Preparing: select * from tb_order where order_number=? DEBUG - ==> Parameters: 20180810001(String)DEBUG - <== Total: 11DEBUG - Cache Hit Ratio [cn.org.kingdom.mapper.OrderMapper]: 0.0DEBUG - ==> Preparing: select * from tb_user where userid=? DEBUG - ==> Parameters: 2(BigDecimal)DEBUG - <== Total: 1阿柯DEBUG - Resetting autocommit to true on JDBC Connection [[email protected]]DEBUG - Closing JDBC Connection [[email protected]]DEBUG - Returned connection 313288686 to pool.
2. MyBatis Call stored Procedure 2.1, MyBatis call no parameter stored procedure
1, first to define a parameter-free stored procedure
create or replace procedure mypro isbegin insert into dept(deptno,dname,loc) values(60,‘销售部‘,‘北京‘); commit;end mypro;
2. Define the interface method
public void callPro();
3. Define Mapper File
<select id = "callPro" statementType="CALLABLE"> {call mypro} </select>
4. Write Test class
@Test public void callpro1()throws Exception{ orderMapper.callPro(); }
2.2. Call a stored procedure with parameters
Define a stored procedure that has parameters
create or replace procedure pro_getUserNameById(v_userid in tb_user.userid%type,v_username out tb_user.user_name%type) isbegin select user_name into v_username from tb_user where userid=v_userid;end pro_getUserNameById;
interface method definition
public void callPro2(User vo);
Configuration of the Mapper.xml file
<!-- 调用有参数的 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 method
@Test public void callpro2()throws Exception{ User user = new User(); user.setUserid(2); orderMapper.callPro2(user); System.out.println(user.getUserName()); }
Log information
DEBUG - Opening JDBC ConnectionDEBUG - Created connection 1723550754.DEBUG - Setting autocommit to false on JDBC Connection [[email protected]]DEBUG - ==> Preparing: {call pro_getusernamebyid( ?, ? )} DEBUG - ==> Parameters: 2(Integer)阿柯DEBUG - Resetting autocommit to true on JDBC Connection [[email protected]]DEBUG - Closing JDBC Connection [[email protected]]DEBUG - Returned connection 1723550754 to pool.
MyBatis Learning (eight)