baseService
baseService层代码
public interface BaseService<T> { /** * 跟据id查询数据 * @param id * @return */ T queryById(Long id); /** * 查询所有数据 * @return */ List<T> queryAll(); /** * 跟据查询条件查询记录数 * @param t * @return */ Integer queryCountByWhere(T t); /** * 跟据查询条件查询数据列表 * @param t * @return */ List<T> queryListByWhere(T t); /** * 分页查询数据 * @param page * @param rows * @return */ List<T> queryByPage(Integer page,Integer rows); /** * 跟据查询条件查询一个数据 * @param t * @return */ T queryOne(T t); /** * 保存数据 * @param t */ void save(T t); /** * 保存数据,忽略空字段 * @param t */ void saveSelective(T t); /** * 更新数据 * @param t */ void updateById(T t); /** * 更新数据,忽略空字段 * @param t */ void updateByIdSelective(T t); /** * 跟据id删除数据 * @param id */ void deleteById(Long id); /** * 跟据id列表删除数据 * @param ids */ void deleteByIds(List<Object> ids); }
itemService 继承baseService
public interface ItemService extends BaseService<Item> { /** * 保存商品与描述 * @param item * @param desc */ void saveItemDesc(Item item, String desc); /** * 分页查询商品信息 * @param page * @param rows * @return */ TaoResult<Item> queryItemByPage(Integer page, Integer rows); }
BaseServiceIml
public class BaseServiceImpl<T extends BasePojo> implements BaseService<T> { private Class<T> clazz; public BaseServiceImpl() { // 获取父类的type Type type = this.getClass().getGenericSuperclass(); // 强转为ParameterizedType,可以使用获取泛型类型的方法 ParameterizedType pType = (ParameterizedType) type; // 获取泛型的class this.clazz = (Class<T>) pType.getActualTypeArguments()[0]; } @Autowired private Mapper<T> mapper; @Override public T queryById(Long id) { return mapper.selectByPrimaryKey(id); } @Override public List<T> queryAll() { return mapper.select(null); } @Override public Integer queryCountByWhere(T t) { return mapper.selectCount(t); } @Override public List<T> queryListByWhere(T t) { return mapper.select(t); } @Override public List<T> queryByPage(Integer page, Integer rows) { PageHelper.startPage(page, rows); List<T> list = mapper.select(null); return list; } @Override public T queryOne(T t) { return mapper.selectOne(t); } @Override public void save(T t) { if (t.getCreated() == null) { t.setCreated(new Date()); t.setUpdated(t.getCreated()); } mapper.insert(t); } @Override public void saveSelective(T t) { if (t.getCreated() == null) { t.setCreated(new Date()); t.setUpdated(t.getCreated()); } mapper.insertSelective(t); } @Override public void updateById(T t) { if (t.getUpdated() == null) { t.setUpdated(new Date()); } mapper.updateByPrimaryKey(t); } @Override public void updateByIdSelective(T t) { if (t.getUpdated() == null) { t.setUpdated(new Date()); } mapper.updateByPrimaryKeySelective(t); } @Override public void deleteById(Long id) { mapper.deleteByPrimaryKey(id); } @Override public void deleteByIds(List<Object> ids) { //组装id列表条件 Example example = new Example(clazz); Example.Criteria criteria = example.createCriteria(); criteria.andIn("id", ids); mapper.deleteByExample(example); } }
itemServiceImpl
@Service public class ItemServiceImpl extends BaseServiceImpl<Item> implements ItemService { @Autowired private ItemDescService descService; @Override public void saveItemDesc(Item item, String desc) { //保存商品信息 item.setStatus(1); super.saveSelective(item); //保存商品描述 ItemDesc itemDesc = new ItemDesc(); itemDesc.setItemId(item.getId()); itemDesc.setItemDesc(desc); descService.saveSelective(itemDesc); //发送mq this.sendMq(item.getId(),"save"); } @Autowired private JmsTemplate jmsTemplate; @Autowired private Destination destination; private final ObjectMapper mapper = new ObjectMapper(); /** * 发送消息 * @param itemId 商品id * @param type 操作的业务类型:save,update,delete */ private void sendMq(final Long itemId, final String type){ jmsTemplate.send(destination, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { //构造消息内容 Map<String, Object> map = new HashMap<>(); map.put("itemId", itemId); map.put("type", type); //创建消息对象 TextMessage msg = new ActiveMQTextMessage(); try { String json = mapper.writeValueAsString(map); //设置消息返回 msg.setText(json); } catch (JsonProcessingException e) { e.printStackTrace(); } return msg; } }); } @Override public TaoResult<Item> queryItemByPage(Integer page, Integer rows) { TaoResult<Item> result = new TaoResult<>(); //设置分页 PageHelper.startPage(page, rows); //开始查询数据 List<Item> items = super.queryListByWhere(null); //设置数据列表返回 result.setRows(items); //设置total返回 PageInfo<Item> info = new PageInfo<Item>(items); result.setTotal(info.getTotal()); return result; } }