您的位置:首页 > 编程语言

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

2017-11-04 14:12 645 查看

以下是第一种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>


项目目录的格式:

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