<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] ## 步驟 1 : 目前的分頁方式 目前的分頁方式是自己寫分頁對應的limit SQL語句,并且提供一個獲取總數的count(*) SQL。 不僅如此, mapper, service, service.impl 里都要提供兩個方法: list(Page page), count() 分類是這么做的,后續其他所有的實體類要做分頁管理的時候都要這么做,所以為了提高開發效率,把目前的分頁方式改為使用 pageHelper分頁插件來實現。 對于pageHelper插件不熟悉的同學請參考: **SSM中使用PageHelper** ## 步驟 2 : CategoryMapper.xml 1. 去掉total SQL語句 2. 修改list SQL語句,去掉其中的limit ~~~ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.dodoke.tmall.mapper.CategoryMapper"> <select id="list" resultType="Category"> select * from t_category order by id desc </select> <insert id="add" keyProperty="id" useGeneratedKeys="true" parameterType="Category"> insert into t_category(name) values(#{name}) </insert> <delete id="delete"> delete from t_category where id=#{id} </delete> <select id="get" resultType="Category"> select * from t_category where id=#{id} </select> <update id="update" parameterType="Category"> update t_category set name=#{name} where id=#{id} </update> </mapper> ~~~ ## 步驟 3 : CategoryMapper 1. 去掉total()方法 2. 去掉list(Page page)方法 3. 新增list() 方法 ~~~ package com.dodoke.tmall.mapper; import java.util.List; import com.dodoke.tmall.pojo.Category; public interface CategoryMapper { List<Category> list(); void add(Category category); void delete(int id); Category get(int id); void update(Category category); } ~~~ ## 步驟 4 : CategoryService 1. 去掉total()方法 2. 去掉list(Page page)方法 3. 新增list() 方法 ~~~ package com.dodoke.tmall.service; import java.util.List; import com.dodoke.tmall.pojo.Category; public interface CategoryService { List<Category> list(); void add(Category category); void delete(int id); Category get(int id); void update(Category category); } ~~~ ## 步驟 5 : CategoryServiceImpl 1. 去掉total()方法 2. 去掉list(Page page)方法 3. 新增list() 方法 ~~~ package com.dodoke.tmall.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.dodoke.tmall.mapper.CategoryMapper; import com.dodoke.tmall.pojo.Category; import com.dodoke.tmall.service.CategoryService; @Service public class CategoryServiceImpl implements CategoryService { @Autowired CategoryMapper categoryMapper; @Override public List<Category> list() { return categoryMapper.list(); } @Override public void add(Category category) { categoryMapper.add(category); } @Override public void delete(int id) { categoryMapper.delete(id); } @Override public Category get(int id) { return categoryMapper.get(id); } @Override public void update(Category category) { categoryMapper.update(category); } } ~~~ ## 步驟 6 : CategoryController 修改list方法 1. 通過分頁插件指定分頁參數 PageHelper.offsetPage(page.getStart(),page.getCount()); 2. 調用list() 獲取對應分頁的數據 categoryService.list(); 3. 通過PageInfo獲取總數 int total = (int) new PageInfo<>(cs).getTotal(); 其余部分沒有變化 ~~~ package com.dodoke.tmall.controller; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.List; import javax.imageio.ImageIO; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import com.dodoke.tmall.pojo.Category; import com.dodoke.tmall.service.CategoryService; import com.dodoke.tmall.util.ImageUtil; import com.dodoke.tmall.util.Page; import com.dodoke.tmall.util.UploadedImageFile; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; @RequestMapping("") @Controller public class CategoryController { @Autowired CategoryService categoryService; @RequestMapping("admin_category_list") public String list(Model model,Page page) { // 通過分頁插件指定分頁參數 PageHelper.offsetPage(page.getStart(),page.getCount()); // 調用list() 獲取對應分頁的數據 List<Category> cs = categoryService.list(); // 通過PageInfo獲取總數 int total = (int) new PageInfo<>(cs).getTotal(); page.setTotal(total); model.addAttribute("cs", cs); model.addAttribute("page",page); return "admin/listCategory"; } /** * 新增分類 * @param c 分類對象 * @param session 用于在后續獲取當前應用的路徑 * @param uploadedImageFile 用于接受上傳的圖片 * @return 頁面路徑 * @throws IOException */ @RequestMapping("admin_category_add") public String add(Category c, HttpSession session, UploadedImageFile uploadedImageFile) throws IOException { // 新增分類 categoryService.add(c); // 通過session獲取ControllerContext,再通過getRealPath定位存放分類圖片的路徑。 File imageFolder= new File(session.getServletContext().getRealPath("img/category")); // 根據分類id創建文件名 File file = new File(imageFolder,c.getId() + ".jpg"); // 如果/img/category目錄不存在,則創建該目錄,否則后續保存瀏覽器傳過來圖片,會提示無法保存 if(!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } System.out.println(file); // 通過UploadedImageFile 把瀏覽器傳遞過來的圖片保存在上述指定的位置 uploadedImageFile.getImage().transferTo(file); // 通過ImageUtil.change2jpg(file); 確保圖片格式一定是jpg,而不僅僅是后綴名是jpg. BufferedImage img = ImageUtil.change2jpg(file); // 寫入圖片 ImageIO.write(img, "jpg", file); // 客戶端跳轉到admin_category_list return "redirect:/admin_category_list"; } /** * 刪除分類 * @param id 分類id * @param session 用于在后續獲取當前應用的路徑 * @return 頁面路徑 * @throws IOException */ @RequestMapping("admin_category_delete") public String delete(int id,HttpSession session) throws IOException { categoryService.delete(id); File imageFolder= new File(session.getServletContext().getRealPath("img/category")); File file = new File(imageFolder,id+".jpg"); file.delete(); return "redirect:/admin_category_list"; } /** * 根據id,查詢分類信息 * @param id 分類id * @param model 模型 * @return 頁面路徑 * @throws IOException */ @RequestMapping("admin_category_edit") public String edit(int id,Model model) throws IOException { Category c= categoryService.get(id); model.addAttribute("c", c); return "admin/editCategory"; } /** * 更新分類 * @param c 接受頁面提交的分類名稱 * @param session 用于在后續獲取當前應用的路徑 * @param uploadedImageFile 用于接受上傳的圖片 * @return 頁面路徑 * @throws IOException */ @RequestMapping("admin_category_update") public String update(Category c, HttpSession session, UploadedImageFile uploadedImageFile) throws IOException { // 更新分類 categoryService.update(c); MultipartFile image = uploadedImageFile.getImage(); // 判斷是否有圖片上傳 if(null!=image &&!image.isEmpty()){ File imageFolder= new File(session.getServletContext().getRealPath("img/category")); // 根據分類id創建文件 File file = new File(imageFolder,c.getId()+".jpg"); // 把瀏覽器傳遞過來的圖片保存在上述指定的位置 image.transferTo(file); // 確保圖片格式一定是jpg,而不僅僅是后綴名是jpg. BufferedImage img = ImageUtil.change2jpg(file); // 覆蓋圖片 ImageIO.write(img, "jpg", file); } // 客戶端跳轉到admin_category_list return "redirect:/admin_category_list"; } } ~~~ ## 步驟 7 : 修改applicationContext.xml applicationContext.xml中關于插件部分本來是被注釋掉的,現在釋放出來 ~~~ <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <context:annotation-config /> <context:component-scan base-package="com.dodoke.tmall.service" /> <!-- 導入數據庫配置文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置數據庫連接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本屬性 url、user、password --> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="1" /> <property name="minIdle" value="1" /> <property name="maxActive" value="20" /> <!-- 配置獲取連接等待超時的時間 --> <property name="maxWait" value="60000" /> <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT 1" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <!-- 打開PSCache,并且指定每個連接上PSCache的大小 --> <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> </bean> <!--Mybatis的SessionFactory配置--> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="typeAliasesPackage" value="com.dodoke.tmall.pojo" /> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:mapper/*.xml"/> <!--分頁插件,目前先注釋,后面重構的時候才會使用 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value> </value> </property> </bean> </array> </property> </bean> <!--Mybatis的Mapper文件識別--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.dodoke.tmall.mapper"/> </bean> </beans> ~~~ ## 步驟 8 : 重啟tomcat,測試 重啟Tomcat后訪問如下地址,可以看到一樣的分頁查詢效果 `http://localhost:8080/tmall_ssm/admin_category_list?start=5` ![](https://box.kancloud.cn/e5b00445180f63553655033a30cca359_1823x424.png) > 注:重構不會影響功能性,所以重構之后的代碼一樣可以實現分頁的功能。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看