通用mapper和分类实现
1?通用Mapper
1.1?通用Mapper介紹
1.1.1?架構設計
?
說明:使用了通用Mapper后,單表的增刪改查操作會自動的進行維護.
問題:如何才能實現數據的通用并且是動態的?
?
1.2?JPA介紹
1.2.1?JPA的思想
?
說明:以面向對象的思維操作數據庫!!
舉例說明:
UserMapper.insert(user);
1.2.2?JPA的發展
說明:有了JPA思想后,Haibernate將JPA實現.
特點:
? 問題:
例子:
如果做插入操作,先會執行查詢操作,之后再插入.
實現業務邏輯時,會產生大量的冗余的sql語句.數據庫的執行速度變慢.
??2.需要學習特定的數據庫語句Hql(適用于多表操作)
?
發展:
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?構建ItemCat對象
?
1.3.3?構建ItemCatMapper
?
1.3.4?定義ItemCatService
@Service public class ItemCatServiceImpl implements ItemCatService {@Autowiredprivate ItemCatMapper itemCatMapper;/*** 使用通用Mapper(JPA),傳入的對象最終充當了查詢的where條件* select * from tb_item_cat where id = 100 and status = 1* * 總結:ItemCat對象會將不為Null的屬性充當where條件 * /如果需要添加查詢條件* 為對象的屬性賦值即可!!!* */@Overridepublic List<ItemCat> findItemCat() {//ItemCat itemCat = new ItemCat();//itemCat.setId(100L);//itemCat.setStatus(1);return itemCatMapper.select(null);}?
1.3.5?編輯ItemCatController
?
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
/*** 1.@ResponseBody * 作用:* 如果返回的數據時對象則自動的轉化為JSON{key:value}* 如果返回的數據為String 則按照字符串原樣返回 String* 注意:轉化JSON數據時,調用的是對象中的getXXX()方法* @return*///商品分類實現@RequestMapping("/list")@ResponseBodypublic List<ItemCat> findItemCat(@RequestParam(value="id",defaultValue="0") Long parentId){//Long parentId = 0L; //定義一級菜單的父級//根據parentId查詢商品的分類信息return itemCatService.findItemCatByParentId(parentId);}?
1.4.6?編輯Service
@Overridepublic 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?分析頁面url
?
?
1.5.2?編輯pojo對象
說明:將pojo對象與數據庫表一一對應
?
1.5.3?編輯Controller
?
1.5.4?編輯Service
?
1.5.5?效果展現
?
1.5.6?EasyUI的校驗
data-options="required:true"
data-options="min:1,max:99999999,precision:2,required:true"
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")@ResponseBodypublic 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
@Overridepublic 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.如果傳遞的數據是數組 array2.如果傳遞的數據是List集合 list3.如果傳遞的數據是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?Log4j日志
1.9.1?說明:
?
2?補充知識
2.1?快捷配置
說明:能夠在new中出現class?interface等java的工具類
?
?
2.1.1?jQuery Validate
?
?
轉載于:https://www.cnblogs.com/xiangyuqi/p/8571530.html
總結
以上是生活随笔為你收集整理的通用mapper和分类实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Callable、Future、Futu
- 下一篇: USACO-Section1.6 Num