当前位置: 首页 > 工具软件 > 勾股CMS > 使用案例 >

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

微生俊捷
2023-12-01

步骤

  • 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
<?xml version="1.0" encoding="UTF-8"?>
<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>cms</display-name>

	<!-- Spring的核心配置文件的位置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<!-- 	核心控制器 -->
	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<!-- SpringMVC的配置文件的位置 -->
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:applicationContext-mvc.xml</param-value>
		</init-param>
		<!--记SpringMVC跟着服务器(tomcat)的启动而启动 -->
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<!--使用杠更加符合咱们的RESTful风格 -->
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<!-- 监听服务器(tomcat)的启动,让Spring也同启动 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 配置相应的过滤器:角色SpringMVC 的POST请求的乱码问题 -->
	<!-- 配置编码方式过滤器,注意一点:要配置在所有过滤器的前面 -->
 <filter>
   <filter-name>CharacterEncodingFilter</filter-name>
   <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
   <init-param>
     <param-name>encoding</param-name>
     <param-value>utf-8</param-value>
   </init-param>
 </filter>
 <filter-mapping>
   <filter-name>CharacterEncodingFilter</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>

</web-app>
  • 2.2 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<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
" >

<!-- 打描包:支持对应的注解,变成bean -->
<context:component-scan base-package="cms.service,cms.dao" />

<!-- 读取jdbc.properties -->
<context:property-placeholder location="classpath:db.properties" />

<!-- 配置dataSource(连接池) -->
<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>

<!-- 
   专门为咱们准备了一个类,用来完成数据库的操作:JdbcTemplate
   JdbcTemplate:jDBC的模板(公共的代码已经完成)
-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
	<property name="dataSource" ref="dataSource" />
</bean>


</beans>
  • 2.3applicationContext-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<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
 " >
	<!--  引入其它的Spring配置文件 -->
 	<!-- <import resource="classpath:applicationContext.xml"/> -->
 
	<!-- 扫描包 -->
	<context:component-scan base-package="cms.controller" />
	<!--支持SpringMVC特有的注解 -->
	<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>2000000000</value>
		</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对象(绝对路径的地址,名称)
 类似资料: