通用mapper和分類實現

來源:互聯網
上載者:User

標籤:詳細說明   需要   items   property   總結   resource   必須   面向   col   

通用Mapper1.1  通用Mapper介紹1.1.1  架構設計

 

說明:使用了通用Mapper後,單表的增刪改查操作會自動的進行維護.

問題:如何才能實現資料的通用並且是動態?

 

1.2  JPA介紹1.2.1  JPA的思想

 

說明:以物件導向的思維操作資料庫!!

舉例說明:

  1. 早期sql語句都需要人為的編輯.
  2. 關係型資料庫中資料表與pojo一一對應.所以可以使用對象操作資料庫
  3. Sql:insert into user values(XXXX);

UserMapper.insert(user);

1.2.2  J PA 的發展

說明:有了JPA思想後,Haibernate將JPA實現.

特點:

  1. 能夠實現物件導向的操作
  2. 能夠實現自動的對象關係映射(orm)

  問題:

例子:

如果做插入操作,先會執行查詢操作,之後再插入.

實現商務邏輯時,會產生大量的冗餘的sql語句.資料庫的執行速度變慢.

  2.需要學習特定的資料庫語句Hql(適用於多表操作)

 

發展:

Mybatis的發展.

 特點:

  1. 能夠實現自動的對象關係映射
  2. Sql語句需要自己根據商務邏輯自己實現,效能更高
  3. 通用Mapper出現後,Mybatis也有了物件導向的功能.
1.2.3  通用Mapper引入
<!-- 通用Mapper外掛程式 -->        <plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">            <!--主鍵自增回寫方法,預設值MYSQL,詳細說明請看文檔 -->            <property name="IDENTITY" value="MYSQL" />            <!--通用Mapper介面,多個通用介面用逗號隔開 -->            <property name="mappers" value="com.jt.common.mapper.SysMapper" />        </plugin>
1.2.4  Mapper的介面的註解形式
/**     * Mybatis的介面中可以添加註解,完成特定的操作     * 說明:     *     Mybatis中的直接根據映射標籤後期開發的.     *  功能上與對應檔一致.     * @return     */    @Select(value="select * from item")    //@Insert("")    //@Delete("")    //@Update("")    List<Item> selectAll();

 

1.2.5  通用Mapper調用規則

 

方法名稱是對應的,可以自動的進行調用

 

 

1.3  商品的新增1.3.1  商品分類的級數

說明:一般的電商網站的商品分類一般都是3級.經過了科學的考證的

 

1.3.2  構建Item Cat 對象

 

1.3.3  構建Item Cat Mapper

 

1.3.4  定義Item CatService
@Servicepublic class ItemCatServiceImpl implements ItemCatService {        @Autowired    private ItemCatMapper itemCatMapper;    /**     * 使用通用Mapper(JPA),傳入的對象最終充當了查詢的where條件     * select * from tb_item_cat where id = 100 and status = 1     *      * 總結:ItemCat對象會將不為Null的屬性充當where條件* /如果需要添加查詢條件     * 為對象的屬性賦值即可!!!     *      */    @Override    public List<ItemCat> findItemCat() {        //ItemCat itemCat = new ItemCat();        //itemCat.setId(100L);        //itemCat.setStatus(1);        return itemCatMapper.select(null);    }

 

1.3.5  編輯Item CatController

 

1.4  商品分類列表的實現1.4.1  頁面的 Url 分析

 

 

 

1.4.2  分析樹形結構

 

{"id":2,"text":"商品名",state:"closed"}

注:state的屬性如果是closed,表示這個是父節點,它還有子節點。open代表子節點

 

1.4.3   擴充節點

 

 

1.4.4  編輯Pojo對象

說明:根據格式要求編輯get方法:

 

1.4.5  編輯Controller
/**     * [email protected]      *  作用:     *      如果返回的資料時對象則自動的轉化為JSON{key:value}     *      如果返回的資料為String  則按照字串原樣返回 String     *  注意:轉化JSON資料時,調用的是對象中的getXXX()方法     * @return     */    //商品分類實現    @RequestMapping("/list")    @ResponseBody    public List<ItemCat> findItemCat    (@RequestParam(value="id",defaultValue="0") Long parentId){        //Long parentId = 0L;    //定義一級菜單的父級                //根據parentId查詢商品的分類資訊        return itemCatService.findItemCatByParentId(parentId);    }

 

1.4.6  編輯Service
@Override    public List<ItemCat> findItemCatByParentId(Long parentId) {        ItemCat itemCat = new ItemCat();        itemCat.setParentId(parentId);        itemCat.setStatus(1); //正常的分類資訊                return itemCatMapper.select(itemCat);    }

 

1.4.7  效果展現

 

 

1.5  商品的新增1.5.1  分析頁面ur l

 

 

1.5.2  編輯 pojo 對象

說明:將pojo對象與資料庫表一一對應

 

1.5.3  編輯 Controller

 

1.5.4  編輯Service

 

1.5.5  效果展現

 

1.5.6  Easy UI 的校正
  1. 必填項

data-options="required:true"

  1. 設定值的範圍

data-options="min:1,max:99999999,precision:2,required:true"

  1. 定義字元的個數

data-options="validType:‘length[1,30]‘

1.6  商品的修改1.6.1  頁面js分析

 

 

 

1.6.2  編輯Controller
//引入日誌工具類    private static final Logger logger = Logger.getLogger(ItemController.class);@RequestMapping("/update")    @ResponseBody    public SysResult updateItem(Item item){        try {            itemService.updateItem(item);            logger.info("{~~~~~更新成功}");            return SysResult.build(200,"更新成功");        } catch (Exception e) {            e.printStackTrace();            //throw new Exception();            //記錄日誌            //System.out.println("sssssss");            logger.error("{更新操作失敗}");            return SysResult.build(201, "更新失敗");        }    }

 

1.6.3  編輯Service

 

1.6.4  動態更新操作(知識回顧)
<!--測試的動態更新         set作用:            1.動態更新時使用            2.能夠去除where條件之前的多餘的1個逗號     -->    <update id="updateUser">        update tb_user  set name = #{name} age=#{age} where id = #{id}        <set>            <if test="name !=null">name = #{name},</if>            <if test="age  !=null">age = #{age},</if>         </set>        where id = #{id}    </update>

 

1.7  商品刪除1.7.1  頁面分析

 

 

 

 

1.7.2  編輯Controller

 

 

1.7.3  編輯Service

 

 

1.8  商品上架下架1.8.1  上架和下架的頁面分析

 

1.8.2  編輯Controller

 

1.8.3  編輯service
@Override    public void updateStatus(int status, Long[] ids) {                /**         * 方案1:         *     在service層通過迴圈遍曆的形式實現操作         * 方案2:         *     通過Mybatis實現一次批量修改資料的操作         */                itemMapper.updateStatus(status,ids);                /*for (Long id : ids) {            Item item = new Item();            item.setId(id); //封裝主鍵            item.setStatus(status);            item.setUpdated(new Date());            itemMapper.updateByPrimaryKeySelective(item);        }*/    }

 

1.8.4  編輯Mybatis
<!--批量修改狀態         collection 的取值有如下的幾種        1.如果傳遞的資料是數組              array        2.如果傳遞的資料是List集合      list        3.如果傳遞的資料是Map        map中的key     -->    <update id="updateStatus">        update tb_item set status = #{status} where id in(            <foreach collection="ids" item="id" separator=",">                #{id}            </foreach>        )
  </update>

 

1.9  Log 4j 日誌1.9.1  說明:
  1. 項目可以自動的掃描\resources\log4j.properties.名稱必須固定.
  2. 引入jar包檔案

 

補充知識2.1  快捷配置

說明:能夠在new中出現class interface等java的工具類

 

 

2.1.1  jQuery Validate

 

 

通用mapper和分類實現

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.