首页 > 技术文章 > 004商城项目:ssm框架的整合之后的调试

shenxiaoquan 2016-12-28 13:12 原文

我们来做一个测试应用,去数据库中输入item表的id然后找到里面的信息转换成json显示在页面上。

item表如下:

 

 

 效果:

代码如下:

Dao层:

逆向工程自己的的Mapper。

Service层:

 

package com.taotao.service.impl;

import java.util.List;

import org.omg.CORBA.PUBLIC_MEMBER;
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
public class ItemServiceImpl implements ItemService{
@Autowired
    private TbItemMapper itemMapper;

public TbItem getItemById(long itemId) {
    //TbItem item = itemMapper.selectByPrimaryKey(itemId);
            //添加查询条件
            TbItemExample example = new TbItemExample();
            Criteria criteria = example.createCriteria();
            criteria.andIdEqualTo(itemId);
            List<TbItem> list = itemMapper.selectByExample(example);
            if (list != null && list.size() > 0) {
                TbItem item = list.get(0);
                return item;
            }
            return null;

}






}

 

Action层:

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.*;
import com.taotao.service.*;
@Controller
public class ItemController {
    
    @Autowired
    private ItemService itemService;
    
    @RequestMapping("/item/{itemId}")
    @ResponseBody
    public TbItem getItemById(@PathVariable Long itemId) {
        TbItem tbItem = itemService.getItemById(itemId);
        return tbItem;
    }


}

 

我们执行:

taotao-manager单击右键,执行Run As->Maven Build(选择第二个)->输入clean tomcat7:run。执行之后出错:

No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK。

按照网上找了很多的办法都解决不了,最后换了一下jdk的版本,使用了资料里面的jdk。然后就好了。

 

 花了一个晚上的时间去解决这个问题,还是jdk的版本有问题。吸取教训啊。

 

 

 

 

 

还有一个很错误要注意:也是这篇文章的重点:

启动时报错:

 

 这是找不到Mapper映射文件的错误。

我们修改如下:

修改taotao-manager-mapper的pom文件

在pom文件中添加如下内容:

<!-- 如果不添加此节点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>

这样就可以了。

 

 

 

 

 

然后重新clean,部署。

可以正常运行了。

在url地址栏里面输入http://localhost:8080/item/536563

结果如下:

{"id":536563,"title":"new2 - 阿尔卡特 (OT-927) 炭黑 联通3G手机 双卡双待","sellPoint":"清仓!仅北京,武汉仓有货!"
,"price":29900000,"num":99999,"barcode":"","image":"http://image.taotao.com/jd/4ef8861cf6854de9889f3db9b2
4dc371.jpg","cid":560,"status":1,"created":1425821598000,"updated":1428755918000}




注意几个点:就是整个流程
1;控制器的扫描:在springmvc.xml中:
<context:component-scan base-package="com.taotao.controller" />就是扫描控制器把用@Control的类然后装入到springmvc中。
2:Service层的扫描:

<!-- 扫描包加载Service实现类 -->
<context:component-scan base-package="com.taotao.service"></context:component-scan>

把com.taotao.service包及子包都扫描一下。把标有@Service的全部装入到spring容器中。

3:Dao层的扫描:

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.taotao.mapper"></property>
</bean>

把com.taotao.mapper里面的mapper都扫描一遍装入到spring容器中。

推荐阅读