本項目採用dubbo分佈式架構:現在主要配置服務提供者(service)
(1)使用mybaits 逆向工程生成pojo和相關mapper映射文件
(2)配置sqlMapConfig.xml文件: “暫時只需要引入頭文件”
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//www.mybatis.org//DTD Config 3.0//EN"
"http://www.mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--此處可以引入mybatis的第三方插件-->
</configuration>
(3)配置spring的核心配置文件: 此處分爲三個配置文件,dao,service,transaction
spring-dao.xml文件:
1)配置據庫連接池:
加載jdbc.properties配置文件;
配置dataSource數據源;
2)配置sqlSessionFactory :使用spring管理sqlSessionFactory和mybatis,
3)配置mapper映射文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 配置數據庫連接池 -->
<!-- 加載數據庫的配置文件 :src/main/resource下-->
<context:property-placeholder location="classpath:config/*.properties" />
<!-- 數據庫連接池 -->
<!--
此外數據庫連接池還有:C3P0、DBCP、PROXOOL。。。。
-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<!-- 數據庫鏈接url -->
<property name="url" value="${jdbc.url}"></property>
<!-- 數據庫驅動 -->
<property name="driverClassName" value="${jdbc.driver}"></property>
<!-- 用戶名 -->
<property name="username" value="${jdbc.username}"></property>
<!-- 密碼 -->
<property name="password" value="${jdbc.password}"></property>
<!-- 最大空閒鏈接-->
<property name="maxActive" value="10"></property>
<!-- 最小空閒連接 -->
<property name="minIdle" value="5"></property>
</bean>
<!-- 讓spring管理sqlsessionfactory 使用mybatis 和 spring整合包中的bean -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 數據庫連接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 加載mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
</bean>
<!-- 掃描mapper映射文件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.yiyou.mapper"></property>
</bean>
</beans>
spring-service.xml 配置文件:用來暴露服務,同時提供服務接口;
1)配置包掃描器;用於掃描業務層
2)使用dubbo發佈服務
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 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-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 配置包 掃描器 -->
<context:component-scan base-package="cn.yiyou.service"></context:component-scan>
<!-- 使用dubbo發佈服務 -->
<!-- 提供方應用信息,用於計算依賴關係 -->
<dubbo:application name="yiyou-manager"/>
<!-- 設置註冊中心 -->
<dubbo:registry protocol="zookeeper" address="192.168.1.4:2181"></dubbo:registry>
<!-- 用dubbo協議在20880端口暴露服務
timeout:配置超時時間;默認爲1000毫秒
-->
<dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
<!-- 聲明需要暴露服務的端口 -->
<dubbo:service interface="cn.yiyou.service.ItemService" ref="itemServiceImpl" timeout="600000" > </dubbo:service>
</beans>
spring-transaction.xml :事務
1)配置事務管理;DataSourceTransactionManager
2)配置事務通知:tx:advice
3)配置切面:aop
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 配置事務管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 數據源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 傳播行爲 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.yiyou.service..*.*(..)``
" />
</aop:config>
</beans>
(4)配置web.xml 文件:(初始化spring容器)
<!-- 加載spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 加載以applicationContext開頭的xml文件 -->
<param-value>classpath:spring/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
配置服務引用方(web):
(1)springmvc-servlet.xml:表現層的配置
1)註解驅動
2)配置controller包掃描器
3)配置視圖解析器:internelResourceViewResolver
4)配置靜態資源映射
5)引用dubbo服務
<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"`
<context:component-scan base-package="cn.yiyou.controller"></context:component-scan>
<!-- 註解形式註解驅動 -->
<mvc:annotation-driven />
<!-- 配置試圖解析器 -->
<bean name="jspviewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前綴 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 後綴 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置資源映射
location:靜態資源所在目錄
mapping:瀏覽器url地址中 如果包含 /css/下的所有文件,則會到location中對應的目錄下查找
-->
<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<!-- 引用dubbo服務 -->
<dubbo:application name="yiyou-manager-web"/>
<!-- 註冊中心 -->
<dubbo:registry protocol="zookeeper" address="192.168.1.4:2181"/>
<!-- 引用服務 -->
<dubbo:reference interface="cn.yiyou.service.ItemService" id=````
itemService" />
</beans>
(2)配置web.xml
<!-- springMVC前端控制器 -->
<servlet>
<servlet-name>yiyou-manager</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation 不是必須要配置的,如果不配置,springmvc的配置文件默認在WEB-INF文件夾下 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>yiyou-manager</servlet-name>
<url-pattern>/</url-pattern> <!-- 每執行一次請求,就攔截一次,(不包括jsp) -->
</servlet-mapping>
<!-- 解決pos亂碼 -->
<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>
個人項目 僅提供參考;