本教程对应视频课程地址: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 [[email protected]]
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 [[email protected]]
DEBUG - Closing JDBC Connection [[email protected]]
DEBUG - Returned connection 80004644 to pool.
修改代码,调用懒属性操作,再次观察日志
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: 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 [[email protected]]
DEBUG - Closing JDBC Connection [[email protected]]
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 [[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.
原文地址:http://blog.51cto.com/11230344/2302211
时间: 2024-10-27 17:59:28