Mapper.xml提示:
1:mapper包中新建一個文件:mybatis-3-mapper.dtd
2:在web app libraries/mybatis.jar/org.apache.ibatis.builder.xml/mybatis-3-mapper.dtd,打開,復制內容,貼到自己新建mybatis-3-mapper.dtd
3:把mapper.xml頭上的dtd的路徑該為當前目錄!
4:關閉StuMapper.xml文件,重新打開,即有提示!
#{}和${}區別: ${} 不會數值,不考慮數據類型,直接拼入sql,如果是字符串,沒有單引號,sql異常!Statment,可能會發生sql注入問題! #{} 不會發生:自動考慮數據類型,字符串會自動拼接'',PreparedStatment
mybatis:動態sql標簽
<if> <choose> <when> <otherwise> <where> <set> <foreach> 必須能解釋清楚 剩下:在逆向生成的mapper.xml中:自己看!擴展
<select id=”findByLike” resultType=”com.stu.bean.Stu” parameterType=”com.stu.bean.Stu”>
selec * from stu where 1=1
<if test=”uname!=null”>and uname=#{uname}</if>
<if test=”uage!=null”>and uage=#{uage}</if>
<if test=”usex!=null”>and usex=#{usex}</if>
</select>
<select id=”findByLike” resultType=”com.stu.bean.Stu” parameterType=”com.stu.bean.Stu”>
selec * from stu where 1=1
<if test=”uname!=null”>or uname=#{uname}</if>
<if test=”uage!=null”>or uage=#{uage}</if>
<if test=”usex!=null”>or usex=#{usex}</if>
</select>
問題:如果永遠都是全表
<select id=”findByLike” resultType=”com.stu.bean.Stu” parameterType=”com.stu.bean.Stu”>
selec * from stu where 1!=1
<if test=”uname!=null”>or uname=#{uname}</if>
<if test=”uage!=null”>or uage=#{uage}</if>
<if test=”usex!=null”>or usex=#{usex}</if> </select>解決上面問題
<select id="xxx3" parameterType="com.stu.bean.Stu" resultType="com.stu.bean.Stu">
select * from stu where 1!=1
<choose> <!-- if elseif elseif -->
<when test="sid!=null">
or sid = #{sid}
</when>
<when test="sname!=null">
or sname like #{sname}
</when>
<when test="sage!=null">
or sage = #{sage}
</when>
<otherwise>
or saddress like #{saddress}
</otherwise>
</choose> 條件只可能成立一個!
</select>
<select id="xxx4" parameterType="com.stu.bean.Stu" resultType="com.stu.bean.Stu">
select * from stu
<where> <!-- 根據條件自動拼接where和and -->
<if test="sname!=null">
and sname=#{sname}
</if>
<if test="saddress!=null">
and saddress=#{saddress}
</if>
</where>
</select>
<update id="xxx5" parameterType="com.stu.bean.Stu">
update stu <!-- 根據條件自動拼接set和逗號 -->
<set>
<if test="sname!=null">
sname=#{sname},
</if>
<if test="sage!=null">
sage=#{sage},
</if>
<if test="ssex!=null">
ssex=#{ssex},
</if>
<if test="saddress!=null">
saddress=#{saddress},
</if>
</set>
<where>
<if test="sid!=null">
sid=#{sid}
</if>
</where>
</update>
<Delete id=”aaa” parameterType=”array”>
Delete from stu where sid in
<foreach collection=”array” index=”index” item=”item” open=”(” separator=”,” close=”)”> #{item}
</foreach>
</Delete >
=========================================================================================
逆向生成:
1:工具需要插件:放入dropins,重啟my10!
2:考入jar包,並且把mysql。jar導入工作區
3: 考入generatorConfig.xml文件,修改路徑
4: 右鍵 generatorConfig.xml,點擊蝴蝶 XxxExample:方法條件樣例
使用:
SqlSession session= MybatisUtil.getSqlSessionFactory().openSession();
StuMapper sm = session.getMapper(StuMapper.class);
sm.selectByPrimaryKey(Integer sid); //和原來自己寫xml方法一樣 sm.selectByExample(Stuexample se); StuExample se = new StuExample(); Criteria c = se.createCriteria(); c.andxxxxx() sm.selectByExample(null); 查詢所有 sm.countByExample(Stuexample se); 統計Criteria條件的行數 sm.deleteByExample(Stuexample e); sm.deleteByPrimaryKey(Integer sid); //和原來自己寫xml方法一樣 sm.insert(Stu s); //和原來自己寫xml方法一樣,注意主鍵! sm.insertSelective(Stu s); 哪個屬性不為null,插入哪個屬性 sm.updateByExample(Stu s, Stuexample e); sm.updateByExampleSelective(Stu s, Stuexample e); sm.updateByPrimaryKey(Stu s); //和原來自己寫xml方法一樣 sm.updateByPrimaryKeySelective(Stu s); 哪個屬性不為null,修改哪個屬性 條件或者: se.or(criteria2);
排序: se.setOrderByClause("sid desc");
分頁: oracle: select * from ( select x1.*,rownum rr from (select * from stu order by sid desc) x1 where rr<=5*n ) x2 where x2.rr>=5*n-4
mysql: select * from stu order by sid desc limit 5*n-5,5
mybatis:不使用Mapper,使用session.selectList(); 使用一個類:RowBounds SqlSession session= MybatisUtil.getSqlSessionFactory().openSession(); RowBounds rb = new RowBounds(pageSize*pageNow-pageSize,pageSize); List<Stu> list = session.selectList("com.aaa.mapper.StuMapper.selectByExample",null, rb);
重點:理解selective:所有的屬性添加了if判斷 Example: 對象的模板 Criteria:sql條件模板 StuExample se = new StuExample(); Criteria c = se.createCriteria(); 熟練使用:XXXExample類
============================================================================
作業:2表crud servlet-》biz->mapper(分頁 模糊查詢)
SqlSessionFactroy的作用:
SqlSession的作用:
StuMapper.xml中描述:必須清楚,動態sql標簽,resultType resultMap 對象間的關系 xml描述
${}和#{}的區別! StuExample的作用:提供了條件模板,提供了排序,提供去冗余 Criteria的作用: 排序 分頁
=========================================================================
group by:分組:只要用到分組,只能必須一定查詢組信息 組信息:select 組名,5個聚合函數 from 表名 group by 組名 having:給組加條件,只能出現組名,5個聚合函數
符合排序:select * from stu order by sage desc,sid asc 符合分組:select ssex,count(*) from stu group by ssex,saddress... 是把ssex和saddress完全相同分為一組!