南山商城网站建设,网络宣传网站建设,WordPress图床api,永久免费的网站推荐参考http://www.cnblogs.com/xdp-gacl/p/3498271.html 。 1.什么是maven maven是基于项目对象模型(POM)#xff0c;是跨平台的管理工具#xff0c;主要服务于java平台的项目构建#xff0c;依赖管理和项目信息管理。 2.maven的好处 Maven中使用约定#xff0c;约定java源代…参考http://www.cnblogs.com/xdp-gacl/p/3498271.html 。 1.什么是maven maven是基于项目对象模型(POM)是跨平台的管理工具主要服务于java平台的项目构建依赖管理和项目信息管理。 2.maven的好处 Maven中使用约定约定java源代码代码必须放在哪个目录下编译好的java代码又必须放到哪个目录下这些目录都有明确的约定。 Maven的每一个动作都拥有一个生命周期例如执行 mvn install 就可以自动执行编译测试打包等构建过程 只需要定义一个pom.xml,然后把源码放到默认的目录Maven帮我们处理其他事情 使用Maven可以进行项目高度自动化构建依赖管理(这是使用Maven最大的好处)仓库管理。 3.安装maven 下载地址http://maven.apache.org/download.cgi 下载完成后得到一个压缩包解压可以看到maven的组成目录 Maven目录分析 bin含有mvn运行的脚本boot含有plexus-classworlds类加载器框架conf含有settings.xml配置文件lib含有Maven运行时所需要的java类库LICENSE.txt, NOTICE.txt, README.txt针对Maven版本第三方软件等简要介绍 注意在安装maven之前要先确定本机上是否安装jak 要jdk 1.6的版本使用如下的两个命令检查检查JDK安装的情况。 1 Echo %JAVA_HOME% 2 Java -version *设置环境变量MAVEN_HOME 本人是配置在本机用户下 配置在系统下步骤一样。 *设置环境变量Path将%MAVEN_HOME%\bin加入Path中一定要注意要用分号与其他值隔开如下图所示 *测试 maven 搭建是否成功 打开cmd窗口输入mvn -v命令 查看Maven的相关信息。如图 4.ok,进入主话题搭建Maven 项目 ssmspringMVCspringmybatis 在平时的Javaweb项目开发中为了便于后期的维护我们一般会进行分层开发最常见的就是分为domain域模型层、dao数据库访问层、service业务逻辑 层、web表现层这样分层之后各个层之间的职责会比较明确后期维护起来也相对比较容易今天我们就是使用Maven来构建以上的各个层。 一创建 demo_parent(父类用于给各个子模块继承) 在pom.xml文件中 配置ssm需要的jar包。 maven jar包仓库地址 http://mvnrepository.com/ 1 project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance2 xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd3 modelVersion4.0.0/modelVersion4 groupIdcom.baidu/groupId5 artifactIddemo_parent/artifactId6 version0.0.1-SNAPSHOT/version 7 packagingpom/packaging 8 9 !-- 统一jar包版本 -- 10 properties 11 project.build.sourceEncodingUTF-8/project.build.sourceEncoding 12 spring.version3.2.4.RELEASE/spring.version 13 /properties 14 15 16 17 dependencies 18 dependency 19 groupIdcommons-fileupload/groupId 20 artifactIdcommons-fileupload/artifactId 21 version1.2.1/version 22 /dependency 23 24 dependency 25 groupIdcommons-io/groupId 26 artifactIdcommons-io/artifactId 27 version1.4/version 28 /dependency 29 30 dependency 31 groupIdcom.googlecode.json-simple/groupId 32 artifactIdjson-simple/artifactId 33 version1.1/version 34 /dependency 35 36 37 dependency 38 groupIdjunit/groupId 39 artifactIdjunit/artifactId 40 version4.10/version 41 scopetest/scope 42 /dependency 43 44 dependency 45 groupIdcom.oracle/groupId 46 artifactIdojdbc14/artifactId 47 version10.2.0.1.0/version 48 /dependency 49 50 dependency 51 groupIdcom.fasterxml.jackson.core/groupId 52 artifactIdjackson-core/artifactId 53 version2.2.3/version 54 /dependency 55 56 57 dependency 58 groupIdorg.springframework/groupId 59 artifactIdspring-core/artifactId 60 version${spring.version}/version 61 /dependency 62 dependency 63 groupIdorg.springframework/groupId 64 artifactIdspring-beans/artifactId 65 version${spring.version}/version 66 /dependency 67 dependency 68 groupIdorg.springframework/groupId 69 artifactIdspring-aop/artifactId 70 version${spring.version}/version 71 /dependency 72 dependency 73 groupIdorg.springframework/groupId 74 artifactIdspring-context/artifactId 75 version${spring.version}/version 76 /dependency 77 dependency 78 groupIdorg.springframework/groupId 79 artifactIdspring-web/artifactId 80 version${spring.version}/version 81 /dependency 82 dependency 83 groupIdorg.springframework/groupId 84 artifactIdspring-tx/artifactId 85 version${spring.version}/version 86 /dependency 87 dependency 88 groupIdorg.springframework/groupId 89 artifactIdspring-jdbc/artifactId 90 version${spring.version}/version 91 /dependency 92 dependency 93 groupIdcommons-dbcp/groupId 94 artifactIdcommons-dbcp/artifactId 95 version1.4/version 96 /dependency 97 dependency 98 groupIdorg.mybatis/groupId 99 artifactIdmybatis/artifactId 100 version3.2.7/version 101 /dependency 102 dependency 103 groupIdorg.mybatis/groupId 104 artifactIdmybatis-spring/artifactId 105 version1.2.2/version 106 /dependency 107 108 109 dependency 110 groupIdorg.aspectj/groupId 111 artifactIdaspectjweaver/artifactId 112 version1.8.5/version 113 /dependency 114 115 116 dependency 117 groupIdlog4j/groupId 118 artifactIdlog4j/artifactId 119 version1.2.12/version 120 /dependency 121 122 dependency 123 groupIdcom.fasterxml.jackson.core/groupId 124 artifactIdjackson-annotations/artifactId 125 version2.2.3/version 126 /dependency 127 128 dependency 129 groupIdcom.fasterxml.jackson.core/groupId 130 artifactIdjackson-databind/artifactId 131 version2.2.3/version 132 /dependency 133 134 dependency 135 groupIdorg.springframework/groupId 136 artifactIdspring-webmvc/artifactId 137 version${spring.version}/version 138 /dependency 139 140 dependency 141 groupIdorg.springframework/groupId 142 artifactIdspring-webmvc-portlet/artifactId 143 version${spring.version}/version 144 /dependency 145 146 dependency 147 groupIdorg.springframework/groupId 148 artifactIdspring-test/artifactId 149 version${spring.version}/version 150 /dependency 151 152 dependency 153 groupIdorg.springframework/groupId 154 artifactIdspring-oxm/artifactId 155 version${spring.version}/version 156 /dependency 157 158 dependency 159 groupIdorg.springframework/groupId 160 artifactIdspring-jms/artifactId 161 version${spring.version}/version 162 /dependency 163 164 dependency 165 groupIdorg.springframework/groupId 166 artifactIdspring-expression/artifactId 167 version${spring.version}/version 168 /dependency 169 170 dependency 171 groupIdorg.springframework/groupId 172 artifactIdspring-context-support/artifactId 173 version${spring.version}/version 174 /dependency 175 176 dependency 177 groupIdorg.springframework/groupId 178 artifactIdspring-instrument/artifactId 179 version${spring.version}/version 180 /dependency 181 182 dependency 183 groupIdorg.springframework/groupId 184 artifactIdspring-instrument-tomcat/artifactId 185 version${spring.version}/version 186 /dependency 187 188 dependency 189 groupIdorg.springframework/groupId 190 artifactIdspring-aspects/artifactId 191 version${spring.version}/version 192 /dependency 193 194 dependency 195 groupIdorg.springframework/groupId 196 artifactIdspring-aop/artifactId 197 version${spring.version}/version 198 /dependency 199 200 dependency 201 groupIdjavax.servlet/groupId 202 artifactIdjavax.servlet-api/artifactId 203 version3.0.1/version 204 scopeprovided/scope 205 /dependency 206 dependency 207 groupIdjavax.servlet/groupId 208 artifactIdjsp-api/artifactId 209 version2.0/version 210 scopeprovided/scope 211 /dependency 212 dependency 213 214 215 groupIdjavax.servlet/groupId 216 artifactIdjstl/artifactId 217 version1.2/version 218 /dependency 219 dependency 220 groupIdcommons-pool/groupId 221 artifactIdcommons-pool/artifactId 222 version1.5.3/version 223 /dependency 224 225 /dependencies 226 /project 二: 创建 demo_domain (模型层) 步骤如上右键-》new-》maven project-(直接)next-next-finish 生产项目如下 在pom.xml中 配置依赖关系 project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionartifactIddemo_domain/artifactIdpackagingjar/packagingnamedemo_domain/nameurlhttp://maven.apache.org/url!-- 依赖父类 demo_parent --parentgroupIdcom.baidu/groupIdartifactIddemo_parent/artifactIdversion0.0.1-SNAPSHOT/versionrelativePath../garden_partent/pom.xml/relativePath/parentpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/properties/project 三创建demo_service 和 demo_dao 步骤和 demo_domain一样 四创建demo_web (filtermave-archetype-webapp) 五把项目进行整合。加入 ssm整合的 配置文件 在各个项目中配置相应的xml 1.在 demo_web中 配置springMVC.xml文件 扫描controller层 1 ?xml version1.0 encodingUTF-8 ?2 beans3 xmlnshttp://www.springframework.org/schema/beans4 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance5 xmlns:phttp://www.springframework.org/schema/p 6 xmlns:contexthttp://www.springframework.org/schema/context 7 xmlns:mvchttp://www.springframework.org/schema/mvc 8 xsi:schemaLocationhttp://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context-3.2.xsd 12 http://www.springframework.org/schema/mvc 13 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 14 15 16 context:component-scan base-packagecom.baidu.*.controller/ 17 mvc:annotation-driven/ 18 mvc:default-servlet-handler/ 19 20 21 !-- 视图 解析器 -- 22 bean idviewResolver classorg.springframework.web.servlet.view.InternalResourceViewResolver 23 property nameprefix value/WEB-INF// 24 property namesuffix value.jsp/ 25 /bean 26 27 28 /beans web.xml的配置springMVC的核心控制器... ... 1 ?xml version1.0 encodingUTF-8?2 web-app xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlnshttp://java.sun.com/xml/ns/javaee xsi:schemaLocationhttp://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd idWebApp_ID version2.53 display-namessm/display-name4 5 6 7 !-- spring MVC核心控制器 8 9 springmvc配置文件 默认 10 位置WEB-INF 11 名称 [servlet-name]-servlet.xml 12 -- 13 servlet 14 servlet-namespringmvc/servlet-name 15 servlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class 16 17 init-param 18 param-namecontextConfigLocation/param-name 19 param-valueclasspath*:conf/**/springmvc.xml/param-value 20 /init-param 21 22 23 !-- 配置加载顺序 -- 24 load-on-startup1/load-on-startup 25 26 27 /servlet 28 29 servlet-mapping 30 servlet-namespringmvc/servlet-name 31 url-pattern//url-pattern 32 /servlet-mapping 33 34 35 36 37 !-- 编码过滤器 -- 38 filter 39 filter-nameencoding/filter-name 40 filter-classorg.springframework.web.filter.CharacterEncodingFilter/filter-class 41 init-param 42 param-nameencoding/param-name 43 param-valueUTF-8/param-value 44 /init-param 45 /filter 46 47 filter-mapping 48 filter-nameencoding/filter-name 49 url-pattern/*/url-pattern 50 /filter-mapping 51 52 53 54 !-- spring 加载的监听器 55 加载spring配置文件 默认 WEB-INF 名称 applicationContext.xml 56 -- 57 58 context-param 59 param-namecontextConfigLocation/param-name 60 param-valueclasspath*:conf/**/spring*.xml/param-value 61 /context-param 62 63 listener 64 listener-classorg.springframework.web.context.ContextLoaderListener/listener-class 65 /listener 66 67 68 !-- 定义LOG4J监听器 -- 69 listener 70 listener-classorg.springframework.web.util.Log4jConfigListener/listener-class 71 /listener 72 73 74 welcome-file-list 75 welcome-fileindex.html/welcome-file 76 welcome-fileindex.htm/welcome-file 77 welcome-fileindex.jsp/welcome-file 78 welcome-filedefault.html/welcome-file 79 welcome-filedefault.htm/welcome-file 80 welcome-filedefault.jsp/welcome-file 81 /welcome-file-list 82 /web-app 2.在 demo_service 下的 src/main/resources 中放入spring.xml 1 ?xml version1.0 encodingUTF-8?2 beans xmlnshttp://www.springframework.org/schema/beans3 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance4 xmlns:aophttp://www.springframework.org/schema/aop5 xmlns:contexthttp://www.springframework.org/schema/context6 xmlns:mvchttp://www.springframework.org/schema/mvc 7 xmlns:txhttp://www.springframework.org/schema/tx 8 xsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 9 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 11 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 12 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 13 14 15 context:component-scan base-packagecom.baidu.*.service/ 16 17 18 !-- 配置事务管理器 -- 19 bean idtransactionManager classorg.springframework.jdbc.datasource.DataSourceTransactionManager 20 property namedataSource refdataSource / 21 /bean 22 23 24 !-- 事务处理 -- 25 26 aop:config 27 aop:pointcut expressionexecution(* com.baidu.*.service..*.*(..)) idtrPointcut/ 28 aop:advisor advice-reftrAdvice pointcut-reftrPointcut/ 29 /aop:config 30 31 tx:advice idtrAdvice transaction-managertransactionManager 32 tx:attributes 33 tx:method namedoAddED* propagationREQUIRED/ 34 tx:method nameadd* propagationREQUIRED/ 35 tx:method nameinsert* propagationREQUIRED/ 36 tx:method nameupdate* propagationREQUIRED/ 37 tx:method namedel* propagationREQUIRED/ 38 tx:method nameget* propagationREQUIRED/ 39 tx:method name* propagationREQUIRED read-onlytrue/ 40 /tx:attributes 41 /tx:advice 42 43 44 45 46 /beans 3.demo_dao 下的 src/main/resources 中放入spring.xml 1 ?xml version1.0 encodingUTF-8?2 beans xmlnshttp://www.springframework.org/schema/beans3 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance4 xmlns:aophttp://www.springframework.org/schema/aop5 xmlns:contexthttp://www.springframework.org/schema/context6 xmlns:mvchttp://www.springframework.org/schema/mvc 7 xmlns:txhttp://www.springframework.org/schema/tx 8 xsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 9 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 11 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 12 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 13 14 15 context:property-placeholder locationclasspath*:conf/**/jdbc.properties/ 16 bean iddataSource classorg.apache.commons.dbcp.BasicDataSource destroy-methodclose 17 property namedriverClassName value${jdbc.driverClassName}/ 18 property nameurl value${jdbc.url}/ 19 property nameusername value${jdbc.username}/ 20 property namepassword value${jdbc.password}/ 21 !-- 连接池启动时的初始值 -- 22 property nameinitialSize value${initialSize}/ 23 !-- 连接池的最大值 -- 24 property namemaxActive value${maxActive}/ 25 !-- 最大空闲值.当经过一个高峰时间后连接池可以慢慢将已经用不到的连接慢慢释放一部分一直减少到maxIdle为止 -- 26 property namemaxIdle value${maxIdle}/ 27 !-- 最小空闲值.当空闲的连接数少于阀值时连接池就会预申请去一些连接以免洪峰来时来不及申请 -- 28 property nameminIdle value${minIdle}/ 29 !-- 是否启用超时自动移除空闲连接 -- 30 property name removeAbandoned valuefalse/property 31 !-- 超时时间(以秒数为单位) -- 32 property nameremoveAbandonedTimeout value${removeAbandonedTimeout}/ 33 !-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -- 34 property namemaxWait value${maxWait}/ 35 36 /bean 37 38 39 40 bean idsqlSessionFactory classorg.mybatis.spring.SqlSessionFactoryBean 41 property namedataSource refdataSource/property 42 !-- property nameconfigLocation 43 valueclasspath:mybatis-config.xml/value 44 /property -- 45 !-- 配置实体别名 -- 46 property nametypeAliasesPackage 47 valuecom.baidu.entity/value 48 /property 49 !-- 配置映射文件加载 -- 50 property namemapperLocations 51 valueclasspath*:com/baidu/entity/*.xml/value 52 /property 53 /bean 54 55 56 57 !-- 在spring里使用org.mybatis.spring.mapper.MapperScannerConfigurer 进行自动扫描的时候 58 设置了sqlSessionFactory 的话可能会导致PropertyPlaceholderConfigurer失效也就是用${jdbc.username} 59 这样之类的表达式将无法获取到properties文件里的内容。 导致这一原因是因为MapperScannerConigurer 60 实际是在解析加载bean定义阶段的这个时候要是设置sqlSessionFactory的话会导致提前初始化一些类这个时候 61 PropertyPlaceholderConfigurer还没来得及替换定义中的变量导致把表达式当作字符串复制了。 但如果不设置 62 sqlSessionFactory 属性的话就必须要保证sessionFactory在spring中名称一定要是sqlSessionFactory 63 否则就无法自动注入。 64 解决方案 65 改用sqlSessionFactoryBeanName注入就没有问题(不要使用sqlSessionFactory属性注入 66 使用sqlSessionFactoryBeanName注入)因为这时不会立即初始化sqlSessionFactory,传入的只是名字 67 非bean所以不会引发提前初始化问题。 68 -- 69 70 !-- 配置bean 自动扫描所有mapper 自动给Mapper接口产生代理类对象 并且给代理对象注入SqlSessionFactory-- 71 bean classorg.mybatis.spring.mapper.MapperScannerConfigurer 72 property namebasePackage 73 valuecom.baidu.dao/value 74 /property 75 76 property namesqlSessionFactoryBeanName valuesqlSessionFactory/property 77 !-- property namesqlSessionFactory refsqlSessionFactory/property -- 78 /bean 79 80 /beans 4.在pom.xml配置文件中 配置依赖管理 1.demo_web 中的 pom.xml 加入 1 dependencies
2 dependency
3 groupIdcom.baidu/groupId
4 artifactIddemo_service/artifactId
5 version${project.version}/version
6 /dependency 7 /dependencies 2.demo_service 中的pom.xml 加入 1 dependencies
2 dependency
3 groupIdcom.baidu/groupId
4 artifactIddemo_dao/artifactId
5 version${project.version}/version
6 /dependency 7 /dependencies 3.demo_dao 中的pom.xml 加入 1 dependencies
2 dependency
3 groupIdcom.baidu/groupId
4 artifactIddemo_domain/artifactId
5 version${project.version}/version
6 /dependency 7 /dependencies 4.demo_parent 中的pom.xml 加入 1 !-- 管理子项目的xml文件和资源文件 --2 build3 resources4 resource5 directorysrc/main/java/directory6 includes 7 include**/*.xml/include 8 /includes 9 /resource 10 resource 11 directorysrc/main/resources/directory 12 includes 13 include**/*.xml/include 14 include**/*.properties/include 15 /includes 16 /resource 17 /resources 18 /build 1 !-- 父类里包含的模块 --
2 modules
3 module../demo_dao/module
4 module../demo_service/module
5 module../demo_web/module
6 module../demo_domain/module 7 /modules 1 project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance2 xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd3 modelVersion4.0.0/modelVersion4 5 artifactIddemo_domain/artifactId6 packagingjar/packaging 7 8 namedemo_domain/name 9 urlhttp://maven.apache.org/url 10 11 !-- 依赖父类 demo_parent -- 12 parent 13 groupIdcom.baidu/groupId 14 artifactIddemo_parent/artifactId 15 version0.0.1-SNAPSHOT/version 16 relativePath../garden_partent/pom.xml/relativePath 17 /parent 18 19 properties 20 project.build.sourceEncodingUTF-8/project.build.sourceEncoding 21 /properties 22 23 24 / 六。 右键 - run as -Maven install 可以自动执行编译测试打包等构建过程 进行测试 项目 搭建完成。转载于:https://www.cnblogs.com/majun520/p/5039346.html