零零商城之SSM框架最全整合

整合SSM框架

框架:Spring + SpringMVC + Mybatis
前端:EasyUI
数据库:mysql

整合思路

零零商城之SSM框架最全整合_第1张图片
整合思路.png

整合Dao层

创建SqlMapConfig.xml配置文件






Spring整合mybatis,创建applicationContext-dao.xml



    
    
    
    
    
        
        
        
        
        
        
    
    
    
        
        
        
        
    
    
        
    

Service整合

管理Service实现类



        


事务管理,创建applicationContext-trans.xml


    
    
        
        
    
    
    
        
            
            
            
            
            
            
            
            
            
            
        
    
    
    
        
    

表现层整合

springmvc.xml




    
    
    
        
        
    

    
    

web.xml



    llmall-manager-web
    
        login.html
    
    
    
        contextConfigLocation
        classpath:spring/applicationContext*.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    

    
    
        CharacterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            utf-8
        
        
    
    
        CharacterEncodingFilter
        /*
    


    
    
        llmall-manager
        org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:spring/springmvc.xml
        
        1
    
    
        llmall-manager
        /
    


修改llmall-manager-mapper的pom文件


    
        
            
                src/main/java
                
                    **/*.properties
                    **/*.xml
                
                false
            
        
    

测试

ItemServiceImpl.java

@Service
public class ItemServiceImpl implements ItemService{
    //注入mapper代理对象
    @Autowired
    private TbItemMapper itemMapper;

    @Override
    public TbItem geTbItemById(long itemId) {
        // TbItem tbItem = itemMapper.selectByPrimaryKey(itemId);
        TbItemExample example = new TbItemExample();
        // 创建example对象,添加查询条件
        Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(itemId);
        List list = itemMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

ItemController.java

@Controller
public class ItemController {
    @Autowired
    private ItemService itemService;

    /**
     * 根据id来查询商品信息
     */
    @RequestMapping("/item/{itemId}")
    @ResponseBody
    public TbItem getTbItem(@PathVariable Long itemId) {
        return itemService.geTbItemById(itemId);
    }
}

在浏览器输入:http://localhost:8080/item/536563
得到json数据:

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

表示测试成功了!

恭喜你!骚年,成功练成了SSM整合大法!

你可能感兴趣的:(零零商城之SSM框架最全整合)