概念
本项目中,服务器使用Spring+SpringMVC+Mybatis框架作为运行的环境,前端使用html+css+javaScript+jquery+ajax,数据库使用mysql数据库,开发软件使用idea进行搭建项目。
环境搭建
按照项目的功能要求,创建数据库carsys
新建数据库表,字段如下:
?
在数据库表中添加模拟测试数据:
?打开idea开发软件,配置SSM框架环境:
web-》WEB-INF-》lib中添加jar文件
?
将SSM框架的配置文件导入resources文件夹:
?
springmvc.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--开启组件扫描 -->
<context:component-scan
base-package="com.car.controller" />
<!--开启mvc注解支持 -->
<mvc:annotation-driven />
<!--释放静态资源 -->
<mvc:default-servlet-handler />
<!--视图解析器 -->
<!--<bean id="internalResourceViewResolver"-->
<!--class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
<!--<property name="prefix" value="/WEB-INF/jsp/" />-->
<!--<property name="suffix" value=".jsp" />-->
<!--</bean>-->
<!-- 设置文件上传下载的配置信息 -->
<!-- <bean id ="multipartResolver" -->
<!-- class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> -->
<!--默认编码 -->
<!-- <property name ="defaultEncoding" value="utf-8" /> -->
<!--文件大小最大值
<property name = "maxUploadSize" value="10485760000" />-->
<!--内存中的最大值
<property name = "maxInMemorySize" value="40960" />-->
<!--</bean>-->
</beans>
在这里需要配置控制层的完整包名:
?
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"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开启组件扫描 -->
<context:component-scan
base-package="com.car.service" />
<!-- 引入外面的properties常量配置文件 -->
<context:property-placeholder
location="classpath:db.properties" />
<!-- 数据源配置 -->
<bean id="dataSource"
class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 开启基于注解配置的事务管理 -->
<tx:annotation-driven
transaction-manager="transactionManager" />
<!-- 扫描mapper接口文件 -->
<bean id="mapperScannerConfigurer"
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.car.dao" />
</bean>
<!-- 创建sqlSession工厂 -->
<bean id="sessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- <property name="typeAliasesPackage" value="com.yhh.domain" /> -->
<!-- 如果还有一些专门针对于mybatis的配置,需要引入 -->
<property name="configLocation"
value="classpath:mybatis-config.xml" />
<!-- 配置mybatis分页插件PageHelper-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value></value>
</property>
</bean>
</array>
</property>
</bean>
</beans>
?在这里需要配置业务逻辑层包名以及数据访问层包名:
?
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 设置延迟加载开关,默认false(立即加载) -->
<setting name="lazyLoadingEnabled" value="false" />
<!-- 开启MyBatis二级缓存配置,默认已经开启,可以省略 -->
<setting name="cacheEnabled" value="true" />
<!-- 设置驼峰命名规则,会将表字段名user_name自动映射到属性名userName -->
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
<!-- <plugins>
<plugin interceptor ="com.github.pagehelper.PageInterceptor ">
<property name = "properties" value="mysql" />
</plugin>
</plugins> -->
</configuration>
?db.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/carsys
jdbc.username=root
jdbc.password=admin
?
?
在web.xml文件中配置参数数据:
?
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- 监听并加载spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置DispatcherServlet需要的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--加载时机-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!-- 配置/意味着拦截除了jsp之外的所有请求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置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>*.do</url-pattern>
</filter-mapping>
</web-app>
?在src文件夹中完成mvc三层架构的搭建:
将jquery插件导入web-》js文件夹中
?
新建index.html页面,然后部署tomcat运行环境,则开发环境搭建完成。
读者如果不会搭建tomcat运行环境,请查阅其他文章
【Java】Idea软件配置tomcat以及创建web项目步骤_笔触狂放的博客-CSDN博客_ideatomcat配置web项目
|