【SSM电商项目实战2】利用maven整合spring、springMCV和mybatis
2017-06-14 18:40 核桃不是桃 阅读(692) 评论(0) 收藏 举报关于Maven的简述
开始时搞不清楚Maven是什么,简单理解就是一个工具,工具就是为了给我们提供方便,提高我们生产力了,那么maven是怎么提高生产力的呢,就这这个思路再来了解maven就很好理解了。
- 首先,maven帮我们管理项目依赖的jar包。原理就是maven通过一个叫做pom.xml的配置文件帮我们管理jar包。pom(project object Model项目对象模型)
- 另一个问题就是,关于项目的目录结构没有统一的标准,而maven定义了一种标准。
总之maven的作用就是两个,统一管理jar包和统一开发规范。这在团队开发中尤其重要。
亲爱的pom.xml文件
新建一个maven项目后,会有一个pom文件,最初的时候是这样的:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xrq.withmaven</groupId> <artifactId>mmall</artifactId> <version>0.0.1-SNAPSHOT</version> <build/> </project>
这里定义了maven的模型版本号(modelversion)公司或组织名(groupId)项目名(artifactId)和版本号(version)
除此之外,还有一些其他的元素,咱们整理一下
packing元素用来定义项目打包的类型,可以是jar、war、rar、ear、pom,默认是jar。在这里我们使用war。
dependencies和dependency,Maven中依赖的每一个jar包就是一个dependency,这里理解一下maven管理jar包的原理,在用户每次修改pom之后,maven会检查pom中的元素,然后核对依赖,如果有新加入的依赖,就会在用户的电脑上的一个本地仓库中拿出这个依赖,本地仓库没有,就到maven的中央仓库(远程的仓库)上面去拿,然后悄悄地帮我们下载了。是不是感觉很省心。我就是这么觉得的。
比如说我们的项目依赖了很多jar,pom文件大概是这样的:
1 <dependencies> 2 3 <dependency> 4 <groupId>org.apache.tomcat</groupId> 5 <artifactId>tomcat-servlet-api</artifactId> 6 <version>7.0.64</version> 7 </dependency> 8 9 <dependency> 10 <groupId>org.springframework</groupId> 11 <artifactId>spring-webmvc</artifactId> 12 <version>${org.springframework.version}</version> 13 </dependency> 14 15 <dependency> 16 <groupId>org.springframework</groupId> 17 <artifactId>spring-oxm</artifactId> 18 <version>${org.springframework.version}</version> 19 </dependency> 20 21 <dependency> 22 <groupId>org.springframework</groupId> 23 <artifactId>spring-jdbc</artifactId> 24 <version>${org.springframework.version}</version> 25 </dependency> 26 27 <dependency> 28 <groupId>org.springframework</groupId> 29 <artifactId>spring-tx</artifactId> 30 <version>${org.springframework.version}</version> 31 </dependency> 32 33 <dependency> 34 <groupId>org.springframework</groupId> 35 <artifactId>spring-test</artifactId> 36 <version>${org.springframework.version}</version> 37 </dependency> 38 39 40 <dependency> 41 <groupId>org.aspectj</groupId> 42 <artifactId>aspectjweaver</artifactId> 43 <version>1.7.3</version> 44 </dependency> 45 46 <dependency> 47 <groupId>org.mybatis</groupId> 48 <artifactId>mybatis-spring</artifactId> 49 <version>${org.mybatis.spring.version}</version> 50 </dependency> 51 <dependency> 52 <groupId>org.mybatis</groupId> 53 <artifactId>mybatis</artifactId> 54 <version>${org.mybatis.version}</version> 55 </dependency> 56 57 <dependency> 58 <groupId>org.aspectj</groupId> 59 <artifactId>aspectjrt</artifactId> 60 <version>1.6.11</version> 61 </dependency> 62 63 <dependency> 64 <groupId>org.codehaus.jackson</groupId> 65 <artifactId>jackson-mapper-asl</artifactId> 66 <version>1.9.12</version> 67 </dependency> 68 69 <dependency> 70 <groupId>commons-dbcp</groupId> 71 <artifactId>commons-dbcp</artifactId> 72 <version>1.4</version> 73 <!--<scope>runtime</scope>--> 74 </dependency> 75 76 77 <dependency> 78 <groupId>ch.qos.logback</groupId> 79 <artifactId>logback-classic</artifactId> 80 <version>1.1.2</version> 81 <scope>compile</scope> 82 </dependency> 83 <dependency> 84 <groupId>ch.qos.logback</groupId> 85 <artifactId>logback-core</artifactId> 86 <version>1.1.2</version> 87 <scope>compile</scope> 88 </dependency> 89 90 <dependency> 91 <groupId>mysql</groupId> 92 <artifactId>mysql-connector-java</artifactId> 93 <version>5.1.6</version> 94 </dependency> 95 96 <dependency> 97 <groupId>com.google.guava</groupId> 98 <artifactId>guava</artifactId> 99 <version>20.0</version> 100 </dependency> 101 102 103 <dependency> 104 <groupId>org.apache.commons</groupId> 105 <artifactId>commons-lang3</artifactId> 106 <version>3.5</version> 107 </dependency> 108 109 110 <dependency> 111 <groupId>commons-collections</groupId> 112 <artifactId>commons-collections</artifactId> 113 <version>3.2.1</version> 114 </dependency> 115 116 117 <dependency> 118 <groupId>junit</groupId> 119 <artifactId>junit</artifactId> 120 <version>4.12</version> 121 <!--<scope>test</scope>--> 122 </dependency> 123 124 <dependency> 125 <groupId>joda-time</groupId> 126 <artifactId>joda-time</artifactId> 127 <version>2.3</version> 128 </dependency> 129 130 131 <!-- id加密解密 --> 132 <dependency> 133 <groupId>org.hashids</groupId> 134 <artifactId>hashids</artifactId> 135 <version>1.0.1</version> 136 </dependency> 137 138 139 <!-- ftpclient --> 140 <dependency> 141 <groupId>commons-net</groupId> 142 <artifactId>commons-net</artifactId> 143 <version>3.1</version> 144 </dependency> 145 146 <!-- file upload --> 147 148 <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> 149 <dependency> 150 <groupId>commons-fileupload</groupId> 151 <artifactId>commons-fileupload</artifactId> 152 <version>1.2.2</version> 153 </dependency> 154 155 <dependency> 156 <groupId>commons-io</groupId> 157 <artifactId>commons-io</artifactId> 158 <version>2.0.1</version> 159 </dependency> 160 161 162 163 164 <!-- mybatis pager --> 165 166 <dependency> 167 <groupId>com.github.pagehelper</groupId> 168 <artifactId>pagehelper</artifactId> 169 <version>4.1.0</version> 170 </dependency> 171 172 <dependency> 173 <groupId>com.github.miemiedev</groupId> 174 <artifactId>mybatis-paginator</artifactId> 175 <version>1.2.17</version> 176 </dependency> 177 178 <dependency> 179 <groupId>com.github.jsqlparser</groupId> 180 <artifactId>jsqlparser</artifactId> 181 <version>0.9.4</version> 182 </dependency> 183 184 185 <!-- alipay --> 186 <dependency> 187 <groupId>commons-codec</groupId> 188 <artifactId>commons-codec</artifactId> 189 <version>1.10</version> 190 </dependency> 191 <dependency> 192 <groupId>commons-configuration</groupId> 193 <artifactId>commons-configuration</artifactId> 194 <version>1.10</version> 195 </dependency> 196 <dependency> 197 <groupId>commons-lang</groupId> 198 <artifactId>commons-lang</artifactId> 199 <version>2.6</version> 200 </dependency> 201 <dependency> 202 <groupId>commons-logging</groupId> 203 <artifactId>commons-logging</artifactId> 204 <version>1.1.1</version> 205 </dependency> 206 <dependency> 207 <groupId>com.google.zxing</groupId> 208 <artifactId>core</artifactId> 209 <version>2.1</version> 210 </dependency> 211 <dependency> 212 <groupId>com.google.code.gson</groupId> 213 <artifactId>gson</artifactId> 214 <version>2.3.1</version> 215 </dependency> 216 <dependency> 217 <groupId>org.hamcrest</groupId> 218 <artifactId>hamcrest-core</artifactId> 219 <version>1.3</version> 220 </dependency> 221 222 <dependency> 223 <groupId>redis.clients</groupId> 224 <artifactId>jedis</artifactId> 225 <version>2.9.0</version> 226 </dependency> 227 </dependencies>
以上就是我们项目依赖的全部jar,maven是通过什么去中央仓库准确的找到对应的jar包的?答案就是上文中说得groupId、artifacId、version这三个属性,类似我们邮寄快递时候的邮编地址手机号。嗯,就这么简单。
properties定义了一些配置属性,这里我们比较关注的是编码,统一设置成utf-8,这样就不会出现中文乱码问题了。
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.encoding>UTF-8</maven.compiler.encoding> <org.springframework.version>4.0.0.RELEASE</org.springframework.version> <org.mybatis.version>3.4.1</org.mybatis.version> <org.mybatis.spring.version>1.3.0</org.mybatis.spring.version> </properties>
build表示和构建相关的配置,比如finalName就是构建后的项目名。而plugins就是项目中用到的插件名,我们项目中主要使用了mybatis.generator,具体的在之后用到的时候再讲。
<build> <finalName>mmall</finalName> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> <!-- geelynote maven的核心插件之-complier插件默认只支持编译Java 1.4,因此需要加上支持高版本jre的配置,在pom.xml里面加上 增加编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> <compilerArguments> <extdirs>${project.basedir}/src/main/webapp/WEB-INF/lib</extdirs> </compilerArguments> </configuration> </plugin> </plugins> </build>
项目目录结构
用idea新建一个maven项目。这里是根据我这个项目的整体架构来创建目录结构的。

common存放一些诸如常量类一类的文件util存放一些工具类。这两个包为整个项目提供支持。controller调用service再访问dao,最后dao访问数据库。数据库中的数据定义为pojo,pojo和vo进行组合之后返给controller发送给前端展示。大概就是这么个流程。
浙公网安备 33010602011771号