SSM框架相信有一定开发经验的应该都很熟悉了,最重要的就在于XML文件的配置,本篇文章用来记录配置文件的一些细节,也方便自己以后随时查看。
一、导入相关jar包
二、spring核心配置文件的配置
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder
location="classpath:jdbc.properties" />
<bean id="dataSource"
class=" org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!--还没有配置完,后面需要在在这片空白的地方配置mybatis的配置 -->
<!-- 开启注解 -->
<context:annotation-config />
<!-- 对实现注解类文件所属的包进行扫描 -->
<context:component-scan
base-package="com.hgh"></context:component-scan>
</bean>
头部引入我们需要用到的命名空间,之后context:property-placeholder指定了我们配置的properties文件的位置所在
这里我们先利用dbcp配置了一个datasource的bean,用于后面mybatis配置sessionFactory注入。
下面是properties文件的配置。
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url =jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8&useSSL=true
jdbc.username=root
jdbc.password=6497195
因为导入的是较新的MySQL驱动,所以URL和driver都是新的配置形式,我的数据库名叫SSM
下面就要配置mybatis了,把mybatis的SqlSessionFactory对象交给spring进行管理。
!!!注意:SqlSessionFactory是单例的。一旦被创建就应该在应用的运行期间一直存在,没有任何理由对它进行清除或重建。而spring容器中bean对象也默认是单例创建对象。(SqlSession对象是多例的,每一个线程都应该有一个SqlSession对象)
三、mybatis的配置
在spring核心配置文件applicationContext.xml文件的空白处。写入如下内容,来让spring整合持久层的mybatis框架
<!-- 配置sqlSessionFactory对象 -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载SQL映射文件 -->
<!-- <property name="mapperLocations" value="classpath:com/hgh/mybatis/mapper/CustomerMapper.xml"
/> -->
<!-- 引入插件 -->
<property name="plugins">
<array><!-- 引入分页插件 -->
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
helperDialect=mysql
</value>
</property>
</bean>
</array>
</property>
</bean>
这样我们就配置好了sqlsessionfactory并且也配置好了数据源,这里面注释了一个mapperLocations的配置,这个属性是用来指定mapper接口文件的位置,当然,我们不用这种方式,下面告诉大家一种包扫描的方式,比这个简单方便多了。在后面紧接着写上:
<bean id=""
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hgh.mybatis.mapper" />
</bean>
查看org.mybatis.spring.SqlSessionFactoryBean里面会发现有很多成员变量,这里我们只配置了一个分页插件plugins,这些在配置文件里面都是作为属性<property name=""></property>来配置的。如果用得到就可以去配置。
此外我们还要加上对于事务的管理,直接XML配置文件里配置,配合后面注解加上@Transactional
<!-- 开启对事务的注解 -->
<tx:annotation-driven transaction-manager="transactionManager" />
四、spring-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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.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">
<context:component-scan
base-package="com.hgh.mybatis.controller"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:resources location="/images/" mapping="/images/**" />
<mvc:resources location="/easyui/" mapping="/easyui/**" />
<mvc:resources location="/css/" mapping="/css/**" />
</bean>
告知spring容器创建时扫描的包,并且开启包中的注解,配置viewResolver,注意视图解析器名字只能叫这个!因为后面在web.xml中会配置过滤器,但不希望拦截图片样式和JS静态资源,那就在springMVC.xml文件中设置静态资源不拦截。如果被拦截这些静态资源在网页中是无法被加载的。
下面我们就需要配置web容器在启动时读取的web.xml文件。
五、web.xml文件配置
web容器启动时,加载过程顺序如下:
1、启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点。
2、紧急着,容创建一个ServletContext(servlet上下文),这个web项目的所有部分都将共享这个上下文。
3、容器将<context-param>转换为键值对,并交给servletContext。
4、容器创建<listener>中的类实例,创建监听器。
web.xml文件配制如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>MybatisWeb</display-name>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.xml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.swf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.zip</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<!-- 配置启动SpringMVC -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 启动spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spring容器核心配置文件的路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
其中的内容可以概括为:
(1)配置springmvc的前端控制器dispatcherServlet,并且设置启动顺序为第1个,还有告知配置文件的路径
(2)配置spring核心容器,启动web容器就创建,以及告知核心配置文件的路径
(3)配置一个编码过滤器
这样我们的架子基本就搭好了,那么还有一点就是,我们的静态资源是放在webContent路径下,而不是web-inf路径下。
而我们的JSP页面放在web-inf下,因为我们前面配置视图解析器的时候前缀也已经指定了是在WEB-INF下
OK了,打完收工!