Spring框架-7(搭建SSM)

Spring系列文章

Spring框架-1(基础)
Spring框架-2(IOC上)
Spring框架-3(IOC下)
Spring框架-4(AOP)
Spring框架-5(JDBC模板&Spring事务管理)
Spring框架-6(SpringMvc)
Spring框架-7(搭建SSM)
Spring框架-8(SpringMVC2)

前面学习了spring和SpringMvc,掌握了一些基础知识,那么现在我们来整合一下ssm。使用一个简单的小项目来使用一下。下面是整合ssm的一些基础:


Spring框架-7(搭建SSM)_第1张图片
ssm.png

先附上Demo地址

搭建SSM

写好后的目录结构:


Spring框架-7(搭建SSM)_第2张图片
目录结构.png

这里的所有的配置文件我单独创建了一个config。所有的配置文件doub放在这里,当然我在web.xml中这样是无法访问的。我们需要通过idea吧这个目录设置为resource目录。点击config目录右键,如图:

Spring框架-7(搭建SSM)_第3张图片
配置config目录.png

这样在web.xml中就能访问到我们的配置文件了

创建项目导入jar包

使用IDEA创建一个Web项目。


Spring框架-7(搭建SSM)_第4张图片
创建项目

导入jar包

Spring框架-7(搭建SSM)_第5张图片
jar1.png
Spring框架-7(搭建SSM)_第6张图片
jar2.png

Dao层配置文件

数据库文件,使用mybatis逆向工程生成mapper

数据库表结构
数据库.png
逆向工程生成所用的东西
Spring框架-7(搭建SSM)_第7张图片
如图.png

创建SqlMapConfig.xml

mybatis核心配置文件




    


创建ApplicationContext-dao.xml




    
    
    
    
        
        
        
        
        
        
    

    
    
    
        
        
        
        
    

    
    
        
    



通配符配置的db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://39.108.184.253:3306/zTest
jdbc.username=root
jdbc.password=123456

Service层配置文件

ApplicationContext-trans.xml 事务




    
    
        
        
    

    
    
        
            
            
            
            
            
            
            
        
    

    
    
        
    


ApplicationContext-service.xml @Service注解扫描




    
    

controller层配置文件

SpringMvc.xml




    
    

    
    

    
    
        
        
        
        
        
    




web.xml配置



    
    
        contextConfigLocation
        classpath:ApplicationContext-*.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    

    
    
        springMvc
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:SpringMvc.xml
        
        
        1
    
    
        springMvc
        *.action
    

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

流程

  1. tomcat启动时首先加载web.xml文件
  2. web.xml中加载spring的所有的ApplicationContext开头的配置文件
  3. 在所有的ApplicationContext中再加载所有的db.properties,SqlMapConfig.xml文件
  4. web.xml中加载SpringMvc.xml

加载完成,我们的ssm框架就能跑起来了。

写一个测试

需求

请求http://localhost:8080/list.action查询数据库数据,返回jsp页面显示出来

1.编写jsp

商品列表:

商品名称 商品价格 生产日期 商品描述 操作
${item.name } ${item.price } ${item.detail } 修改

2.编写service

使用mybatis查询数据库

@Service
public class ItemsServiceImpl implements ItemsService {

    @Autowired
    private ItemsMapper itemsMapper;

    @Override
    public List list() throws Exception {
        //如果不需要任何查询条件,直接将example对象new出来即可
        ItemsExample example = new ItemsExample();
        List list = itemsMapper.selectByExampleWithBLOBs(example);
        return list;
    }

}

3.编写controller

springMvc中默认支持的参数类型:也就是说在controller方法中可以加入这些参数如下的Model,也可以不加, 加不加看自己需不需要,都行.HttpServletRequest,HttpServletResponse,HttpSession,Model

@Controller
public class ItemsController {

    @Autowired
    private ItemsService itmesService;
    
    @RequestMapping("/list")
    public String itemsList(Model model) throws Exception {
        List list = itmesService.list();
        //Model模型:模型中放入了返回给页面的数据
        //model底层其实就是用的request域来传递数据,但是对request域进行了扩展.
        model.addAttribute("itemList", list);
        //如果springMvc方法返回一个简单的string字符串,那么springMvc就会认为这个字符串就是页面的名称
        return "itemList";
    }

}

4.测试

配置tomcat访问http://localhost:8080/list.action,注意在前面配置了.action所以必须在后面加上.action。

结果:成功

效果.png

你可能感兴趣的:(Spring框架-7(搭建SSM))