Java開發(fā)之ssm三大框架整合
1.springmvc
和只有spring-mvc時一樣,web.xml spring-mvc.xml
spring-mvc.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/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <!-- 注解驅動--> <mvc:annotation-driven/> <!-- 靜態(tài)資源過濾--> <!-- 開啟jsp專用的視圖控制器 internalresourceresoler--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 設置前綴--> <property name="prefix" value="/WEB-INF/templates/"></property> <!-- 設置后綴--> <property name="suffix" value=".jsp"></property> </bean> <!-- 掃描 controller注解--> <context:component-scan base-package="com.hxut.rj1192.zyk.Controller"></context:component-scan> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <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_4_0.xsd" version="4.0"> <!-- 設置攔截器,解決參數(shù)亂碼,一定要在設置HiddenHttpMethodFilter請求前,要在其他攔截器和servlet執(zhí)行前設置編碼--> <filter> <filter-name>paramencoding</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> <!-- 解決返回的請求數(shù)亂碼 response--> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <!--攔截所有頁面--> <filter-mapping> <filter-name>paramencoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--servlet 將所有除了jsp的頁面攔截,交給dispatcherservlet視圖控制器,并設置dispatcherservlet的xml文件的位置--> <servlet-mapping> <servlet-name>all</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <servlet> <servlet-name>all</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:Spring-springmvc.xml</param-value> </init-param> </servlet> <!-- 攔截所有請求,并交給hiddenhttpmethodfilter 檢測否是post請求,且_method不為空,如果是,就將請求類型改為_method的值--> <filter> <filter-name>hiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>hiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
2.spring-dao.xml與mybatis-config.xml
主要就是spring整合mybatis
在上面的基礎上,去掉成接口的實現(xiàn)類了,需要配置dao接口掃描包,我的理解是這個dao接口掃描包中有datasource,有mapper的掃描范圍, 它會自動生成這些接口對應的mapper,并將接口的mapper放到xml文件中,所以在spring-service中,直接
<property name="bookmapper" ref="bookmapper"></property>
引用即可
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframwork.org/schema/context/spring-context.xsd"> <!-- 讀取數(shù)據(jù)庫配置文件--> <context:property-placeholder location="classpath:database.properties"></context:property-placeholder> <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"/> <!-- 關閉連接后不自動commit --> <property name="autoCommitOnClose" value="false"/> <!-- 獲取連接超時時間 --> <property name="checkoutTimeout" value="10000"/> <!-- 當獲取連接失敗重試次數(shù) --> <property name="acquireRetryAttempts" value="2"/> </bean> <bean id="sqlsessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!--配置dao接口掃描包 ,動態(tài)的實現(xiàn)了dao接口可以注入到spring容器中 就是用來代替BookMapperImpl類 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlsessionfactory--> <!--個人理解,這個dao接口掃描包中有datasource,有mapper的掃描范圍, 它會自動生成這些接口對應的mapper,并將接口的mapper放到xml文件中,所以在spring-service中,直接 <property name="bookmapper" ref="bookmapper"></property> 引用即可--> <property name="sqlSessionFactoryBeanName" value="sqlsessionFactory"></property> <!-- 要掃描的dao包, 會自動生成包下的類的接口的實現(xiàn)類--> <property name="basePackage" value="com.hxut.rj1192.zyk"></property> </bean> </beans>
mybatis-config.xml 詳細在上面的mybatis整合spring的文章中,它做兩件事,配置映射文件路徑,配置接口掃描范圍,它被import到 spring-dao.xml中。
<?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> <!-- 配置數(shù)據(jù)源交給spring了--> <!-- 給類起別名--> <typeAliases> <package name="com.hxut.rj1192.zyk.mapper"/> </typeAliases> <!-- 設置映射文件路徑--> <mappers> <mapper resource="com/hxut/rj1192/zyk/mapper/Bookmapper.xml"></mapper> </mappers> </configuration>
3.spring-service.xml
在這個文件中要進行事務的處理(事務本來就應該是在service層),要將service層的類全部放到ioc容器中,然后這些類中因為調用了dao層的類,然后因為剛才第二部配置了接口掃描包,直接ref獲取mapper即可
<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 https://www.springframwork.org/schema/context/spring-context.xsd"> <!-- 開啟注解驅動--> <context:component-scan base-package="com.hxut.rj1192.zyk.service"></context:component-scan> <bean id="booksServiceimpl" class="com.hxut.rj1192.zyk.service.BooksServiceimpl"> <property name="bookmapper" ref="bookmapper"></property> </bean> <!-- 聲明式事務--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入數(shù)據(jù)源 --> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
4.引用
將這些文件的引用放到一個大的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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="applicationContext.xml"></import> <import resource="spring-dao.xml"></import> <import resource="Spring-Service.xml"></import> <import resource="Spring-springmvc.xml"></import> </beans>
或者在project structure中設置 spring application context,效果是一樣的
到此這篇關于Java開發(fā)之ssm三大框架整合的文章就介紹到這了,更多相關Java ssm框架整合內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
IDEA 創(chuàng)建一個Mybatis Maven項目的方法步驟(圖文)
這篇文章主要介紹了IDEA 創(chuàng)建一個Mybatis Maven項目的方法步驟(圖文),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03Spring Security實現(xiàn)多次登錄失敗后賬戶鎖定功能
當用戶多次登錄失敗的時候,我們應該將賬戶鎖定,等待一定的時間之后才能再次進行登錄操作。今天小編給大家分享Spring Security實現(xiàn)多次登錄失敗后賬戶鎖定功能,感興趣的朋友一起看看吧2019-11-11springboot maven 項目打包jar 最后名稱自定義的教程
這篇文章主要介紹了springboot maven 項目打包jar 最后名稱自定義的教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10