SSM整合(Maven管理)

SSM的整合实质是就是配置文件的配置。我就做了一个连接本地mysql数据库进行按id查询的小Demo。麻雀虽小五脏俱全。希望你能有所帮助,这里采用的是Maven进行管理。这个项目一共抽了5个配置文件,当然在实际的项目开发者,会分的更细。

至于如何在eclipse下创建maven管理的ssm整合项目,网上的资源非常的多

数据库常量配置文件:db.properties

这里的内容就不用细说。就是连接数据库的那一套、

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///mybatis
jdbc.username=root
jdbc.password=root
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.InitialSize=5
jdbc.validationQuery=select 1

Spring的配置文件:applicationContext.xml

这里的配置朱主要包括:加载数据源,配置事物,Mybatis工厂配置(配置数据源,指定mybatis的核心配置文件,指定映射文件),mapper扫描器,扫描service。

着重注意的是mapper扫描器:这里配置的是自动扫描,这样的话在mayatis的配置文件中就不需要为每一个mapper配置映射文件。同时也不需要在spring的配置文件中为每一个mapper配置实例类。大大的简化配置。

着重注意的扫描service:因为该项目是通过注解的方式为service层创建bean,并在service层中引用注解。而dao层(mapper)因为配置的是自动扫描后会在spring容器中自动配置bean。因此不需要扫描dao层。

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	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/aop
     http://www.springframework.org/schema/aop/spring-aop.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 ">
	<!-- 读取配置源 -->
	<context:property-placeholder
		location="classpath:db.properties" />
	<bean id="dataSource"
		class="org.apache.commons.dbcp2.BasicDataSource">
		<!-- 注入属性值 -->
		<property name="driverClassName" value="${jdbc.driver}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="maxIdle" value="${jdbc.maxIdle}"></property>
		<property name="InitialSize" value="${jdbc.InitialSize}"></property>
		<property name="validationQuery" value="${jdbc.validationQuery}" />
	</bean>

	<!-- Mybatis文件 -->
	<bean id="sqlSessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 指定mybatis的核心配置文件 -->
		<property name="configLocation"
			value="classpath:mybatis-config.xml" />
		<!-- mapper的接口和映射文件就可以不配置在同一个包下。mapper扫描器扫描的是接口所在的包。 -->
		<property name="mapperLocations">
			<array>
				<value>classpath:/mapper/*.xml</value>
			</array>
		</property>

		<!-- 注入数据源 -->
		<property name="dataSource" ref="dataSource" />
	</bean>



	<!-- 配置mapper扫描器 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.huawei.dao"></property>
	</bean>

	<!-- 扫描service -->
	<context:component-scan
		base-package="com.huawei.service.impl"></context:component-scan>

	<!-- 事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 开启事物注解 -->
	<tx:annotation-driven
		transaction-manager="transactionManager" />
</beans>

mybatis的配置文件mybatis-config.xml

同样的由于在applicationContexy.xml中配置了数据源和开启了mapper自动扫描。这里的配置简化为根据pojo类路径进行别名配置即可。

<?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>
	<!-- 全局别名设置,在映射文件中只需写别名,而不必写出整个类路径 -->
	<typeAliases>
		<package name="com.huawei.model" />
	</typeAliases>

</configuration>

Spring MVC 的配置文件springnvc-config.xml

这里需要配置的也很少。包括配置扫描controller注解的类,加载mvc注解驱动,配置视图解析器。

<?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: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-3.0.xsd  
     http://www.springframework.org/schema/context  
     http://www.springframework.org/schema/context/spring-context-3.0.xsd  
     http://www.springframework.org/schema/mvc  
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
     ">

<!-- 配置包扫描器,扫描@controller注解的类 -->
	<context:component-scan
		base-package="com.huawei.controller"></context:component-scan>
<!-- 加载注解驱动 -->
<mvc:annotation-driven/>
	<!-- 视图解析器 可以设置前缀和后缀,这样在Controller中跳转到指定界面就可以简化 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">

		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<property name="suffix" value=".jsp"></property>

	</bean>

</beans>

Web.xml

这里需要配置的有spring-mvc的前端控制器,spring的文件监听器,编码过滤器。

<?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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SSM configuration</display-name>
    <!-- 解决工程编码过滤器 -->
    <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>
    
    <!-- 配置加载spring文件的监听器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- SpringMVC配置 -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <welcome-file-list>
      <welcome-file>index.html</welcome-file>
      <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

猜你喜欢

转载自blog.csdn.net/weixin_42331540/article/details/81982467