CMS系统项目常规开发流程和简单CRUD实现

步骤

  • 1.设计实体,数据库表和基本实现类
    • 1.1 数据库表 DDL
CREATE TABLE `t_images` (
  `imgid` int(11) NOT NULL AUTO_INCREMENT,
  `storepath` varchar(255) DEFAULT NULL,
  `storename` varchar(255) DEFAULT NULL,
  `intro` varchar(255) DEFAULT NULL,
  `isenabled` bit(1) DEFAULT NULL,
  `inputdate` datetime DEFAULT NULL,
  PRIMARY KEY (`imgid`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

  • 1.2 实体类,加上getter/setter和toString 无参构造(简单JavaBean)
	
	//图片的id(编号)
	private Integer imgid;
	//图片的路径
	private String storepath;
	//图片的名称
	private String storename;
	//图片的介绍(描述)
	private String intro;
	//是否启用 true:启用  false:禁用
	private Boolean isenabled;
	//录入时间
	private Date inputdate = new Date();

	//上传的图片文件(和数据库没有关系,我们只是通过这个字段接收文件)
	private MultipartFile fileImg;
  • 1.3 dao层
public interface IImagesDao {
	
	void save(Images images);
	
	void update(Images images);
	
	void delete(Integer id);
	
	Images findOne(Integer id);
	
	List<Images> findAll();
}

  • 1.4 service 层

public interface IImageService {
	
	void save(Images images);
	
	void update(Images images);
	
	void delete(Integer id);
	
	Images findOne(Integer id);
	
	List<Images> findAll();
}
  • 1.5 controller层
package cms.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import cms.domain.Images;
import cms.service.IImageService;

@Controller
@RequestMapping("/img")
public class ImagesController {
	
	@Autowired
	private IImageService imgservice;
	
	@RequestMapping("/save")
	public String save(Images images,HttpServletRequest req) throws IllegalStateException, IOException{
		//一、解决上传的名称问题
		//1.拿到对应的文件
		MultipartFile fileImg = images.getFileImg();
		//2.拿到文件名称
		String filename = fileImg.getOriginalFilename();
		//3.拿到文件后缀
		String extension = FilenameUtils.getExtension(filename);
		//4.获取随机名称
		String uuid = UUID.randomUUID().toString();
		//5.拼接新的文件名
		String newfilename = uuid+"."+extension;
		
		//二、解决上传的路径
		//1.获取实际的路径
		String realPath = req.getServletContext().getRealPath("/upload");
		//2.创建文件
		File file = new File(realPath,newfilename);
		//3.创建文件夹
		File parentFile = file.getParentFile();
		if (!parentFile.exists()) {//如果不存在父文件夹
			//创建文件夹
			parentFile.mkdirs();
		}
		
		//三、保存文件 transferTo:保存文件的方法
		fileImg.transferTo(file);
		
		//添加数据
		//1.添加名称
		images.setStorename(filename);
		//2.添加地址
		images.setStorepath("/upload/"+newfilename);
		
		imgservice.save(images);
		return "forward:main";
	}
	
	//主页面
	@RequestMapping("/index")
	public String query(){
		return "index";
	}
	
	//数据查询
	@RequestMapping("/main")
	public String  tomain(Model model){
		List<Images> findAll = imgservice.findAll();
		model.addAttribute("imageList",findAll);
		return "main";
	}
	
	@RequestMapping("/update")
	public String update(){
		return "main_add";
	}
	
	@RequestMapping("/delete")
	public String delete(Integer imgid){
		imgservice.delete(imgid);
		return "forward:main";
		
	}
	
	@RequestMapping("/input")
	public String input(){
		
		return "main_add";
	}
}

  • 2.xml配置
    • 2.1 web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<display-name>cmsdisplay-name>

	
	<context-param>
		<param-name>contextConfigLocationparam-name>
		<param-value>classpath:applicationContext.xmlparam-value>
	context-param>
	
	<servlet>
		<servlet-name>dispatcherservlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
		<init-param>
			
			<param-name>contextConfigLocationparam-name>
			<param-value>classpath:applicationContext-mvc.xmlparam-value>
		init-param>
		
		<load-on-startup>1load-on-startup>
	servlet>
	<servlet-mapping>
		<servlet-name>dispatcherservlet-name>
		
		<url-pattern>/url-pattern>
	servlet-mapping>
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
	listener>

	
	
 <filter>
   <filter-name>CharacterEncodingFilterfilter-name>
   <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
   <init-param>
     <param-name>encodingparam-name>
     <param-value>utf-8param-value>
   init-param>
 filter>
 <filter-mapping>
   <filter-name>CharacterEncodingFilterfilter-name>
   <url-pattern>/*url-pattern>
 filter-mapping>

web-app>
  • 2.2 applicationContext.xml

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


<context:component-scan base-package="cms.service,cms.dao" />


<context:property-placeholder location="classpath:db.properties" />


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
   <property name="driverClassName" value="${jdbc.driverClassName}" />
   <property name="url" value="${jdbc.url}" />
   <property name="username" value="${jdbc.username}" />
   <property name="password" value="${jdbc.password}" />
bean>


<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
	<property name="dataSource" ref="dataSource" />
bean>


beans>
  • 2.3applicationContext-mvc.xml

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc.xsd
 " >
	
 	
 
	
	<context:component-scan base-package="cms.controller" />
	
	<mvc:annotation-driven />
	
	<mvc:default-servlet-handler />
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jsp">property>
	bean>
	
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize">
			<value>2000000000value>
		property>
	  bean>

beans>
  • 3.页面(WEN-INF安全目录必须通过controller进入)

  • 4.和前端进行数据交互

    • 4.1 前端给我们前端页面,我们弄到我们的项目中
    • 4.2 iframe:把其他页面放入当前页面,实现页面嵌套
    • 4.3 注意:如果配置有上下文路径:所有跳转路径前加:${pagecontext.request.contextpath}
  • 5.文件上传

    • 5.1配置form表单 :1.form表单需要配置method = post ,enctype = “multipart/form-data”
    • 5.2 上传文件(图片)文件名处理
      • 5.2.1 修改名称
      • 5.2.2 获取路径
      • 5.2.3 保存图片(fileImg.transferTo(file))
      • 5.2.4 保存Images对象(绝对路径的地址,名称)

你可能感兴趣的:(CMS系统项目常规开发流程和简单CRUD实现)