SSM从零开始搭框架(二)

前言

继上篇博客之后,我们需要往里面填充,我们先来看一下service和web的目录结构


web

tomcat在启动一个web项目的时候,会先去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点。 

来看一下web.xml的内容.里面都写了注释,应该可以理解的

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
<!--参考博客链接: http://elf8848.iteye.com/blog/2008595, context-param声明应用范围内的初始化参数-->
<context-param>
  <param-name>webAppRootKey</param-name>
  <param-value>tgb.kwy</param-value>
</context-param>
  <!--springMVC配置-->
  <!--The front controller of this Spring Web application,responsible for handling all application requests-->
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--Spring 配置:needed for ContextLoaderListener-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!--Map all requests to the DispatcherServlet for handling-->
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <!--过滤器,encoding request; forceEncoding,同时设置response 解决乱码问题-->
<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>
  <init-param>
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>characterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

web.xml 的加载顺序是:context-param -> listener -> filter -> servlet ,而同个类型之间的实际程序调用的时候的顺序是根据对应的 mapping 的顺序进行调用的。

在配完了web.xml,就该配springmvc.xml了

我们先来看一下内容吧

springmvc.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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--springMVC只是控制网站跳转逻辑-->
    <!--扫描控制器-->
    <context:component-scan base-package="com.tgb.kwy.dubbo.controller"/>
    <!-- 注解扫描,处理动态资源 -->
    <mvc:annotation-driven/>

    <!--这里的PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现.可以将配置文件中的属性值放在另一个单独的标准Java Properties文件中去.在xml文件中用${key}替换指定的properties文件中的值.这样的话,只需要对properties文件进行修改,而不用对xml配置文件进行修改.-->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="1"/>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="locations">
            <list>
                <value>classpath:dubbo.properties</value>
            </list>
        </property>
    </bean>
    
    <!--使用import其实就是简化配置文件,分模块了,不然太多的配置文件,如果一个不小心,就不知道哪跟哪了.
    如果后面再有新的配置文件加入,就把xml写好. 在spring-context就import一下就好了.
    spring提供了两种前缀标记来辅助查找配置文件
    [classpath:]表示从classpath开始寻找后面的资源文件
    [file:]表示使用文件系统的方式寻找后面的文件(文件的完整路径)
    classpath:xxx相当于/WIN-INF/classes/xxx
    如果使用了classpath那就表示只会到class路径中去查找文件
    还有一个classpath* 表示不仅会在class路径中查找文件,还会在jar中查找.

    -->
    <import resource="classpath:spring/spring-dubbo.xml"/>
    <!--<import resource="classpath:spring/spring-swagger.xml"/>-->
    <!--因为前后端分离,所以没有页面,也就不需要视图解析器了-->
    <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/pages/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>-->
</beans>

添加dubbo配置文件, 即spring-dubbo.xml文件

spring-dubbo.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:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!-- dubbo配置-start -->
    <!--默认dubbo协议,现在我们项目就是用的默认dubbo协议,如果系统即支持dubbo,又支持http协议,只要通过protocol配置就可以了.-->
    <dubbo:protocol name="dubbo" port="20894"/>
    <!--当前应用名称,用于注册中心计算应用间依赖关系-->
    <dubbo:application name="dubbo-web"/>
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://192.168.22.60:2181"/>
    <!--timeout:远程服务调用超时时间(毫秒) ,check:启动时检查提供者是否存在,true报错,false
忽略-->
    <dubbo:consumer check="false" timeout="3000"/>
    <dubbo:annotation />
    <!-- dubbo配置-end -->
</beans>

web到这里,基本的配置就有了,下面配service

service

数据库使用的是mysql.自己先建一个数据库.并添加上基础数据

后面就是mybatis逆向工程生成实体和mapper.

把实体放到dubbo-api中.把mapper放到如上图位置中

现在来改配置文件:

由于我们的web和service是两个war包.service是一个单独的项目,所以需要配置web.xml.基本与web的是相似的

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:spring/spring-context.xml</param-value>
</context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <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>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

陪完了web.xml.我们就该配spring-context.xml了.

我们来看一下spring-context,先来整合dubbo.再整合mybatis;

整合dubbo

这里注释没咋写,因为和上面web的是查不多的,大家看一下上面的就好.

<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--引入配置文件-->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="1"/>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="locations">
            <list>
                <value>classpath:dubbo-service.properties</value>
            </list>
        </property>
    </bean>
    <import resource="classpath:spring/dubbo.xml"/>
</beans>

dubbo.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:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
					        http://www.springframework.org/schema/beans/spring-beans.xsd
					        http://code.alibabatech.com/schema/dubbo
			 		        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
			 		        ">

    <!-- 应用信息,用于计算依赖关系 -->
    <dubbo:application name="dubbo-service"/>
    <dubbo:protocol name="dubbo" port="20895"/>
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry protocol="zookeeper" address="{dubbo.registry.address}"  register="${dubbo.registry.register}"/>

    <dubbo:annotation package="com.tgb.kwy.dubbo.facade"/>
    <dubbo:annotation package="com.tgb.kwy.dubbo.service"/>
    <dubbo:consumer check="false" timeout="3000"/>
    <dubbo:provider timeout="3000"/>

</beans>

dubbo-service.properties

dubbo.registry.address=zookeeper://192.168.22.61:2181
dubbo.registry.register=true

数据源配置

spring-context.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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <!--引入配置文件-->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="1"/>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="locations">
            <list>
                <value>classpath:db.properties</value>
                <value>classpath:dubbo-service.properties</value>
            </list>
        </property>
    </bean>
    <!--context-scan把需要的类注册到spring容器中-->
    <!--配置service扫描 -->
    <context:component-scan base-package="com.tgb.kwy.dubbo.service"/>
    <context:component-scan base-package="com.tgb.kwy.dubbo.service.impl"/>
    <!--配置facade扫描 -->
    <context:component-scan base-package="com.tgb.kwy.dubbo.facade.impl"/>
    <!--配置dao扫描 -->
    <context:component-scan base-package="com.tgb.kwy.dubbo.dao"/>


    <import resource="classpath:spring/dubbo.xml"/>
    <!--引入数据库的配置文件-->
    <import resource="classpath:spring/datasource.xml"/>
</beans>

datasource.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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--spring用来控制业务逻辑,数据源,事务控制,aop-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--基本属性url user password-->
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc_url}"/>
        <property name="user" value="${jdbc_username}"/>
        <property name="password" value="${jdbc_password}"/>
    </bean>

    <!--spring 事务管理-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--开启基于注解的事务-->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
</beans>

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/401_itoo?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
jdbc_username=root
jdbc_password=root

整合mybatis

spring-context.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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--引入配置文件-->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="1"/>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="locations">
            <list>
                <value>classpath:db.properties</value>
                <value>classpath:dubbo-service.properties</value>
            </list>
        </property>
    </bean>
    <!--context-scan把需要的类注册到spring容器中-->
    <!--配置service扫描 -->
    <context:component-scan base-package="com.tgb.kwy.dubbo.service"/>
    <!--配置facade扫描 -->
    <context:component-scan base-package="com.tgb.kwy.dubbo.facade.impl"/>
    <!--配置dao扫描 -->
    <context:component-scan base-package="com.tgb.kwy.dubbo.dao"/>

    <import resource="classpath:spring/dubbo.xml"/>
    <!--引入数据库的配置文件-->
    <import resource="classpath:spring/datasource.xml"/>
    <!--整合mybatis
    目的:1.spring管理所有组件,mapper的实现类.
    service==>Dao @Autowired:自动注入mapper;
    2.spring用来管理事务,spring声明式事务-->
    <import resource="classpath:spring/spring-mybatis.xml"/>
</beans>

spring-mybatis

<?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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <!--创建出SqlSessionFactory对象,让spring ioc一启动,就代替我们自己创建的过程,spring创建数据库连接池,spring管理sqlSessionFactory,mapper代理对象,需要mybatis和spring的整合包-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 自动扫描mapping.xml文件,指定mapper文件的位置 ,这里只有一个,就写死了,多个使用mapper/*.xml来替代-->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    </bean>
    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <!--扫描所有的mapper接口的实现,让这些mapper能够自动注入-->
    <!--新版项目用这个-->
    <mybatis-spring:scan base-package="com.tgb.kwy.dubbo.dao"/>
    <!--老版项目用下面这四行,可以自定义更多的规则-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--basepackage:指定mapper接口的包名-->
        <property name="basePackage" value="com.tgb.kwy.dubbo.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

</beans>

剩下就是测试了.自己写一条线,单元测试一下就行了. 如果本地没有装zk和dubbo.那么请看这篇教程: linux 单机版安装zookeeper



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阳光下是个孩子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值