文章目录
一、SSM框架集成
首先引入框架的对应核心库以及依
1.1创建jdbc.properties数据库资源文件
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/ssm
jdbc.username = xxxx
jdbc.password = xxxx
1.2创建并配置Spring核心文件—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:mvc="http://www.springframework.org/schema/mvc"
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/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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 1.加载属性配置文件:如果属性文件中没有前缀,必须加上system-properties-mode="NEVER" -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 2.将连接池对象交给Spring去管理 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 3.将SqlSessionFactory交给Spring去管理 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="cn.itsource.domain,cn.itsource.query"></property>
<property name="mapperLocations" value="classpath:cn/itsource/mapper/*Mapper.xml"></property>
</bean>
<!-- 4.将Mapper接口交给Spring去管理 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定mapper接口的包路径 -->
<property name="basePackage" value="cn.itsource.mapper"></property>
</bean>
<!-- 5.扫描包路径 -->
<context:component-scan base-package="cn.itsource.service"></context:component-scan>
<!-- 测试Bean -->
<bean id="now" class="java.util.Date"></bean>
</beans>
1.3配置SpringMVC核心文件—applicationContext-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:mvc="http://www.springframework.org/schema/mvc"
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/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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 1.静态资源放行 -->
<mvc:default-servlet-handler />
<!-- 2.扫描包路径:上下文组件扫描 ,Spring容器会去扫描cn.itsource这个包及其子包中所有的类, 如果发现类上有类似@Controller这样注解,就会创建该类的对象,并进行管理 -->
<context:component-scan base-package="cn.itsource.controller"></context:component-scan>
<!-- 3.开启Spring对Mvc的支持:能够使用@RequestMapping -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 4.视图解析器:统一处理【webmvc】 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property><!-- 前缀 -->
<property name="suffix" value=".jsp"></property><!-- 后缀 -->
</bean>
</beans>
1.4配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<welcome-file-list>
<welcome-file>/home/index.html</welcome-file>
</welcome-file-list>
<!--1.配置监听器:加载Spring的配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 2.配置前端控制器:加载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:applicationContext-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>
<!--3.配置过滤器:解决post请求的中文参数乱码问题-->
<filter>
<filter-name>EncodingFilter</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>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>