您的位置:首页 > 其它

SSM的基本集成配置文件

2019-02-28 19:20 127 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/user_path/article/details/88042377

SSM集成

ssm【Spring+SpringMVC+MyBatis】是市面上使用最多的框架

集成配置文件

1.准备properties文件【略】
2.配置Spring.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: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"
>

<!--
集成SSM
1.准备properties属性文件
2.dataSource数据源配置
3.spring与mybatis的集成配置【绑定SqlSessionFactory】
4.service的扫描配置
-->
<!--扫描service层-->
<context:component-scan base-package="com.cn.service"/>
<!--引入properties属性文件-->
<context:property-placeholder location="classpath:dp.properties"/>
<!--dataSource数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!--驱动-->
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<!--路径-->
<property name="url" value="${jdbc.url}"/>
<!--数据库用户名-->
<property name="username" value="${jdbc.username}"/>
<!--数据库密码-->
<property name="password" value="${jdbc.password}"/>
</bean>
<!--集成MyBatis的核心对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置数据源-->
<property name="dataSource" ref="dataSource"/>
<!--配置别名-->
<property name="typeAliasesPackage" value="com.cn.domain"/>
<!--配置Mapper映射的扫描-->
<property name="mapperLocations" value="classpath:com/cn/mapper/*.xml"/>
</bean>

<!--一劳永逸的配置Mapper的方案-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描的包的配置-->
<property name="basePackage" value="com.cn.mapper" />
</bean>

<!--配置事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<!--配置事务支持-->
<tx:annotation-driven/>
</beans>

3.配置Spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
">
<!--配置扫描controller层-->
<context:component-scan base-package="com.cn.controller"/>

<!--静态资源放行--支持restful风格-->
<mvc:default-servlet-handler/>

<!--Spring-Mvc注解支持-->
<mvc:annotation-driven/>

<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/system/" />
<property name="suffix" value=".jsp" />
</bean>
<!--上传解析器【略】-->
</beans>

3.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--监听Spring-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!--启动SpringMVC的配置-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--支持restful风格-->
<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>/*</url-pattern>
</filter-mapping>

</web-app>

4.配置后启动服务器常见问题
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
说找不到你的监听器
解决:
启动服务器就行了,遇到类似的问题也能解决

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: