mybaits(级联操作)

缓存:内存与远程硬盘(服务器)之间关系

原理:查询数据的时候,应用程序现在内存中查询,如果没有查到,在到磁盘查询,查询出来的结果给缓存,缓存再给应用程序!

好处:下次不需要经过磁盘,直接找缓存。

mybatis的一级缓存:Sqlsession,所有的操作都由SqlSession发出的,对应的是实体类。

mybatis的二级缓存:SqlSessionFactory,称之为session工厂,采用工厂模式,目的是为了生成sqlsession对象,称之为二级缓存,对应的是数据库。

配置多对一(Bizmapper.xml):

resultMap(高级映射)   association(关系)

    <!--映射关系  -->
    <resultMap  id="rmap" type="sbill">
    <!--id称为主键   property:属性(javabean) column:数据表中的  -->
        <id property="id"  column="id" />
        <result property="billcode" column="billcode"  />
        <result property="productname" column="productname"  />
        <result property="ispayment" column="ispayment"  />
        <result property="creationdate" column="creationdate"  />
        <!--配置多对一  
        property:这个属性是一个什么类型
        javaType:这个属性是什么类型
          -->
        <association  property="spid" javaType="sprovider">
                <id property="id"  column="id" />
                <result property="proname" column="proname"/>
        </association>
    </resultMap>

    <select id="getAllSmbmsBillByRa" resultMap="rmap">
        select a.id,
           a.billcode,
           a.productname,
           a.totalprice,
           a.ispayment,
           a.creationdate,
           b.proname
             from smbms_bill a
            inner join smbms_provider b on a.providerid = b.id
    </select>

pojo实体类:

private Integer id;
    private String billcode;
    private String productname;
    private String productdesc;
    private String productunit;
    private Double productcount;
    private Double totalprice;
    private Integer ispayment;
    private Integer createdby;
    private Date creationdate;
    private Integer modifyby;
    private Date modifydate;
    private Integer providerid;
    private SmbmsProvider spid;//声明一个类:代表一方

get/set方法省略

接口方法:List<SmbmsBill>   getAllSmbmsBillByRa(); 

通过HashSet集合(键是唯一的)   resultMap(高级映射)  collection(集合)

配置一对多(Bizmapper.xml):

<resultMap id="rmap2" type="srole">
    
    <result  property="rolename" column="rolename"></result>
    <!--集合 对应HashSet 集合
        ofType:在collection中叫做ofType  类似于association中的javaType
        ofType:集合中装载的类型  指的不是集合类型 java.util.HashSet
      -->
        <collection  property="smbmsuser" ofType="suser">
        <id property="id"  column="id" />
        <result property="usercode" column="usercode"  />
        <result property="username" column="username"  />
        <result property="gender" column="gender"  />
        <result property="age" column="age"  />
        <result property="phone" column="phone"  />
        </collection>
    </resultMap>
    
    <select id="getAllUserInfo" resultMap="rmap2" > 
    select b.id,
       b.usercode,
       b.username,
       b.gender,
       trunc(months_between(sysdate, b.birthday) / 12) as age,
       b.phone,
       a.rolename
       from smbms_role a
       inner join smbms_user b on a.id = b.userrole
    </select>

pojo实体类:

private Integer id; 
    private String rolecode;
    private String rolename;
    private Integer createdby;
    private Date   creationdate;
    private Integer modifyby;
    private Date   modifydate;
    private Set<SmbmsUser>  smbmsuser=new HashSet<SmbmsUser>();

get/set方法省略

接口方法:List<SmbmsRole>   getAllUserInfo();

 

 

多表查询,不经过pojo,通过mybatis本身提供的resultType类型,直接返回其它类型?  ----->集合

<!--不经过 javabean,直接返回HashMap?  存储原理:把所有的字段当成key,找值  -->
    <select id="getAllSmbmsBill_r" resultType="java.util.HashMap">
    select a.id,
         a.billcode,
         a.productname,
         a.totalprice,
         a.ispayment,
         a.creationdate,
         b.proname
         from smbms_bill a
         inner join smbms_provider b on a.providerid = b.id
    </select>

方法:List<HashMap<Object, Object>>  getAllSmbmsBill_r();//通过HashMap把所有字段当成key,找值

测试:遍历集合中的集合

:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

brid_fly

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值