一、实现目标
点击新增商品,弹出类目选择窗口
二、实现BaseService
(1)在taotao-manager-interface子工程中编写接口
package com.taotao.manager.service;
import java.util.List;
public interface BaseService<T> {
/**
* 根据id查询数据
*
* @param id
* @return
*/
public T queryById(Long id);
/**
* 查询所有数据
*
* @return
*/
public List<T> queryAll();
/**
* 根据条件查询数据条数
*
* @param t
* @return
*/
public Integer queryCountByWhere(T t);
/**
* 根据条件查询数据
*
* @param t
* @return
*/
public List<T> queryListByWhere(T t);
/**
* 分页查询数据
*
* @param page
* @param rows
* @return
*/
public List<T> queryByPage(Integer page, Integer rows);
/**
* 根据条件查询一条数据
*
* @param t
* @return
*/
public T queryOne(T t);
/**
* 新增
*
* @param t
* @return
*/
public void save(T t);
/**
* 新增,忽略空参数
*
* @param t
* @return
*/
public void saveSelective(T t);
/**
* 根据主键更新
*
* @param t
* @return
*/
public void updateById(T t);
/**
* 根据主键更新,忽略空参数
*
* @param t
* @return
*/
public void updateByIdSelective(T t);
/**
* 根据id删除数据
*
* @param id
* @return
*/
public void deleteById(Long id);
/**
* 根据ids批量删除数据
*
* @param ids
* @return
*/
public void deleteByIds(List<Object> ids);
}
(2)在taotao-manager-service子工程中编写实现类
package com.taotao.manager.service.impl;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Date;
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.taotao.manager.pojo.BasePojo;
import com.taotao.manager.service.BaseService;
public class BaseServiceImpl<T extends BasePojo> implements BaseService<T> {
@Autowired
private Mapper<T> mapper;
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];
}
@Override
public T queryById(Long id) {
T t = this.mapper.selectByPrimaryKey(id);
return t;
}
@Override
public List<T> queryAll() {
List<T> list = this.mapper.select(null);
return list;
}
@Override
public Integer queryCountByWhere(T t) {
int count = this.mapper.selectCount(t);
return count;
}
@Override
public List<T> queryListByWhere(T t) {
List<T> list = this.mapper.select(t);
return list;
}
@Override
public T queryOne(T t) {
T result = this.mapper.selectOne(t);
return result;
}
@Override
public void save(T t) {
// 如果调用者没有设置时间,则这里设置,如果设置了时间,则这里不设置了
if (t.getCreated() == null) {
t.setCreated(new Date());
t.setUpdated(t.getCreated());
} else if (t.getUpdated() == null) {
t.setUpdated(t.getCreated());
}
this.mapper.insert(t);
}
@Override
public void updateById(T t) {
// 更新方法直接设置时间
t.setUpdated(new Date());
this.mapper.updateByPrimaryKey(t);
}
@Override
public void updateByIdSelective(T t) {
// 更新方法直接设置时间
t.setUpdated(new Date());
this.mapper.updateByPrimaryKeySelective(t);
}
@Override
public void deleteById(Long id) {
this.mapper.deleteByPrimaryKey(id);
}
@Override
public void deleteByIds(List<Object> ids) {
// 声明条件
Example example = new Example(this.clazz);
example.createCriteria().andIn("id", ids);
this.mapper.deleteByExample(example);
}
@Override
public List<T> queryByPage(Integer page, Integer rows) {
// TODO Auto-generated method stub
PageHelper.startPage(page, rows);
List<T> list = this.mapper.select(null);
return list;
}
@Override
public void saveSelective(T t) {
// TODO Auto-generated method stub
}
}
(3)改造ItemCatService接口,继承BaseService,注释掉之前的方法
package com.taotao.manager.service;
import java.util.List;
import com.taotao.manager.pojo.ItemCat;
public interface ItemCatService extends BaseService<ItemCat>{
//public List<ItemCat> queryItemCatByPage(Integer id,Integer row);
}
(4)改造ItemCatServiceImpl实现类,把方法注释掉,加service注解
package com.taotao.manager.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.taotao.manager.mapper.ItemCatMapper;
import com.taotao.manager.pojo.ItemCat;
import com.taotao.manager.service.ItemCatService;
@Service
public class ItemCatServiceImpl extends BaseServiceImpl<ItemCat> implements ItemCatService {
/*@Autowired
private ItemCatMapper itemCatMapper;
@Override
public List<ItemCat> queryItemCatByPage(Integer page, Integer row) {
// TODO Auto-generated method stub
PageHelper.startPage(page, row);
List<ItemCat> list = this.itemCatMapper.select(null);
return list;
}*/
@Override
public List<ItemCat> queryItemCatByParentId(Long parentId) {
// 设置查询条件
ItemCat param = new ItemCat();
param.setParentId(parentId);
List<ItemCat> list = super.queryListByWhere(param);
return list;
}
}
(5)改造ItemCatController的queryItemCat方法
@RequestMapping("query/{page}")
@ResponseBody
public List<ItemCat> queryItemCat(@PathVariable Integer page,@RequestParam("row") Integer rows){
return this.itemCatService.queryByPage(page,rows);
}
三、实现类目选择功能
(1)分析数据库
看到有parent_id和is_parent判断是一个树形结构
使用parent_id作为条件进行查询,发现有三级类目
(2)在BaseServiceImpl修改更新和新增方法
@Override
public void save(T t) {
// 如果调用者没有设置时间,则这里设置,如果设置了时间,则这里不设置了
if (t.getCreated() == null) {
t.setCreated(new Date());
t.setUpdated(t.getCreated());
} else if (t.getUpdated() == null) {
t.setUpdated(t.getCreated());
}
this.mapper.insert(t);
}
@Override
public void updateById(T t) {
// 更新方法直接设置时间
t.setUpdated(new Date());
this.mapper.updateByPrimaryKey(t);
}
@Override
public void updateByIdSelective(T t) {
// 更新方法直接设置时间
t.setUpdated(new Date());
this.mapper.updateByPrimaryKeySelective(t);
}
(3)在taotao-manager-web的Controller增加ItemCatController的方法
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public List<ItemCat> queryItemCatByParentId(@RequestParam(value = "id",defaultValue = "0")Long parentId){
List<ItemCat> list = this.itemCatService.queryItemCatByParentId(parentId);
return list;
}
(4)在taotao-manager-interface编写接口
package com.taotao.manager.service;
import java.util.List;
import com.taotao.manager.pojo.ItemCat;
public interface ItemCatService extends BaseService<ItemCat>{
//public List<ItemCat> queryItemCatByPage(Integer id,Integer row);
List<ItemCat> queryItemCatByParentId(Long parentId);
}
(5)在taotao-manager-service实现方法
package com.taotao.manager.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.taotao.manager.mapper.ItemCatMapper;
import com.taotao.manager.pojo.ItemCat;
import com.taotao.manager.service.ItemCatService;
@Service
public class ItemCatServiceImpl extends BaseServiceImpl<ItemCat> implements ItemCatService {
/*@Autowired
private ItemCatMapper itemCatMapper;
@Override
public List<ItemCat> queryItemCatByPage(Integer page, Integer row) {
// TODO Auto-generated method stub
PageHelper.startPage(page, row);
List<ItemCat> list = this.itemCatMapper.select(null);
return list;
}*/
@Override
public List<ItemCat> queryItemCatByParentId(Long parentId) {
// 设置查询条件
ItemCat param = new ItemCat();
param.setParentId(parentId);
List<ItemCat> list = super.queryListByWhere(param);
return list;
}
}
(6)改造ItemCat,增加get方法
public String getText() {
return this.name;
}
public String getState() {
return this.isParent?"closed":"open";
}
四、访问
http://localhost:8081/rest/page/index
成功!!!