一:使用spring mvc搭建一个网站,需要先初始化webapp(指定IOC容器上下文配置所在路径,IOC容器监听器,请求拦截规则,文件上传路径,调度器)这些都在web.xml里配置
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_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name> <!-- 指定IOC容器配置文件路径 -->
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
<!-- 指定IOC容器监听器,它会在web工程完成初始化之前,完成IOC容器初始化,在web工程关闭前销毁IOC容器-->
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<!-- http请求调度器,它拦截符合拦截规则的请求路径,并根据请求路径相应的控制器相应请求-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定DispatcherServlet初始化时机,2表示服务器启动时初始化-->
<load-on-startup>2 </load-on-startup>
<multipart-config>
<location>d:/mvc/uploads/</location> <!--指定文件上传路径-->
<max-file-size>5242880</max-file-size> <!--最大单个文件大小-->
<max-request-size>10485760</max-request-size> <!--最大总上传数据大小-->
<file-size-threshold>0</file-size-threshold> <!--文件大小阈值,当大于这个阈值时将写入到磁盘,否则在内存中。默认值为0-->
</multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern> <!--拦截以'/'结束请求-->
</servlet-mapping>
</web-app>
二:有了上面的配置,就需要去配置IOC容器,即数据库连接池、管理数据库连接工厂、事务等,这些对象都会被装载到容器里,需要的时候去容器调用即可。配置内容见application.xml
application.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:p = "http://www.springframework.org/schema/p"
xmlns:tx = "http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 使用注解驱动-->
<context:annotation-config/>
<!-- 数据库连接池-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
<property name="maxActive" value="255"/>
<property name="maxIdle" value="5"/>
<property name="maxWait" value="100000"/>
</bean>
<!-- 整合mybatis,管理数据库连接-->
<bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:/mybatis/mybatis-base-config.xml"/>
</bean>
<!-- 开启数据库事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- redis 连接池-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="50"></property>
<property name="maxTotal" value="100"></property>
<property name="maxWaitMillis" value="20000"></property>
</bean>
<!-- redis存的是字符转,在redis存java对象需要先把对象序列化(对象需要实现Serializable接口),这里指定采用哪个序列化器-->
<bean id ="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<!-- 配置redis-->
<bean id ="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="localhost"></property>
<property name="port" value="6379"></property>
<property name="poolConfig" ref="poolConfig"></property>
</bean>
<!-- 配置RedisTemplate,与redis存取数据操作都需要调用该对象-->
<bean id = "redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="valueSerializer" ref="stringRedisSerializer"/>
</bean>
<!-- 映射器,根据接口生成相应的代理实现类,这样只能一个一个生成某个接口的实现类-->
<bean id="empoy" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="mapper.EmployeeMapper"/>
<property name="sqlSessionFactory" ref="SqlSessionFactory"/>
</bean>
<!-- 映射器,根据接口生成相应的代理实现类,批量生成某个包下所有接口的实现类-->
<!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">-->
<!-- <property name="basePackage" value="mapper"/>-->
<!-- <property name="sqlSessionFactoryBeanName" value="SqlSessionFactory"/>-->
<!-- <property name="annotationClass" value="org.springframework.stereotype.Repository"/>-->
<!-- </bean>-->
</beans>
三:当有http请求时,webapp 是怎么决定由哪个控制器响应请求的呢?这些都是由DispatcherSevlet根据请求路径决定的,DispatcherSevlet初始化配置如下:
dispatcher-servlet.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:p = "http://www.springframework.org/schema/p"
xmlns:tx = "http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 使用注解驱动-->
<mvc:annotation-driven/>
<!-- 定义扫描的包,spring会根据@Controller等注解,找到控制器等其他内容-->
<context:component-scan base-package="com.webapp"/>
<!-- 初始化视图,它会返回一个视图解析器,该视图解析器会到根据路径:/WEB-INF/+视图名称+".jsp"找到我们编写好的jsp文件,返回给用户-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
</beans>
四:mybatis配置
mybatis-base-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!--可以不写,没有配置则使用默认配置-->
<setting name="lazyLoadingEnabled" value ="true"/>
<setting name="aggressiveLazyLoading" value ="false"/>
</settings>
<mappers>
<!--映射文件的配置,这些xml文件是接口的实现逻辑,spring使用代理根据这些逻辑生成代理类-->
<mapper resource="mapper/EmployeeMapper.xml"/>
</mappers>
</configuration>
注:我是个菜鸡,有理解错误的或者不到位的请大佬指正