MyBatis的知识点

来源:互联网 发布:js清除浏览器缓存数据 编辑:程序博客网 时间:2024/06/07 22:35
mybatis 分页技术
MyBatis 自带分页 是吧查询出来的结果集进行分页 ====内存分页
 eg: RowBounds rb= new RowBounds(offset, limt); //offset 起始行   limt 一页多少个
 
 2,Mybatis与Mysql结合 当表中有自动增长 插入后 查看ID
    <insert id="insertUser" parameterType="com.shi.User">
  <selectkey keyProperty="id" resultType="Integer" order="after">
    select last_insert_id();
  </selectkey>
   insert into ...
</insert> 
 3,#{}代表占位符(防sql注入) ${}代表字符串连接(不防sql注入)
 4,指定Mapper文件的方法
    <mappers>
<!--  mapper 标签--->
<mapper  resource="mapper.xml所在的位置"/>
<mapper  class="mapper.xml对应接口所在的位置"/>
<!-- package 标签 要求 接口名与mapper 名一样 放在同一个包下-->
<package name="包名"/>
</mappers>
  5,resultMap 映射
  <!-- -->
       <resultMap type=""  id="" >
  <id cloumn="" property="" 主键> 
  <result 普通属性>
  <association  一对一>
  <collection  一对多>
  </resultMap>
  6,if标签
     <if test="布尔表达式"></if>
<where>
  <if></if>       //去掉第一个if的前and
</where>
 
   7,sql片段
      <sql id="">抽取公共sql语句</sql>
 <select>
     <include rfid="sql标签的id" />  //引入公共的sql
 
 </select>
  8,当传入的值是多个值时使用 <foreach></foreach> 标签
      <foreach  collection="要遍历的集合 数组放array 集合写list"  item =“ 遍历的元素” separate=“,断点,分割点” open=“ id in (” close=“)” >
 #{id}
 </foreach>
   9,在resultMap映射时 单张表查询可以只映射 字段和属性不同的 在一对一关联association 时必须映射所有的字段和属性
10,在resultMap 映射 collection时  <collection property="字段名" ofType=“集合的泛型名”  
11,mapper动态代理增强版----扫描
  普通mapper动态代理:
    <bean name="dao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.run.dao.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
   </bean> 
mapper增强--扫描
   <bean class=“org.mybatis.spring.mapper.MapperScannerCofigurer”>
 <!-- 注入基本包-->
 <property  name="basePackage" value="mapper接口所在的包">
</bean>
原创粉丝点击