基于maven的SSM项目搭建步骤

上一篇中介绍了每个pom文件需要引用的依赖,考到自己的项目的时候需要注意每个parent中的g,a,v需要和自己的保持一致,不然dependency会出现包丢失的错误,以下是框架搭建步骤

1.entity和dao层中没什么好说的,全部都是通过工具生成的实体类

2.interface中就写自己需要的接口就行了

3.service:(这个需要注意),

把配置文件写在当前模块中的原因:

    以前写过一个SSH项目,没有通过分布式来写,配置文件都写在web层,直接build主程序,就可以吧整个程序跑起来;但是现在没有原来的那个web了,所以service就充当了以前web的角色,所以web.xml和几个配置文件都需要写在service中


4.一下介绍每个配置文件中具体的代码:

首先是database.properties:(这里一个需要注意的地方,我之前写项目的时候没有在每个属性前面加jdbc.只是driver=。。。。,这样配置的时候程序出错了,说是需要去Administrator中寻找一个东西,具体是什么东西忘了,在每个配置文件前面加上jdbc.就好了,网上解释说database.properties找你的属性的时候,因为名字相同,用了别的东西,说这些的主要目的还是大家在每个)



5.我的mybatis中配置的是mybatis使用自身的分页工具类的配置:如果不用分页的话可以不用配置这个,以下是代码(所用的版本在上一个博客中可以找到,这里需要说明一下,老版本的分页,和新版本的分页的配置文件有区别,需要注意):

<?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>
     <plugins>
           <plugin interceptor="com.github.pagehelper.PageInterceptor">
           		<!-- 分页参数合理化  -->  
        		<property name="reasonable" value="true"/> 
           </plugin>
     </plugins>
 </configuration>

6.applicationContext-dao:

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

      <!-- 加載配置文件 -->
	  <context:property-placeholder location="classpath:conf/database.properties"/>
	
      <!-- 数据库连接池 -->
      <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 
      	destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="10"></property>
        <property name="minIdle" value="5"></property>
    </bean>
	 <!-- 讓spring管理SqlSessionFactoryBean 使用mybatis和spring整合包中的 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 引用数据源组件 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 引用MyBatis配置文件中的配置 -->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
    </bean>
    
    <!-- 3. 自动扫描mybatis映射文件和接口的包自動生成實例 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn.lichenyang.emall.dao"></property>
	</bean>
	

</beans>

7.applicationContext-trans

<?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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
	http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-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/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-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="login" propagation="SUPPORTS" read-only="true"/>
    		<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
    		<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
    		<tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
    		<tx:method name="*" propagation="REQUIRED"/>
    	</tx:attributes>
    </tx:advice>
    
    <!-- 切面 -->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.lichenyang.emall.service..*.*(..))" />
   </aop:config>

</beans>

8.applicationContext-service(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:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
	http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd
	http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-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/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
	
	<!-- 配置包扫描器 -->
	<context:component-scan base-package="cn.lichenyang.emall.service.impl"></context:component-scan>

	<!-- 使用bubbo发布服务 -->
	<!-- 提供应用信息,用于计算依赖关系 -->
	<dubbo:application name="emall"/>
	<dubbo:registry protocol="zookeeper" address="10.10.26.163:2181" /><!-- 这个需要在zookeeper中配置的,我会在下一个博客中说明如何配置 -->
	<!-- 用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo" port="20880" />
	<!-- 
		声名需要暴露的服务端口
		ref:只能是service实现类名字,且首字母要小写!!!
	 -->
	<dubbo:service interface="cn.lichenyang.emall.service.ItemService" ref="itemServiceImpl" timeout="600000"/>
	<dubbo:service interface="cn.lichenyang.emall.service.ItemCatService" ref="itemCatServiceImpl" timeout="600000"/>
	
</beans>
    

9.log4j没什么好说的:

# Global logging configuration
log4j.rootLogger=INFO, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

10.webxml

<?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_2_5.xsd" version="2.5">
  <display-name>emall-service</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  

	<!--监听器去找classpath:applicationContext.xml-->
	<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/applicationContext-*.xml</param-value>
	</context-param>
	
	<!--设置监听器-->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

</web-app>

好了,接下来是web层的配置了:

因为web是在搭建模板中不是主项目的子模块,所以需要把web创建在parent下面,从pom中可以看到,一下是模块中的内容


接下来介绍每个配置文件中的内容:

conf文件夹下面的resource.properties是做文件上传下载的,以后的博客中会讲到(1星期内更新)

接下来说springmvc中的配置:

<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
	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/tx
    http://www.springframework.org/schema/tx/spring-tx-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/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">

	<!-- 加载配置文件 -->
	<context:property-placeholder location="classpath:conf/resource.properties"/>

	<!-- 配置包扫描器 -->
	<context:component-scan base-package="cn.lichenyang.emall.web.controller"></context:component-scan>
	<mvc:annotation-driven />
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/content/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

	<!-- 配置这个看下一张图片 -->
	<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>	
	<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
	<mvc:resources location="/upload/itemdesc/" mapping="/upload/itemdesc/**"></mvc:resources>
	
	<!-- 使用bubbo发布服务 -->
	<!-- 提供应用信息,用于计算依赖关系 -->
	<dubbo:application name="emall-web"/>
	<!-- 这里的address需要和applicationContext-server中的address相同 -->
	<dubbo:registry protocol="zookeeper" address="10.10.26.163:2181" />
	<!-- 声名需要暴露的服务端口,注意啊!这里用的是reference!!!! -->
	<dubbo:reference interface="cn.lichenyang.emall.service.ItemService" id="itemService"/>
	<dubbo:reference interface="cn.lichenyang.emall.service.ItemCatService" id="itemCatService"/>
	
	<!-- 定义文件上传解析器 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 设定默认编码 -->
		<property name="defaultEncoding" value="UTF-8"></property>
		<!-- 设定文件上传最大值5M,5*1024*1024 -->
		<property name="maxUploadSize" value="5242880"></property>
	</bean>
	
</beans>
    

配置mvc:resource的目的是不拦截css,js和itemdesc中的图片


加下来是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_2_5.xsd" version="2.5">
  <display-name>emall-web</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  
	<!-- 解决post方式造成的中文乱码问题 -->
	<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>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- 加载springmvc配置文件 -->
	<servlet>
		<servlet-name>springmvc</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>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>


猜你喜欢

转载自blog.csdn.net/qq_37385585/article/details/79873584