40.MyBaits懒加载、一二级缓存、模糊查询、分页查询、动态SQL
MyBaits_Day04
第一節、作業
<!-- 對getAllCateAndGoods做實現--><select id="getAllCateAndGoods" resultMap="cateAndGoods">SELECT * FROM category;</select><!-- type表示resultMap是在封裝誰的對象 --><resultMap type="category" id="cateAndGoods"><id property="cid" column="cid"/><!-- allGoods是一個list集合 myBatis不能自動封裝--><!--對應list類型 我們用collection --><collection property="allGoods" ofType="goods" select="getGoodsByCid" column="cid"></collection></resultMap><select id="getGoodsByCid" resultType="goods">SELECT * FROM goods WHERE cid=#{cid};</select> private int cid;private String cname;//每一個分類下對應的商品//分析:一個商品分類下面有多個商品--一對多--一個分類下有多個商品(多個-集合)private List<Goods> allGoods;第二節、MyBatis做級聯查詢的時候的懶加載
什么是懶加載:按需加載
場景:我頁面上只需要顯示分類,當用戶選中某個分類的時候,才去加載商品
版本一:(沒有開啟懶加載)每次頁面進來,只要調用接口,會查詢分類同時也查詢分類下的商品
解決:開啟懶加載
方法一:
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-u8bTsIQd-1631707125142)(C:\Users\王元元\AppData\Roaming\Typora\typora-user-images\image-20210904192103279.png)]
方法二:
<collection property="allGoods" ofType="goods" select="getGoodsByCid" column="cid" fetchType="lazy"></collection>=什么叫關聯對象:
查分類及其分類的商品的時候,每一個分類下對應的商品就是關聯對象
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-WS0M0PPZ-1631707125143)(C:/Program%20Files/Typora/upload/image-20210904101652038.png)]
第三節、MyBatis中緩存
什么叫緩存:
緩存的作用:
1、提高訪問的效率(緩存放在內存)
2、減小數據庫的壓力
3、緩存有一個過期時間
MyBaits中緩存
一級緩存:sqlSession(通一個SqlSession的操作會觸發緩存 默認開啟的)
SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession(); CategoryMapper mapper = sqlSession.getMapper(CategoryMapper.class); List<Category> allCateAndGoods = mapper.getAllCateAndGoods(); sqlSession.clearCache();//清除緩存 CategoryMapper mapper2 = sqlSession.getMapper(CategoryMapper.class); List<Category> allCateAndGoods2 = mapper.getAllCateAndGoods(); System.out.println(allCateAndGoods==allCateAndGoods2);//地址比較二級緩存:基于mapper(同一個mapper下的操作觸發緩存)
開啟二級緩存的步驟:
1、全局配置文件中開啟緩存
<setting name="cacheEnabled" value="true"/>2、需要開啟二級緩存mapper開啟緩存
3、mapper中操作的實體類實現序列化接口
注意點:如果實體類中有關聯對象,關聯對象也需要實現序列化接口
public class Category implements Serializable{第四節、模糊查詢
直接將模糊的條件傳入即可
<select id="getGoodByLike" resultType="goods">SELECT * FROM goods WHERE name LIKE #{name} </select> SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession(); GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class); List<Goods> goodByLike = mapper.getGoodByLike("%紅%");//直接將模糊條件傳入即可 for (Goods goods : goodByLike) {System.out.println(goods); }第五節、分頁查詢
方式一、
1、在Mapper中直接通過SQL實現(DAO)
<select id="getGoodsBylimit" resultType="goods">SELECT * FROM goods limit #{start},#{pagesize} </select>2、在Service層調用DAO層(需要將前臺傳入參數做計算)
SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession(); GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);//查第幾頁 每頁的條數 controller -->service-->dao List<Goods> goodsBylimit = mapper.getGoodsBylimit(2, 2);3、數據來源從頁面上來–接收是通過Controller
注意:分頁的計算方式: (pagenum-1)*pageSize, pageSize
方式二、通過PageHelper實現
pageHelper是MyBaits一個分頁插件
環境搭建:
1、導入依賴pagehelper-5.1.2.jar jsqlparser-1.0.jar
2、在全局配置文件中使用pageHelper的插件
<plugins><!-- com.github.pagehelper為PageHelper類所在包名 --><plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> </plugins>3、使用—Service層
//1.配置分頁pageNum 第幾頁 pageSize 每一頁多少條 PageHelper.startPage(2, 2); SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession(); GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class); List<Goods> allGoods = mapper.getAllGoods();//通過pageInfo對象進行分頁 PageInfo<Goods> pageInfo = new PageInfo<>(allGoods);List<Goods> list = pageInfo.getList(); for (Goods goods : list) {System.out.println(goods); } System.out.println(pageInfo.getTotal());//獲取數據的總數 System.out.println(pageInfo.getPrePage());//pageInfo 封裝分頁的一些信息第六節、動態SQL
動態 SQL 是 MyBatis 的強大特性之一。如果你使用過 JDBC 或其它類似的框架,你應該能理解根據不同條件拼接 SQL 語句有多痛苦,例如拼接時要確保不能忘記添加必要的空格,還要注意去掉列表最后一個列名的逗號。利用動態 SQL,可以徹底擺脫這種痛苦。
if標簽
<if test="title != null">規則:如果if中條件滿足,則將if標簽SQL進行拼接
<!--如果條件指傳我才根據傳入的條件進行查詢什么都不傳 查所有如:頁面傳了名字 按照名字查頁面傳了名字和地址 按名字和地址...--> SELECT * FROM goods WHERE 1=1 <if test="name!=null and name!=''">AND name=#{name} </if> <if test="location!=null and location!=''">AND location=#{location} </if> <if test="price!=null and price!=0">AND price > #{price} </if> </select>choose、when、otherwise
作用:類似于JAVA中switch
<select id="getGoods" resultType="goods">SELECT * FROM goods WHERE 1=1<choose><when test="name!=null and name!=''">AND name=#{name}</when><when test="location!=null and location!=''">AND name=#{name}</when><otherwise>ORDER BY price DESC;</otherwise></choose> </select>[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-Ehi7cAvV-1631707125150)(upload/image-20210904154728332.png)]
where
<select id="getGoods" resultType="goods">SELECT * FROM goods <where><if test="name!=null and name!=''">AND name=#{name}</if><if test="location!=null and location!=''">AND location=#{location}r</if></where>set
<update id="getGoods">update goods<set><if test="name!=null and name!=''">name=#{name},</if><if test="location!=null and location!=''">location=#{location},</if> </set>WHERE id=#{id} </update>trim標簽----自定義標簽
foreach標簽 -----遍歷list或者map集合
/if>
location=#{location},
WHERE id=#{id}
總結
以上是生活随笔為你收集整理的40.MyBaits懒加载、一二级缓存、模糊查询、分页查询、动态SQL的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何查看chrome浏览器插件位置
- 下一篇: 基于javaweb的茶叶商城管理系统(j