通用mapper和分类实现
1?通用Mapper
1.1?通用Mapper介紹
1.1.1?架構(gòu)設(shè)計(jì)
?
說明:使用了通用Mapper后,單表的增刪改查操作會自動的進(jìn)行維護(hù).
問題:如何才能實(shí)現(xiàn)數(shù)據(jù)的通用并且是動態(tài)的?
?
1.2?JPA介紹
1.2.1?JPA的思想
?
說明:以面向?qū)ο蟮乃季S操作數(shù)據(jù)庫!!
舉例說明:
UserMapper.insert(user);
1.2.2?JPA的發(fā)展
說明:有了JPA思想后,Haibernate將JPA實(shí)現(xiàn).
特點(diǎn):
? 問題:
例子:
如果做插入操作,先會執(zhí)行查詢操作,之后再插入.
實(shí)現(xiàn)業(yè)務(wù)邏輯時,會產(chǎn)生大量的冗余的sql語句.數(shù)據(jù)庫的執(zhí)行速度變慢.
??2.需要學(xué)習(xí)特定的數(shù)據(jù)庫語句Hql(適用于多表操作)
?
發(fā)展:
Mybatis的發(fā)展.
?特點(diǎn):
1.2.3?通用Mapper引入
<!-- 通用Mapper插件 --><plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor"><!--主鍵自增回寫方法,默認(rèn)值MYSQL,詳細(xì)說明請看文檔 --><property name="IDENTITY" value="MYSQL" /><!--通用Mapper接口,多個通用接口用逗號隔開 --><property name="mappers" value="com.jt.common.mapper.SysMapper" /></plugin>1.2.4?Mapper的接口的注解形式
/*** Mybatis的接口中可以添加注解,完成特定的操作* 說明:* Mybatis中的直接根據(jù)映射標(biāo)簽后期開發(fā)的.* 功能上與映射文件一致.* @return*/@Select(value="select * from item")//@Insert("")//@Delete("")//@Update("")List<Item> selectAll();?
1.2.5?通用Mapper調(diào)用規(guī)則
?
方法名稱是對應(yīng)的,可以自動的進(jìn)行調(diào)用
?
?
1.3?商品的新增
1.3.1?商品分類的級數(shù)
說明:一般的電商網(wǎng)站的商品分類一般都是3級.經(jīng)過了科學(xué)的考證的
?
1.3.2?構(gòu)建ItemCat對象
?
1.3.3?構(gòu)建ItemCatMapper
?
1.3.4?定義ItemCatService
@Service public class ItemCatServiceImpl implements ItemCatService {@Autowiredprivate ItemCatMapper itemCatMapper;/*** 使用通用Mapper(JPA),傳入的對象最終充當(dāng)了查詢的where條件* select * from tb_item_cat where id = 100 and status = 1* * 總結(jié):ItemCat對象會將不為Null的屬性充當(dāng)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?商品分類列表的實(shí)現(xiàn)
1.4.1?頁面的Url分析
?
?
?
1.4.2?分析樹形結(jié)構(gòu)
?
{"id":2,"text":"商品名",state:"closed"}
注:state的屬性如果是closed,表示這個是父節(jié)點(diǎn),它還有子節(jié)點(diǎn)。open代表子節(jié)點(diǎn)
?
1.4.3??擴(kuò)展節(jié)點(diǎn)
?
?
1.4.4?編輯Pojo對象
說明:根據(jù)格式要求編輯get方法:
?
1.4.5?編輯Controller
/*** 1.@ResponseBody * 作用:* 如果返回的數(shù)據(jù)時對象則自動的轉(zhuǎn)化為JSON{key:value}* 如果返回的數(shù)據(jù)為String 則按照字符串原樣返回 String* 注意:轉(zhuǎn)化JSON數(shù)據(jù)時,調(diào)用的是對象中的getXXX()方法* @return*///商品分類實(shí)現(xiàn)@RequestMapping("/list")@ResponseBodypublic List<ItemCat> findItemCat(@RequestParam(value="id",defaultValue="0") Long parentId){//Long parentId = 0L; //定義一級菜單的父級//根據(jù)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?效果展現(xiàn)
?
?
1.5?商品的新增
1.5.1?分析頁面url
?
?
1.5.2?編輯pojo對象
說明:將pojo對象與數(shù)據(jù)庫表一一對應(yīng)
?
1.5.3?編輯Controller
?
1.5.4?編輯Service
?
1.5.5?效果展現(xiàn)
?
1.5.6?EasyUI的校驗(yàn)
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?動態(tài)更新操作(知識回顧)
<!--測試的動態(tài)更新 set作用:1.動態(tài)更新時使用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層通過循環(huán)遍歷的形式實(shí)現(xiàn)操作* 方案2:* 通過Mybatis實(shí)現(xiàn)一次批量修改數(shù)據(jù)的操作*/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
<!--批量修改狀態(tài) collection 的取值有如下的幾種1.如果傳遞的數(shù)據(jù)是數(shù)組 array2.如果傳遞的數(shù)據(jù)是List集合 list3.如果傳遞的數(shù)據(jù)是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?補(bǔ)充知識
2.1?快捷配置
說明:能夠在new中出現(xiàn)class?interface等java的工具類
?
?
2.1.1?jQuery Validate
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/xiangyuqi/p/8571530.html
總結(jié)
以上是生活随笔為你收集整理的通用mapper和分类实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Callable、Future、Futu
- 下一篇: USACO-Section1.6 Num