易优商城 SSM配置文件(一)

本项目采用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>

个人项目 仅提供参考;

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sun897827804

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值