ssm整合之文档配置

本文详细介绍了如何整合Spring、SpringMVC和MyBatis(SSM)框架,包括添加必要的jar包、配置Spring容器、数据库连接、SQLSessionFactory、mybatis配置文件、SpringMVC配置以及web.xml的设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1添加 jar包

在这里就不细说了

2.配置文件

首先是Spring容器的配置文件

springconfig.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:aop="http://www.springframework.org/schema/aop"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:util="http://www.springframework.org/schema/util"
 xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
 xmlns:cache="http://www.springframework.org/schema/cache"
 xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
  http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
  http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
 
 <!-- 添加配置文件 db.properties -->
 <context:property-placeholder location="classpath:db.properties"/>
 <!-- 配置数据库的连接,在这里用的是c3p0(注意需要添加c3p0 的jar包) -->
 <bean name = "dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <property name="jdbcUrl" value="${url}"></property>
  <property name="user" value="${username}"></property>
  <property name="password" value="${password}"></property>
  <property name="driverClass" value="${driver}"></property>
 </bean>
 <!-- 配置SQLSessionFactory 主要有两个属性(DataSource和configlocation) -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource"></property>
  <!-- 这里是加载mybatis的配置文件的 ,还可用mapperlocations属性 (<property name="mapperLocations" value="classpath:com/xufl/mapper/*.xml">)-->
  <property name="configLocation" value="classpath:mapper/SqlMapConfig.xml"></property>  
 </bean> 
 <!--配置mybatis配置文件 -->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 <!-- 加载mapper文件 -->
  <property name="basePackage" value="com.xufl.mapper"></property>
  <!-- 使用sqlSessionFactory -->
  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
 </bean>

</beans>

SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

<!-- 设置日志输出 -->
 <settings>
  <setting name="logImpl" value="LOG4J"/>
 </settings>

<!-- 设置类简称-->
 <typeAliases>
 <package name="com.xufl.bean"/>
 </typeAliases>
</configuration>

springmvc.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: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-3.2.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.2.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
 

   <!-- 后端控制器 -->
 
   <context:component-scan base-package="com.xufl.control"></context:component-scan>
   <!-- 处理器映射器 -->
   <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
   <!-- 处理器适配器 -->
   <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
  
  
      <!--  视图解析器 -->
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
 
</beans>

最后配置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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>ssm01</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

<!-- 服务器启动时加载-->
 <!-- 配置springmvc前段控制器-->

 <servlet>
   <servlet-name>springMvc</servlet-name>
   <servlet-class>classpath:spring/springmvc.xml</servlet-class>
  </servlet>
  <servlet-mapping>
   <servlet-name>springMvc</servlet-name>
   <url-pattern>*.action</url-pattern>
  </servlet-mapping>

<!-- 配置spring容器到服务器 -->
  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:spring/bean_core.xml</param-value>
  </context-param>
  <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
 
</web-app>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值