SSM框架整合方式(一):XML配置文件代码

以下是第一种SSM整合的xml配置文件代码

bean.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<!-- 开启注解模式 -->
<context:annotation-config></context:annotation-config>
<!-- 开启包扫描 -->
<context:component-scan base-package="com.itjob.service.impl"></context:component-scan>
<context:component-scan base-package="com.itjob.dao.impl"></context:component-scan>

<!-- 读数据库配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!--配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${usernamex}"></property>
<property name="password" value="${passwordx}"></property>
</bean>

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

<!-- 开启注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

<!-- 配置mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 各实体类取别名    别名是类的简单名称 -->
<property name="typeAliasesPackage" value="com.itjob.entity"></property>
<!-- 找到mapper.xml文件 -->
<property name="mapperLocations" value="classpath:com/itjob/dao/*.xml"></property>
</bean>

<!-- 告诉mybatis  mapper.xml配置文件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itjob.dao"></property>
</bean>
</beans>

jdbc.properties配置文件

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
usernamex=root
passwordx=root

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">
  <display-name>mAndSpringMVC</display-name>
  
  <!-- 配置springMVC 核心转换器 -->
  <servlet>
  <servlet-name>springMVC</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!-- 核心转换器启动时间  tomcat启动1秒后启动-->
  <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 拦截路径 -->
  <servlet-mapping>
  <servlet-name>springMVC</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-mapping>
  
<!-- 配置mybatis -->
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:bean.xml</param-value>
  </context-param>
  <!-- 监听 -->
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <welcome-file-list>
    <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

springMVC-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: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-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:component-scan base-package="com.itjob.controller"></context:component-scan>

<!-- 配置静态资源  spring核心转换器不会拦截 -->
<!-- <mvc:resources location="/" mapping="**/*.js"/> -->
<mvc:default-servlet-handler/>

<!-- 开启注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>

<!-- 配置controller返回jsp的前后缀 -->
<bean id="internalResource" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 配置后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

项目目录的格式:

这里写图片描述

感谢您的阅读,欢迎参观我的个人网站:闲乐小站【www.xianlewang.cn】

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值