您的位置:首页 > 其它

史上最全SSM框架整合(四)-----测试整合后的SSM框架

2017-06-27 17:36 387 查看
测试整合后的SSM框架

  前言:  

        每一个项目、实践,我们去做了,但是到底能不能真正的运行起来,我们自己首先要进行一个测试。
前面我们已经对SSM框架的搭建、整合进行了详细的介绍和具体的实践,接下来,小编将带你进入下一个阶段,对SSM框架的一个测试,看看整合的SSM框架是否可运行:

   测试一个根据商品id查询商品信息,返回json数据:
 



Dao层:

      查询的表 tb_item,根据商品id查询,可以使用逆向工程生成相应的代码。
详情参考生成逆向工程。

Service层:

   接受商品id,调用mapper查询商品信息,返回商品pojo。
   参数:Long itemId
   返回值:TbItem
package com.taotao.service;

import com.taotao.pojo.TbItem;

public interface ItemService {

TbItem getItemById(Long itemId);
}


ItemServiceImpl.java 
package com.taotao.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemExample;
import com.taotao.pojo.TbItemExample.Criteria;
import com.taotao.service.ItemService;

/**
* 商品查询Service
*@author Daniel
*@date: 2017年6月19日上午10:17:09
*/
@Service
public class ItemServiceImpl implements ItemService {

@Autowired//注入mapper
private TbItemMapper itemMapper;
@Override
public TbItem getItemById(Long itemId) {
//方法一,根据主键查询(这个相对简单,而方法二创建条件查询其实更加的复杂)
//TbItem item=itemMapper.selectByPrimaryKey(itemId);

//方法二:创建查询条件2017-6-19 10:51:25
TbItemExample example=new TbItemExample();
Criteria criteria=example.createCriteria();
criteria.andIdEqualTo(itemId);
List<TbItem>list=itemMapper.selectByExample(example);
//判断list中是否为空
TbItem item=null;
if(list !=null&& list.size()>0){
item=list.get(0);
}
return item;
}
}



Controller层:

      接收商品id,调用Service返回一个商品的pojo,直接响应pojo。需要返回json数据,使用@ResponseBody。
   

     注意:使用@ResonseBody时候一定要把Jackson包添加到工程中。

     Url:/item/{itemId}
     响应:TbItem

package com.taotao.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.pojo.TbItem;
import com.taotao.service.ItemService;

/**
* 商品查询 Controller
* @author Daniel
* @date: 2017年6月19日上午11:03:38
*/
@Controller
// 一来先加上注解(有四个基本注解,这里加@Service也是可以的,
// @Component,@Reponsitory, @Controller,@Service)
// 这四个功能都是一样的,随便写哪个都行,但是为了区分,
// 我们在会在service层写@Service,controller层写@Controller,dao层写@Repository
public class ItemController {

@Autowired
private ItemService itemService;

@RequestMapping("/item/{itemId}")
@ResponseBody
/*private TbItem getItemById(@PathVariable Long itemId){
TbItem item=itemService.getItemById(itemId);
return item;*/
private TbItem getItemById(@PathVariable Long itemId) {
TbItem item = itemService.getItemById(itemId);
return item;
}
}


解决mapper映射文件不拷贝问题   

需要添加taotao-manager-dao工程的pom.xml文件。添加代码如下:

<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>



测试结果如下:

      我们找一个调数据,id=830972,输入“http://localhost:8080/item/830972”效果出现数据库表中的内容说明SSM整合成功。



 

 


小结:

        SSM的学习、搭建、整合和测试,通过这些实践,SSM框架的一些列理论、代码、实践就非常的完整的告上了一个阶段。SSM依托Spring、Srpingmvc 和Mybatis进行整合,给攻城狮们带来极其多的便利,希望该文能对您有所帮助。

    SSM搭建整合源码下载:http://pan.baidu.com/s/1i5rCjrf

          
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: