ssm框架使用通用Mapper和BaseService

来源:互联网 发布:金融大数据平台架构 编辑:程序博客网 时间:2024/06/15 13:03

由于mybatis的通用mapper配置我们已经在上一篇文章ssm三大框架整合mybatis-config.xml文件中配置好了,因此这里就不在阐述,上一篇文章的地址:

http://www.tpyyes.com/a/javaweb/2016/1103/23.html

在我们写接口的时候可以直接继承Mappe<T>接口,常用的增删该查方法就完全不用写了,只写一些特殊方法就可以了,如下:

package com.taobao.manage.mapper;import com.github.abel533.mapper.Mapper;import com.taobao.manage.pojo.ItemCat;public interface ItemCatMapper extends Mapper<ItemCat> {}

这个时候我们就可以在service上用@Autowired注入我们的dao层了。

package com.taobao.manage.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.taobao.manage.mapper.ItemDescMapper;import com.taobao.manage.pojo.ItemCat;import com.taobao.manage.pojo.ItemDesc;@Servicepublic class ItemCatService extends BaseService<ItemCat>{    @Autowired    private ItemCatMapper itemCatMapper;    /**     * 根据商品id查询商品信息     * @param itemId     * @return     */    public ItemDesc queryItemCatById(Long itemId) {                   return this.itemCatMapper.selectByPrimaryKey(itemId);    }}

下面还有一个非常好得baseService工具类,当我们的service类继承了这个baseService类后,我们就可以直接在controller层注入这个service了,例如:

import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import com.taobao.manage.pojo.ItemCat;import com.taobao.manage.service.ItemCatService;@RequestMapping("item/cat")@Controllerpublic class ItemCatController {    @Autowired    private ItemCatService itemCatService;    // http://localhost:8081/rest/item/cat    @RequestMapping(method = RequestMethod.GET)    @ResponseBody    public ResponseEntity<List<ItemCat>> queryItemListAll(            @RequestParam(value = "id", defaultValue = "0") Long parentId) {        try {            ItemCat itemCat = new ItemCat();            itemCat.setParentId(parentId);            List<ItemCat> list = this.itemCatService.queryListByWhere(itemCat);            //200            return ResponseEntity.ok(list);        } catch (Exception e) {            e.printStackTrace();        }        //500        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);    }}

以下是我们的baseService工具类,为了能实现事务控制,请把baseService类放在我们的service层下:

package com.taobao.manage.service;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import com.github.abel533.entity.Example;import com.github.abel533.mapper.Mapper;import com.github.pagehelper.PageHelper;import com.github.pagehelper.PageInfo;public abstract class BaseService<T> {        @Autowired    private Mapper<T> mapper;           //根据id查询实体    public T queryById(Long id){        return this.mapper.selectByPrimaryKey(id);    }        //查询所有    public List<T> queryAll(){        return this.mapper.select(null);    }        //条件查询    public List<T> queryListByWhere(T param){        return this.mapper.select(param);    }        //查询记录数    public Integer queryCount(T param){        return this.mapper.selectCount(param);    }        //分页    public PageInfo<T> queryPageListByWhere(T param,Integer page,Integer rows){        PageHelper.startPage(page, rows);        List<T> list = this.queryListByWhere(param);        return new PageInfo<T>(list);    }        //查询一条记录    public T queryOne(T param){        return this.mapper.selectOne(param);    }        //插入    public Integer save(T param){        return this.mapper.insert(param);    }        //新增非空字段    public Integer saveSelect(T param){        return this.mapper.insertSelective(param);    }        //根据主键更新    public Integer update(T param){        return this.mapper.updateByPrimaryKey(param);    }        //根据主键更新非空字段    public Integer updateSelective(T param){        return this.mapper.updateByPrimaryKeySelective(param);    }           //根据主键删除    public Integer deleteById(Long id){        return this.mapper.deleteByPrimaryKey(id);    }             //批量删除    public Integer deleteByIds(Class<T> clazz,List<Object> values){        Example example = new Example(clazz);        example.createCriteria().andIn("id", values);        return this.mapper.deleteByExample(example);    }      }

今天就写到这里,以上的ssm三大框架配置是可以直接使用的,非常的全,只需要更改部分代码就可以和你的项目整合了。

下一期将写如何与权限框架shiro整合 

原创粉丝点击