实战SSM_O2O商铺_04自下而上逐步整合SSM
生活随笔
收集整理的這篇文章主要介紹了
实战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。
我們自下而上來整合,即先整合數據庫持久層部分,然后整合服務層,最后整合控制層。
整合持久層配置
首先來看下數據庫持久層部分的配置
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全局屬性,說明詳見注釋
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration><!-- 配置全局屬性 --><settings><!-- 使用jdbc的getGeneratedKeys獲取數據庫自增主鍵值 --><setting name="useGeneratedKeys" value="true" /><!-- 使用列別名替換列名 默認:true --><setting name="useColumnLabel" value="true" /><!-- 開啟駝峰命名轉換:Table{create_time} -> Entity{createTime} --><setting name="mapUnderscoreToCamelCase" value="true" /><!-- 打印查詢語句 --><setting name="logImpl" value="STDOUT_LOGGING" /></settings> </configuration>spring-dao.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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 配置整合mybatis過程 --><!-- 1.配置數據庫相關參數properties的屬性:${url} --><context:property-placeholder location="classpath:jdbc.properties"/><!-- 2.數據庫連接池 --><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}" /><!-- c3p0連接池的私有屬性 --><property name="maxPoolSize" value="30" /><property name="minPoolSize" value="10" /><!-- 關閉連接后不自動commit --><property name="autoCommitOnClose" value="false" /><!-- 獲取連接超時時間 --><property name="checkoutTimeout" value="10000" /><!-- 當獲取連接失敗重試次數 --><property name="acquireRetryAttempts" value="2" /></bean><!-- 3.配置SqlSessionFactory對象 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 注入數據庫連接池 --><property name="dataSource" ref="dataSource" /><!-- 配置MyBaties全局配置文件:mybatis-config.xml --><property name="configLocation" value="classpath:mybatis-config.xml" /><!-- 掃描entity包 使用別名 --><property name="typeAliasesPackage" value="com.artisan.o2o.entity" /><!-- 掃描sql配置文件:mapper需要的xml文件 --><property name="mapperLocations" value="classpath:mapper/*.xml" /></bean><!-- 4.配置掃描Dao接口包,動態實現Dao接口,注入到spring容器中 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 注入sqlSessionFactory --><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /><!-- 給出需要掃描Dao接口包 --><property name="basePackage" value="com.artisan.o2o.dao" /></bean> </beans>整合Service層配置
spring-service.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:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 掃描service包下所有使用注解的類型 --><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
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 配置SpringMVC --><!-- 1.開啟SpringMVC注解模式 --><!-- 簡化配置: (1)自動注冊DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter (2)提供一些列:數據綁定,數字和日期的format @NumberFormat, @DateTimeFormat, xml,json默認讀寫支持 --><mvc:annotation-driven /><!-- 2.靜態資源默認servlet配置 (1)加入對靜態資源的處理:js,gif,png (2)允許使用"/"做整體映射 --><mvc:resources mapping="/resources/**" location="/resources/" /><mvc:default-servlet-handler /><!-- 3.定義視圖解析器 --><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><!-- 4.掃描web相關的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/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1" metadata-complete="true"><!-- 配置DispatcherServlet --><servlet><servlet-name>spring-dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 配置springMVC需要加載的配置文件 spring-dao.xml,spring-service.xml,spring-web.xml Mybatis - > spring -> springmvc --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/spring-*.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>spring-dispatcher</servlet-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_O2O商铺_04自下而上逐步整合SSM的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实战SSM_O2O商铺_03项目结构规划
- 下一篇: 实战SSM_O2O商铺_05集成SSM后