提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
目录
一、创建Maven项目的结构?
src/main/java:存放java功能代码
src/test/java:存放java测试代码
src/main/resources:存放资源文件,配置文件,与src/main/java对应
src/test/resources:存放资源文件,配置文件,用于测试,与src/test/java对应
二、使用步骤
1.技术选型。
spring springmvc msyql 数据访问层:mybatis,mybatis-plus 数据源:alibaba bootstrap,jQuery,jsp,jstl
三.项目配置及其配置顺序
1.配置applicationContext.xml
作用:applicationContext是一个spring容器,装载了bean等内容
在src/main/resources位置,新建applicationContext.xml,作用是配置spring相关的内容,bean 的声明。在web项目中,需要手动创建ApplicationContext容器对象,我们需要让项目启动时自动创建 ApplicationContext对象,则需要将ApplicationContext配置信息放在web.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"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.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">
<!--扫描dao和service层的注解-->
<context:component-scan base-package="com.youli.bms"/>
<!--递归扫描dao成接口,进行动态映射-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.youli.bms.mapper"/><!--扫描com.youli.bms.mapper里的内容把他变成bean交给spring管理 -->
</bean>
<!--引入db.properties文件、配置数据源连接-->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
<!--创建Mybatis的sqlSessionFactory对象-->
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- <property name="typeAliasesPackage" value="com.youli.bms.model"/> -->
<!--分页插件-->
<property name="plugins">
<array>
<bean class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor">
<property name="dbType" value="MYSQL"/>
</bean>
</array>
</property>
</bean>
<!-- 为数据源添加事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 开启事务的注解驱动 -->
<tx:annotation-driven transaction-manager="txManager" />
</beans>
2.配置web.xml文件
作用:加载applicationContext.xml,加载springmvc.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_4_0.xsd"
version="4.0">
<!-- web.xml加载applicationContext.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value><!-- classpath:他相当于src/main/resources就是根目录 -->
</context-param>
<!-- 启动Web容器时,自动装配ApplicationContext的配置信息 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置DispatcherServlet 加载配置文件classpath:springmvc.xml -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<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>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--字符编码过滤器 -->
<filter>
<filter-name>characterEncodingFile</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>characterEncodingFile</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3.配置springmvc.xml文件
配置web相关的内容
<?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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描controller层的注解-->
<context:component-scan base-package="com.youli.bms.controller"/>
<!--注解驱动控制器-->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/><!-- /表示是在webapp(根目录)下的 -->
<property name="suffix" value=".jsp"/>
</bean>
<!--引入静态资源-->
<mvc:resources location="/static/" mapping="/static/**"></mvc:resources>
<!--配置文件上传-->
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--配置文件上传大小限制和文件编码-->
<property name="maxUploadSize" value="5000000"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<!-- 注册格式化转换器 用于日期转换-->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"></bean>
<!-- 配置消息属性文件 -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<!-- 指定错误消息的属性文件 -->
<property name="basenames">
<list>
<value>classpath:errorMessages</value>
</list>
</property>
<!-- 资源文件编码格式 -->
<property name="fileEncodings" value="utf-8" />
<!-- 对资源文件内容缓存时间,单位秒 -->
<property name="cacheSeconds" value="120" />
</bean>
<!-- 注册校验器 -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<!-- hibernate校验器-->
<property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
<!-- 指定校验使用的资源文件,在文件中配置校验错误信息,如果不指定则默认使用classpath下的ValidationMessages.properties -->
<property name="validationMessageSource" ref="messageSource" />
</bean>
<!-- 开启spring的Valid功能 -->
<mvc:annotation-driven conversion-service="conversionService" validator="validator"/>
</beans>
4.配置数据库加载文件
这部分内容可以单独抽出来也可以直接配置到applicationContext.xml里面,但是即使是单独抽出来也要在applicationContext里配置这个文件的地址
注意:这里的文件只是一个个的常量值,他会在applicationContext中配置数据源的时候用到。
<!-- 配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
db.propertis文件配置数据库连接信息
url=jdbc:mysql://127.0.0.1:3306/minilibrary?relaxAutoCommit=true&zeroDateTimeBehavior=convertToNull
driver=com.mysql.jdbc.Driver
user=bookadmin
password=123456
四:总结
首次配置web.xml,让他去加载springmvc.xml,applicationContext.xml文件,applicationContext.xml,加载了db.properties(数据库的)和mybatis-config.xml(mybatis的)