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执行的时候