Mybitas 笔记

1. #{} 和 ${}区别

#{} 会有预编译防止sql 注入, ${}则是字符串替换,无法防止sql注入,因此用前者

什么是sql注入

预编译会将参数里面的 引号等符合进行转移
String param ="’=test’ or 1=1 " ;
where name= ’ ‘test’ or 1=1’ ;
where name=‘test’ or 1=1 ;这就是注入

如何获取自动增长的主键

在执行insert的时候会传入一个对象,当执行完sql的时候,该对象会自动被赋予主键

Mybitas和Hibernate

1、mybitas 一个半ORM框架,只需要自己关注sql,使得更为灵活,并将结果通过xml配置映射成pojo返回, 将sql封装成statment语句
2、只是过度依赖sql 数据库迁移性困难大
3、mybitas 适用于数据模型关联度小业务,

1、hibernate 适用于高数据模型关联业务,全自动ORM,数据库无关性
2、不关注sql 无法对SQL进行优化 在批量数据处理时有弱势,,
3、级关联查询设置不当(lazy=false)导致查询缓慢,不灵活

一对一,一对多关联查询

<mapper namespace="com.lcb.mapping.userMapper">

    <!--association 一对一关联查询 -->
    <select id="getClass" parameterType="int" resultMap="ClassesResultMap">
            select * from class c,teacher t where c.teacher_id=t.t_id and  c.c_id=#{id}
      </select>
    <resultMap type="com.lcb.user.Classes" id="ClassesResultMap">
      <!-- 实体类的字段名和数据表的字段名映射 -->
      <id property="id" column="c_id"/>
      <result property="name" column="c_name"/>
      <association property="teacher" javaType="com.lcb.user.Teacher">
        <id property="id" column="t_id"/>
        <result property="name" column="t_name"/>
      </association>
    </resultMap>


    <!--collection 一对多关联查询 -->
    <select id="getClass2" parameterType="int" resultMap="ClassesResultMap2">
            select * from class c,teacher t,student s where c.teacher_id=t.t_id and c.c_id=s.class_id and c.c_id=#{id}
    </select>
  
    <resultMap type="com.lcb.user.Classes" id="ClassesResultMap2">
      <id property="id" column="c_id"/>
      <result property="name" column="c_name"/>
      <association property="teacher"   javaType="com.lcb.user.Teacher">
        <id property="id" column="t_id"/>
        <result property="name" column="t_name"/>
      </association>
      <collection property="student"  ofType="com.lcb.user.Student">
        <id property="id" column="s_id"/>
        <result property="name" column="s_name"/>
      </collection>
    </resultMap>

</mapper>

Mybitas 分页原理

每个DaoMapper都是JDK动态代理基于MapperProxy实现的代理对象,在MapperProxy执行的时候

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值