SSM—CRUD
一、环境搭建
1、开发环境
IDE:idea 2022.2
构建工具:maven 3.8.4
MySQL版本:MySQL 8.0.28
MyBatis版本:MyBatis 3.5.10
2、准备工作
2.2.1、创建maven工程
① 打包方式:war
② 引入依赖
<properties><spring.version>5.3.22</spring.version></properties><dependencies><!-- Spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><!-- SpringMVC --><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><!-- MyBatis核心 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.10</version></dependency><!-- MyBatis和Spring的整合包 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.6</version></dependency><!-- 连接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.9</version></dependency><!-- Junit测试 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency><!-- MySQL驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.28</version></dependency><!-- log4j日志 --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><!-- PageHelper分页 --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>5.3.0</version></dependency><!-- 日志 --><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.11</version></dependency><!-- ServletAPI --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.1</version></dependency><!-- JSON --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.13.2.2</version></dependency><!-- 文件上传 --><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.4</version></dependency><!-- Spring和Thymeleaf整合包 --><dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf-spring5</artifactId><version>3.0.14.RELEASE</version></dependency></dependencies>
③ 创建表
二、搭建MyBatis环境
1、创建属性文件jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm_crud?serverTimezone=UTC
jdbc.username=root
jdbc.password=123456
2、创建MyBatis核心配置文件mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration></configuration>
我们将把MyBatis核心配置文件交给spring管理,因此不在此处配置
3、创建日志文件log4j.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPElog4j:configurationSYSTEM"log4j.dtd"><log4j:configurationxmlns:log4j="http://jakarta.apache.org/log4j/"><appendername="STDOUT"class="org.apache.log4j.ConsoleAppender"><paramname="Encoding"value="UTF-8"/><layoutclass="org.apache.log4j.PatternLayout"><paramname="ConversionPattern"value="%-5p %d{MM-dd HH:mm:ss,SSS}%m (%F:%L) \n"/></layout></appender><loggername="java.sql"><levelvalue="debug"/></logger><loggername="org.apache.ibatis"><levelvalue="info"/></logger><root><levelvalue="debug"/><appender-refref="STDOUT"/></root></log4j:configuration>
三、配置Spring配置文件
<!-- 扫描组件(除控制层) --><context:component-scanbase-package="com.jingchao"><context:include-filtertype="annotation"expression="org.springframework.stereotype.Controller"/></context:component-scan><!-- 引入jdbc.properties --><context:property-placeholderlocation="classpath:jdbc.properties"/><!-- 配置数据源 --><beanid="dataSource"class="com.alibaba.druid.pool.DruidDataSource"><propertyname="driverClassName"value="${jdbc.driver}"/><propertyname="url"value="${jdbc.url}"/><propertyname="username"value="${jdbc.username}"/><propertyname="password"value="${jdbc.password}"/></bean><!-- 配置事务管理器 --><beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><propertyname="dataSource"ref="dataSource"/></bean><!-- 开启事务的注解驱动 --><tx:annotation-driventransaction-manager="transactionManager"/><!-- 配置SqlSessionFactoryBean,可以直接在Spring的IOC中获取SqlSessionFactory --><beanclass="org.mybatis.spring.SqlSessionFactoryBean"><!-- 设置MyBatis核心配置文件的路径 --><propertyname="configLocation"value="classpath:mybatis-config.xml"/><!-- 设置数据源 --><propertyname="dataSource"ref="dataSource"/><!-- 设置类型别名所对应的包 --><propertyname="typeAliasesPackage"value="com.jingchao.entity"/><!-- 设置映射文件的位置,只有当映射文件和接口的包名不一致的情况才设置 --><!-- <property name="mapperLocations" value="classpath:mapper/*.xml"/> --><!-- 配置插件 --><!-- <property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor"/>
</array>
</property> --></bean><!-- 配置mapper接口的扫描,可以将指定包下面的所有mapper接口通过SqlSession创建代理实现类对象,并将这些对象交给IOC容器管理 --><beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer"><propertyname="basePackage"value="com.jingchao.mapper"/></bean>
四、配置SpringMVC配置文件
<!-- 扫描控制层组件 --><context:component-scanbase-package="com.jingchao.controller"/><!--配置视图解析器--><beanid="viewResolver"class="org.thymeleaf.spring5.view.ThymeleafViewResolver"><propertyname="order"value="1"/><propertyname="characterEncoding"value="UTF-8"/><propertyname="templateEngine"><beanclass="org.thymeleaf.spring5.SpringTemplateEngine"><propertyname="templateResolver"><beanclass="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"><!-- 视图前缀 --><propertyname="prefix"value="/WEB-INF/templates/"/><!-- 视图后缀 --><propertyname="suffix"value=".html"/><propertyname="templateMode"value="HTML5"/><propertyname="characterEncoding"value="UTF-8"/></bean></property></bean></property></bean><!-- 配置默认的servlet处理静态资源 --><mvc:default-servlet-handler/><!-- 开启mvc的注解驱动 --><mvc:annotation-driven/><!-- 配置视图控制器 --><mvc:view-controllerpath="/"view-name="index"/><!-- 配置文件上传的解析器 --><beanid="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
五、配置web.xml
1、添加web.xml
File ——> Project Structure
2、配置web.xml
<!-- 配置Spring的编码过滤器 --><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><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 配置处理请求方式的过滤器 --><filter><filter-name>HiddenHttpMethodFilter</filter-name><filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class></filter><filter-mapping><filter-name>HiddenHttpMethodFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 配置SpringMVC的前端控制器 --><servlet><servlet-name>DispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 设置SpringMVC配置文件自定义位置和名称 --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param><!-- 将DispatcherServlet的初始化时间提前到服务器启动时 --><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>DispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!-- 配置Spring的监听器,在服务器启动时加载Spring的配置文件 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 设置Spring配置文件自定义的位置和名称 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring.xml</param-value></context-param>
六、开发前端页面
在WEB-INF下新建文件夹templates,然后新建index.html,使用bootstrap快速搭建前端页面,前端页面使用的资源如下:
- bootstrap-3-4-1-dist
- axios.min.js
- vue.js
- jquery-2.2.4.min.js
前端使用vue和bootstrap完成
七、后端实现
项目使用Rest风格展现,分页使用分页插件pagehelper
八、源代码获取
版权归原作者 京茶吉鹿 所有, 如有侵权,请联系我们删除。