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
项目结构如下图:
下载链接 导入jar包,创建generator配置文件
使用java类来执行逆向工程 public class Main { } 把生成的代码拷贝到项目中 配置数据源和mybatis的session工厂 db.properties文件内容,同样是存放在config资源路径下 service: controller: service: controller: views/items/edit.jsp: 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配置
第四步:配置Controller
第五步:通过Mybatis的逆向工程生成JavaBean/Mapper
简单点说,就是通过数据库中的单表,自动生成java代码。
Mybatis官方提供了逆向工程
可以针对单表自动生成mybatis代码(mapper.javamapper.xmlmodel类)
企业开发中,逆向工程是个很常用的工具
点此链接进行下载
在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">
public static void main(String[] args) throws Exception {
List
我是把生成的po类复制到我com.justin.backoffice中的mapper和model中
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
第九步:创建Spring的applicationContext.xml
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">
第十二步:在applicationContext.xml中添加bean的注解装配
第十三步:ItemsService
第十四步:ItemsController
第十五步:事务配置(在applicationContext.xml中)
第十六步:添加一个保存方法测试事务
public void saveOrUpdate(Items items) { itemsMapper.insert(items);
}
@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
public void deleteById(Integer id) { itemsMapper.deleteByPrimaryKey(id);
}
@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 package com.justin.backoffice.web.controller; @Controller /**
* 商品图片的上传
*/
@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/75557.html 相关文章
发表评论0条评论Simon_Zhou男|高级讲师TA的文章阅读更多
阅读需要支付1元查看
|