taotao-整合SSM框架

一、DAO层:整合Mybatis和Spring

需要的jar包:
1、Mybatis的jar包
2、Mysql数据库驱动
3、数据库连接池
4、Mybatis和spring的整合包
5、Spring的jar包
配置文件:
1、Mybatis的配置文件:SqlMapConfig.xml
2、Spring的配置文件:applicationContext-dao.xml
 2.1、数据源
 2.2、数据库连接池
 2.3、配置SqlSessionFactory(mybatis和spring整合包中的)
 2.4、配置mapper文件的扫描器

配置DAO层应该在哪儿配?最终taotao-manager-dao会打成一个jar包,如果在DAO里面配就读不到配置文件,不管哪一层的配置,都应该把配置放在web下面。向pojo、service最终都会被打成jar包。

SqlMapConfig.xml






db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/taotao?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

applicationContext-dao.xml




    
    
    
    
    
        
        
        
        
        
        
    
    
    
        
        
        
        
        
    
    
        
        
    

二、Service层

需要的jar包:Spring的jar包
配置文件:applicationContext-service.xml
  1. 配置一个包扫描器,扫描所有带@Service注解的类。
事务配置:applicationContext-trans.xml
 1、配置一个事务管理器
 2、配置tx
 3、配置切面

applicationContext-service.xml




    
    
    

applicationContext-trans.xml



    
    
        
        
    
    
    
        
            
            
            
            
            
            
            
            
            
            
        
    
    
    
        
    


三、表现层

需要的jar包:SpringMVC和Spring的jar包
配置文件:springmvc.xml
 1、配置注解驱动
 2、配置一个视图解析器。
 3、包扫描器,@Controller注解

springmvc.xml



    
        
    
    
    
    
    
        
        
    
 

四、Web.xml

1、配置springmvc的前端控制器
2、Spring容器初始化的listener

web.xml



    taotao-manager
    
        index.html
        index.htm
        index.jsp
        default.html
        default.htm
        default.jsp
    
    
    
        contextConfigLocation
        classpath:spring/applicationContext-*.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    

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


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


五、做一个测试

需求:根据商品id查询商品信息,返回json数据

5.1 DAO层

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

将要查询的表.png

5.1 Service层

接收一个商品id(bigint),调用mapper查询商品信息,返回商品的POJO
参数:Long itemId
返回值:TbItem

ItemService

package com.taotao.service;

import com.taotao.pojo.TbItem;

/**
 * Created by yvettee on 2018/7/24.
 */
public interface ItemService {
    TbItem getItemById(Long itemId);
}

ItemServiceImpl

package com.taotao.service.impl;

import com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.service.ItemService;
import org.springframework.stereotype.Service;

/**
 * Created by yvettee on 2018/7/24.
 */
@Service
public class ItemServiceImpl implements ItemService{

    private TbItemMapper tbItemMapper;
    @Override
    public TbItem getItemById(Long itemId) {
        TbItem tbItem = this.tbItemMapper.selectByPrimaryKey(itemId);
        return tbItem;
    }
}

5.1 Controller层

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

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

ItemController

package com.taotao.controller;

import com.taotao.pojo.TbItem;
import com.taotao.service.ItemService;
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;

/**
 * Created by yvettee on 2018/7/24.
 */
@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;
    }
}

访问结果:

结果.png

上篇:使用IDEA创建多模块maven继承、聚合项目——taotao
下篇:taotao-Mybatis分页插件(商品列表展示)
源代码:https://github.com/yvettee36/taotao

你可能感兴趣的:(taotao-整合SSM框架)