实战SSM_O2O商铺_04自下而上逐步整合SSM

文章目录

  • 概述
  • 整合持久层配置
    • jdbc.properties
    • MyBatis全局配置文件mybatis-config.xml
    • spring-dao.xml
  • 整合Service层配置
    • spring-service.xml
  • 整合控制层(web/controller)配置
    • spring-web.xml
  • web.xml中整合spring集成的配置
    • web.xml
  • 总结
  • Github地址

实战SSM_O2O商铺_04自下而上逐步整合SSM_第1张图片

概述

在划分好基本的项目结构后,我们接下来整合SSM。

我们自下而上来整合,即先整合数据库持久层部分,然后整合服务层,最后整合控制层。

实战SSM_O2O商铺_04自下而上逐步整合SSM_第2张图片


整合持久层配置

首先来看下数据库持久层部分的配置

jdbc.properties

在/src/main/resources新建jdbc.properties,配置数据库连接信息

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/o2o?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root

用户名和密码暂时使用明文保存,后续更改为密文.


MyBatis全局配置文件mybatis-config.xml

在/src/main/resources新建mybatis-config.xml,配置MyBatis全局属性,说明详见注释



<configuration>
	
	<settings>
		
		<setting name="useGeneratedKeys" value="true" />

		
		<setting name="useColumnLabel" value="true" />

		
		<setting name="mapUnderscoreToCamelCase" value="true" />
		
		<setting name="logImpl" value="STDOUT_LOGGING" />
	settings>
configuration>

spring-dao.xml


<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"
	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">
    
	

	
	<context:property-placeholder location="classpath:jdbc.properties"/>
	
	
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		
		<property name="driverClass" value="${jdbc.driver}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />

		
		<property name="maxPoolSize" value="30" />
		<property name="minPoolSize" value="10" />
		
		<property name="autoCommitOnClose" value="false" />
		
		<property name="checkoutTimeout" value="10000" />
		
		<property name="acquireRetryAttempts" value="2" />
	bean>

	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		
		<property name="dataSource" ref="dataSource" />
		
		<property name="configLocation" value="classpath:mybatis-config.xml" />
		
		<property name="typeAliasesPackage" value="com.artisan.o2o.entity" />
		
		<property name="mapperLocations" value="classpath:mapper/*.xml" />
	bean>

	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
		
		<property name="basePackage" value="com.artisan.o2o.dao" />
	bean>
beans>

整合Service层配置

spring-service.xml


<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">
    
    <context:component-scan base-package="com.artisan.o2o.service" />

    
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
        <property name="dataSource" ref="dataSource" />
    bean>

    
    <tx:annotation-driven transaction-manager="transactionManager" />
beans>

整合控制层(web/controller)配置

spring-web.xml


<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.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">
	
	
	
	
	
	<mvc:annotation-driven />

	
	<mvc:resources mapping="/resources/**" location="/resources/" />
	<mvc:default-servlet-handler />

	
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/html/">property>
		<property name="suffix" value=".html">property>
	bean>
	
	
	<context:component-scan base-package="com.artisan.o2o.web" />
	
beans>

web.xml中整合spring集成的配置

web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1" metadata-complete="true">
	
	
	<servlet>
		<servlet-name>spring-dispatcherservlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
		
		<init-param>
			<param-name>contextConfigLocationparam-name>
			<param-value>classpath:spring/spring-*.xmlparam-value>
		init-param>
	servlet>
	<servlet-mapping>
		<servlet-name>spring-dispatcherservlet-name>
		
		<url-pattern>/url-pattern>
	servlet-mapping>
	
	
web-app>

总结

我们通过pom.xml加载了依赖jar包,然后在持久层的配置文件spring-dao.xml加载了jdbc.properties以及mybatis的全局配置文件mybatis-config.xml,并且集成了mybatis的配置,

然后service层配置了配置了扫描路径,确定了哪些service层的bean由Spring管理,同时配置了事务管理。

紧接着控制层部分主要是集成SpringMVC。

最后通过web.xml加载了各项配置文件,确保在web容器启动时,Spring也一并启动并初始化各项配置信息。


Github地址

代码地址: https://github.com/yangshangwei/o2o

你可能感兴趣的:(【实战-SSM,In,Action】,实战系列-SSM实战)