SSM框架学习(二)

前端知识

在spring-web中已经配置了前后缀,将html文件放在web-Inf的html下边就可以了,且后缀也配置完毕,字符串return不需要把。html也加入进去

@RequestMapping(value = "/shopopertion")
public String shopOperation(){
	return "shop/shopoperation";
}
等价于在tomcat访问/shopopertion的时候重定向到/webInf/html/shop/shopoperation.html

店铺注册模块

  • Dao层
ShopDao.java
public interface ShopDao{
	int insertShop(Shop shop);
	 int updateShop(Shop shop);
}

对应xml文件

		
		id 是接口中的方法名 
		 useGeneratedKeys 自增主键  
		keyColumn 数据库表中的主键名 
		KeyProperty 代码中对应的主键名
		INSERT INTO
		VALUES(#{类中的变量名})
		
		
		id 接口中的方法名
		parameterType 对应的实体类对象
				
						shop_name=#{shopName},
						...........
				
				where shop_id=#{shopId}
		

  • DTO层
    封装操作结果状态,以及操作结果的返回信息。
ShopStateEnum.java
public enum ShopStateEnum{
	 //这是一个枚举类 封装好结果的状态
	 CHECK(0,"审核中")....
	 private int state;
	 private String stateInfo;
	 //构造器
	 private  ShopStateEnum(int state, String stateInfo){
	 	this.state = state;
	 	this.stateInfo = stateInfo
	 }
	 /**
     * 根据传入的state返回相应的enum值
     */
    public static ShopStateEnum stateOf(int state){
        for(ShopStateEnum stateEnum:values()){
            if(stateEnum.getState() ==  state){
                return stateEnum;
            }
        }
        return null;
    }
    //get方法
     public int getState() {
        return state;
    }

    public String getStateInfo() {
        return stateInfo;
    }
	 
}
ShopExecuyion.java
public class ShopExecution {
    //结果状态
    private int state;
    //状态标识,对state进行解释
    private String stateInfo;
    //店铺数量
    private int count;
    //操作的shop(增删改)
    private Shop shop;
    //shop列表 查询
    private List shopList;

    public ShopExecution(){

    }
    //店铺创建失败的构造器
    public ShopExecution(ShopStateEnum stateEnum){
        this.state=stateEnum.getState();
        this.stateInfo=stateEnum.getStateInfo();
    }
    //店铺创建成功的构造器
    public ShopExecution(ShopStateEnum stateEnum, Shop shop){
        this.state=stateEnum.getState();
        this.stateInfo=stateEnum.getStateInfo();
        this.shop = shop;
    }
    //店铺创建成功的构造器
    public ShopExecution(ShopStateEnum stateEnum, List shopList){
        this.state=stateEnum.getState();
        this.stateInfo=stateEnum.getStateInfo();
        this.shopList = shopList;
    }
  • Service层
public interface ShopService {
//注册店铺,返回值是shopExecution类型,封装好的操作结果和状态
    ShopExecution addShop(Shop shop, File shopImg);
}
ShopServiceImpl.java
@Service //将这个实现类托管在spring中动态代理
public class ShopServiceImpl implements ShopService{
	@Autowired //动态注入Dao对象,Service曾实现的方法是通过Dao对象调用的
	@Override //实现service接口
	@Transactional 将这个实现类声明为事务,一旦出错可以回滚
	public ShopExecution addShop(Shop shop, File shopImg){
		  //空值判断
        if(shop == null){
        	//ShopStateEnum.NULL_SHOP 枚举类直接引用变量
            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(shopImg != null){
                    //存储图片
                    try{
                        addShopImg(shop, shopImg);
                    }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, File shopImg) {
        //获取shop图片目录的相对值路径
        String dest = PathUtil.getShopImagePath(shop.getShopId());
        String shopImgAddr = ImageUtil.generateThumbnail(shopImg,dest);
        shop.setShopImg(shopImgAddr);
    }
}
  • Service测试类
  • BaseTest类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/spring-dao.xml","classpath:spring/spring-service.xml"})
目的就是加入这两个注解,加载进spring的配置
public class ShopServiceTest extends BaseTest{
	@Autowired
	private ShopService shopService;
@Test
 	ShopExecution se = shopService.addShop(shop, shopImg);
}

由于shopImg如果是CommonsMultipartFile的话就无法传入到addshop的参数中去,而这两者之间的转化工作想完成的话必须要都变成InputStream流的形式。

你可能感兴趣的:(SSM框架学习)