文章目录
-
- 1. 区域信息的获取
-
- 1. 编写dao层
-
- 1. AreaDao接口
- 2. AreaDao.xml实现类
- 3. AreaDaoTest
- 2. 编写service层
-
- 1. AreaService接口
- 2. AreaServiceImpl实现类
- 3. AreaServiceTest测试类
- 2. 店铺类别信息获取
-
- 1. dao层的获取
-
- 1. ShopCategoryDao接口
- 2. ShopCategoryDao.xml实现类
- 3. ShopCategoryTest测试类
- 2. service层
-
- 1. ShopService接口
- 2. ShopServiceImpl实现类
- 3. ShopCategoryServiceTest测试类
- 3. Controller层
1. 区域信息的获取
1. 编写dao层
1. AreaDao接口
import com.imooc.o2o.entity.Area;
import java.util.List;
/**
* 列出区域列表
*/
public interface AreaDao {
List<Area> queryArea();
2. AreaDao.xml实现类
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.o2o.dao.AreaDao">
<select id="queryArea" resultType="com.imooc.o2o.entity.Area">
select
area_id,
area_name,
priority,
create_time,
last_edit_time
from
tb_area
order by
priority desc
</select>
</mapper>
3. AreaDaoTest
public class AreaDaoTest extends BaseTest {
@Autowired
private AreaDao areaDao;
@Test
public void testQueryArea(){
List<Area> areaList = areaDao.queryArea();
assertEquals(2,areaList.size());
}
}
2. 编写service层
1. AreaService接口
import com.imooc.o2o.entity.Area;
import java.util.List;
public interface AreaService {
List<Area> getAreaList();
}
2. AreaServiceImpl实现类
import com.imooc.o2o.dao.AreaDao;
import com.imooc.o2o.entity.Area;
import com.imooc.o2o.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AreaServiceImpl implements AreaService {
@Autowired
private AreaDao areaDao;
@Override
public List<Area> getAreaList() {
return areaDao.queryArea();
}
}
3. AreaServiceTest测试类
import com.imooc.o2o.BaseTest;
import com.imooc.o2o.entity.Area;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class AreaServiceTest extends BaseTest {
@Autowired
private AreaService areaService;
@Test
public void testGetAreaList(){
List<Area> areaList = areaService.getAreaList();
assertEquals("东苑",areaList.get(1).getAreaName());
}
}
2. 店铺类别信息获取
1. dao层的获取
1. ShopCategoryDao接口
import com.imooc.o2o.entity.ShopCategory;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface ShopCategoryDao {
List<ShopCategory> queryShopCategory(@Param("shopCategoryCondition")ShopCategory shopCategoryCondition);
}
2. ShopCategoryDao.xml实现类
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.o2o.dao.ShopCategoryDao">
<select id="queryShopCategory" resultType="com.imooc.o2o.entity.ShopCategory">
select
shop_category_id,
shop_category_name,
shop_category_img,
priority,
create_time,
last_edit_time,
parent_id
from
tb_shop_category
<where>
<if test="shopCategoryCondition.parent!=null">
and parent_id = #{shopCategoryCondition.parent.shopCategoryId}
</if>
</where>
order by priority desc;
</select>
</mapper>
3. ShopCategoryTest测试类
先在数据库中添加一条数据,parent_id=1
INSERT INTO tb_shop_category (shop_category_id,shop_category_name,shop_category_desc,shop_category_img,priority,create_time,last_edit_time,parent_id)
VALUES (2,'水果店','杭州','shuiguo.jpg',1,NULL,NULL,1);
INSERT INTO tb_shop_category (shop_category_id,shop_category_name,shop_category_desc,shop_category_img,priority,create_time,last_edit_time,parent_id)
VALUES (3,'蔬菜店','杭州','shucai.jpg',1,NULL,NULL,1);
import com.imooc.o2o.BaseTest;
import com.imooc.o2o.entity.ShopCategory;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class ShopCategoryDaoTest extends BaseTest {
@Autowired
private ShopCategoryDao shopCategoryDao;
@Test
public void testQueryShopCategory() {
List<ShopCategory> shopCategoryList = shopCategoryDao.queryShopCategory(new ShopCategory());
assertEquals(3, shopCategoryList.size());
ShopCategory testCategory = new ShopCategory();
ShopCategory parentCategory = new ShopCategory();
parentCategory.setShopCategoryId(1L);
testCategory.setParent(parentCategory);
shopCategoryList = shopCategoryDao.queryShopCategory(testCategory);
assertEquals(2, shopCategoryList.size());
}
}
为了仅选出parent_id不为空的店铺类别,在ShopCategoryDao.xml中插入:
<if test="shopCategoryCondition != null">
and parent_id is not null
</if>
测试:
@Test
public void testQueryShopCategory() {
List<ShopCategory> shopCategoryList = shopCategoryDao.queryShopCategory(new ShopCategory());
assertEquals(2, shopCategoryList.size());
ShopCategory testCategory = new ShopCategory();
ShopCategory parentCategory = new ShopCategory();
parentCategory.setShopCategoryId(1L);
testCategory.setParent(parentCategory);
shopCategoryList = shopCategoryDao.queryShopCategory(testCategory);
assertEquals(2, shopCategoryList.size());
}
2. service层
1. ShopService接口
import com.imooc.o2o.dto.ShopExecution;
import com.imooc.o2o.entity.Shop;
import java.io.InputStream;
public interface ShopService {
ShopExecution addShop(Shop shop, InputStream shopImgInputStream,String fileName);
}
2. ShopServiceImpl实现类
import com.imooc.o2o.dao.ShopDao;
import com.imooc.o2o.dto.ShopExecution;
import com.imooc.o2o.entity.Shop;
import com.imooc.o2o.enums.ShopStateEnum;
import com.imooc.o2o.exceptions.ShopOperationException;
import com.imooc.o2o.service.ShopService;
import com.imooc.o2o.util.ImageUtil;
import com.imooc.o2o.util.PathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.InputStream;
import java.util.Date;
@Service
public class ShopServiceImpl implements ShopService {
@Autowired
private ShopDao shopDao;
/**
* @param shop 创建的店铺
* @param shopImgInputStream 图片文件流
* @param fileName 图片文件
* @return 返回状态信息结果
* @throws ShopOperationException
* 后面两个传入参数都是与图片处理有关,用于向店铺中添加shopImg信息
*/
@Transactional//事务,方法需要事务支持
public ShopExecution addShop(Shop shop, InputStream shopImgInputStream,String fileName) throws ShopOperationException {
//空值判断
if (shop == null) {
return new ShopExecution(ShopStateEnum.NULL_SHOP);
}
try {
//给店铺信息赋初始值
shop.setEnableStatus(0);//审核中,未上架
shop.setCreateTime(new Date());
shop.setLastEditTime(new Date());
int effectedNum = shopDao.insertShop(shop);//添加店铺信息(将上面赋初值的信息插入到数据库表中)
if (effectedNum <= 0) {
throw new ShopOperationException("店铺创建失败");//事务终止并回滚
} else {
if (shopImgInputStream != null) { //判断传入的文件流是否为空,如果不为空就将图片存储到对应的目录中
try {
//根据shopId获取图片存储的相对路径
//根据相对路径给图片添加水印,并且存储在绝对路径中
addShopImg(shop,shopImgInputStream,fileName);//存储图片
} catch (Exception e) {
throw new ShopOperationException("addShopImg error" + e.getMessage());
}
//添加店铺的时候数据库中并没有添加店铺的图片地址,因此需要更新店铺信息
effectedNum = shopDao.updateShop(shop);//更新店铺的图片地址
if (effectedNum <= 0) {
throw new ShopOperationException("更新图片地址失败");
}
}
}
} catch (Exception e) {
throw new ShopOperationException("addShop error:" + e.getMessage());
}
return new ShopExecution(ShopStateEnum.CHECK, shop);
}
private void addShopImg(Shop shop, InputStream shopImgInputStream,String fileName) {
//根据shopId获取店铺图片的相对路径
String dest = PathUtil.getShopImagePath(shop.getShopId());
//给图片添加水印并将图片存储在绝对值路径中,返回图片的相对值路径
String shopImgAddr = ImageUtil.generateThumbnail(shopImgInputStream,fileName, dest);
shop.setShopImg(shopImgAddr);
}
}
3. ShopCategoryServiceTest测试类
import com.imooc.o2o.BaseTest;
import com.imooc.o2o.entity.ShopCategory;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
public class ShopCategoryServiceTest extends BaseTest {
@Autowired
private ShopCategoryService shopCategoryService;
@Test
public void testShopCategoryService() {
ShopCategory shopCategory = new ShopCategory();
List<ShopCategory> shopCategories = shopCategoryService.getShopCategoryList(shopCategory);
Assert.assertEquals(2, shopCategories.size());
}
}
3. Controller层
@Controller
@RequestMapping("/shopadmin")
public class ShopManagementController {
@Autowired
private ShopCategoryService shopCategoryService;
@Autowired
private AreaService areaService;
@RequestMapping(value = "/getshopinitinfo", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> getshopinitinfo() {
Map<String, Object> modelMap = new HashMap<String, Object>();
List<ShopCategory> shopCategoryList = null;
List<Area> areaList = null;
try {
shopCategoryList = shopCategoryService.getShopCategoryList(new ShopCategory());
areaList = areaService.getAreaList();
// 返回success shopCategoryList areaList,前端通过 data.success来判断从而展示shopCategoryList和areaList的数据
modelMap.put("success", true);
modelMap.put("shopCategoryList", shopCategoryList);
modelMap.put("areaList", areaList);
} catch (Exception e) {
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
}
return modelMap;
}
}