當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringMVC学习08之SSM整合(三)
生活随笔
收集整理的這篇文章主要介紹了
SpringMVC学习08之SSM整合(三)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
回顧
上一章我們完成了Mybatis底層的編寫
Spring層配置
一、spring層配置
1、配置Spring整合MyBatis,我們這里數據源使用c3p0連接池
2、編寫Spring整合Mybatis的相關的配置文件;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/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><!-- 配置整合mybatis --><!-- 1.關聯數據庫文件 --><context:property-placeholder location="classpath:database.properties"/><!-- 2.數據庫連接池 --><!--數據庫連接池dbcp 半自動化操作 不能自動連接c3p0 自動化操作(自動的加載配置文件 并且設置到對象里面)--><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"/></bean><!-- 4.配置掃描Dao接口包,動態實現Dao接口注入到spring容器中 --><!--解釋 :https://www.cnblogs.com/jpfss/p/7799806.html--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 注入sqlSessionFactory --><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/><!-- 給出需要掃描Dao接口包 --><property name="basePackage" value="com.shan.dao"/></bean></beans>3、Spring整合service層
<?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"><!-- 掃描service相關的bean --><context:component-scan base-package="com.shan.service" /><!--BookServiceImpl注入到IOC容器中--><bean id="BookServiceImpl" class="com.shan.service.BookServiceImpl"><property name="bookMapper" ref="bookMapper"/></bean><!-- 配置事務管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- 注入數據庫連接池 --><property name="dataSource" ref="dataSource" /></bean></beans>二、springMVC層配置
1、spring-mvc.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/mvchttps://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 配置SpringMVC --><!-- 1.開啟SpringMVC注解驅動 --><mvc:annotation-driven /><!-- 2.靜態資源默認servlet配置--><mvc:default-servlet-handler/><!-- 3.配置jsp 顯示ViewResolver視圖解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean><!-- 4.掃描web相關的bean --><context:component-scan base-package="com.shan.controller" /></beans>2、Spring配置整合文件,applicationContext.xml
整合Spring的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><import resource="spring-dao.xml"/><import resource="spring-service.xml"/><import resource="spring-mvc.xml"/></beans>配置到此結束,接下來我們開始編寫代碼,完成后臺和前端的操作!
作者有話說
博客創作不易,希望看到這里的讀者動動你的小手點個贊,如果喜歡的小伙伴可以一鍵三連,作者大大在這里給大家謝謝了。
總結
以上是生活随笔為你收集整理的SpringMVC学习08之SSM整合(三)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringMVC学习06之SSM整合(
- 下一篇: SpringMVC学习10之AJAX初体