资讯专栏INFORMATION COLUMN

ssm框架整合

kk_miles / 433人阅读

ssm整合
开发环境ide,mysql数据库,Spring+SpringMVC+Mybatis,tomcat8.5,jdk使用的是1.7版本。
第一步:导入jar包

Spring+ SpringMVC + MyBatis + Mybatis-spring整合包

AOP联盟+织入 + c3p0 数据库连接池 + MySQL连接驱动 + jstl

链接:https://pan.baidu.com/s/1_tSC...  提取码:dyao

项目结构如下图:

第二步:创建springmvc.xml文件


   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.2.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">












    
    

第三步:在web.xml添加springmvc配置



    DispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
    
        
        contextConfigLocation
        classpath:springmvc.xml
    
    1


    DispatcherServlet
    *.do

第四步:配置Controller

第五步:通过Mybatis的逆向工程生成JavaBean/Mapper
简单点说,就是通过数据库中的单表,自动生成java代码。 
Mybatis官方提供了逆向工程
可以针对单表自动生成mybatis代码(mapper.javamapper.xmlmodel类)
企业开发中,逆向工程是个很常用的工具

下载链接
点此链接进行下载

导入jar包,创建generator配置文件
在classpath下,创建generator.xml文件:(文件内容可以从逆向工程的jar包中docs目录下的index.html中找到相关代码)

    PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">


    
    
    

    
    
        
    

    
    
        
        
    

    
    
        
    

    
    
        
    

    
    

使用java类来执行逆向工程

public class Main {

public static void main(String[] args) throws Exception {
    List warnings = new ArrayList();
    boolean overwrite = true;
    File configFile = new File("src/generator.xml");
    ConfigurationParser cp = new ConfigurationParser(warnings);
    Configuration config = cp.parseConfiguration(configFile);
    DefaultShellCallback callback = new DefaultShellCallback(overwrite);
    MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
            callback, warnings);
    myBatisGenerator.generate(null);
}

}

把生成的代码拷贝到项目中
我是把生成的po类复制到我com.justin.backoffice中的mapper和model中

第六步:修改ItemsMapper.java和ItemsMapper.xml



第七步:定义Service层接口并实现

第八步:配置SqlMappingConfig.xml,我这里给它命名为mybatis.xml,放在config资源路径下


    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">



    
    


    
    

第九步:创建Spring的applicationContext.xml

配置数据源和mybatis的session工厂


   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

第十步:配置c3p0数据源和mybatis的会话工厂





    
    
    
    
    
    




    
    
    




    
    

db.properties文件内容,同样是存放在config资源路径下

第十一步:web.xml中配置spring容器


    contextConfigLocation
    classpath:applicationContext.xml


    org.springframework.web.context.ContextLoaderListener


第十二步:在applicationContext.xml中添加bean的注解装配

   

第十三步:ItemsService

第十四步:ItemsController

第十五步:事务配置(在applicationContext.xml中)


    




第十六步:添加一个保存方法测试事务

service:
public void saveOrUpdate(Items items) {

    itemsMapper.insert(items);
}

controller:
@RequestMapping("save")

public String save(){
    //创建商品
    Items items = new Items();
    items.setName("iphonexs");
    items.setPrice(8000.00f);
    items.setCreatetime(new Date());
    items.setDetail("666真好看");
    //保存数据
    itemsService.saveOrUpdate(items);
    return "items/list";
}  
接下来对items这个表数据进行增删改查
显示商品数据


package com.justin.backoffice.web.controller;

views/items/list.jsp

删除商品

service:
public void deleteById(Integer id) {

    itemsMapper.deleteByPrimaryKey(id);
}

controller:
@RequestMapping("delete")

public String delete(Integer id,Model model){
    itemsService.deleteById(id);
    return "forward:list.do";
}
显示编辑商品页面


controller:
@RequestMapping("edit")

public String edit(Integer id,Model model){
    System.out.println("id:"+id);
    //通过id找到商品
    Items items = itemsService.findById(id);
    if (items!=null){
        model.addAttribute("items",items);
    }
    return "items/edit";
}

views/items/edit.jsp:

名称
价格
描述
图片

views/items/edit.jsp


后台

package com.justin.backoffice.web.controller;

@Controller
@RequestMapping("upload")
public class UploadController {

/**
 * 商品图片的上传
 */
@RequestMapping("itemspic")
public void itemspic(HttpServletRequest request, HttpServletResponse response) throws Exception {
    //System.out.println(itemspic1);

    System.out.println(request);
    MultipartHttpServletRequest  mutliRequest = (MultipartHttpServletRequest) request;
    //1.获取图片数据
    MultipartFile mfile = mutliRequest.getFile("itemspic1");

    //2.把图片保存在某个路径
    //2.1文件保存的文件夹路径
    String uploadFolder = request.getServletContext().getRealPath("/upload");
    System.out.println("uploadFolder:" + uploadFolder);
    File uploadFolderFile = new File(uploadFolder);
    if(!uploadFolderFile.exists()){
        uploadFolderFile.mkdirs();
    }

    //2.2.文件
    String suffix = mfile.getOriginalFilename().split(".")[1];
    String fileName = UUID.randomUUID().toString().replace("-","") + "." + suffix;
    String totalPath = uploadFolder + "" + fileName;
    System.out.println("totalpath:" + totalPath);
    FileCopyUtils.copy(mfile.getInputStream(),new FileOutputStream(new File(totalPath)));

    //返回数据给客户端
    String imgURL = "http://localhost:8080/ssm/upload/" + fileName;
    String responseJson = "{"imgUrl":"" + imgURL  + ""}";
    response.setHeader("content-type","text/json;charset=utf-8");
    response.getWriter().write(responseJson);

}

}
文件上传成功啦,如图:

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/35685.html

相关文章

  • 从零开始搭建SSM框架(Spring + Spring MVC + Mybatis)

    摘要:打开,,选中,然后再选中,输入项目的和,指定等配置,修改,打开项目,添加一些必要的目录,最终项目框架目录图如下修改文件,指定各依赖和插件的版本等信息在标签里面管理各依赖的版本号添加项目依赖管理依赖配置好之后,开始整合。 最近在回顾和总结一些技术,想到了把之前比较火的 SSM 框架重新搭建出来,作为一个小结,同时也希望本文章写出来能对大家有一些帮助和启发,因本人水平有限,难免可能会有一些...

    MiracleWong 评论0 收藏0
  • Maven多模块项目搭建+整合SSM框架

    摘要:继承作用就是避免配置重复,对于子项目来说应该关心父项目是怎么样配置的。聚合字面理解就是聚在一起合作完成工作,就是将子模块聚集起来完成相应的项目需求父工程的搭建项目结构在父工程中,主要负责完成依赖的版本管理,并不是实际的依赖。 从大二开始就一直关注segmentFault,在问题专区帮忙回答一些自己知晓的问题;在写这篇文章之前我一直会在朋友圈发一些自己遇到的问题以及解决办法,这是第一次写...

    liaosilzu2007 评论0 收藏0
  • SSM框架整合

    摘要:整合项目结构导入版本号相关包相关包相关包相关包数据库连接池集成标准标签库日志相关包单元测试相关包里面为空开发环境下,日志级别设置 ssm整合项目结构 showImg(https://segmentfault.com/img/bVbsw8O?w=533&h=815); Maven导入jar pom.xml 4.0.0 cn.scitc Test 1...

    twohappy 评论0 收藏0
  • ssm框架整合

    ssm整合 开发环境ide,mysql数据库,Spring+SpringMVC+Mybatis,tomcat8.5,jdk使用的是1.7版本。 第一步:导入jar包 Spring+ SpringMVC + MyBatis + Mybatis-spring整合包 AOP联盟+织入 + c3p0 数据库连接池 + MySQL连接驱动 + jstl 链接:https://pan.baidu.com/...

    Simon_Zhou 评论0 收藏0
  • ssm框架整合

    ssm整合 开发环境ide,mysql数据库,Spring+SpringMVC+Mybatis,tomcat8.5,jdk使用的是1.7版本。 第一步:导入jar包 Spring+ SpringMVC + MyBatis + Mybatis-spring整合包 AOP联盟+织入 + c3p0 数据库连接池 + MySQL连接驱动 + jstl 链接:https://pan.baidu.com/...

    phpmatt 评论0 收藏0

发表评论

0条评论

kk_miles

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<