背景:淘淘商城項目的環境搭建
說明:采用SpringMVC+Spring+Mybatis+Mysql+Maven+Svn結構搭建,在開發之中可以參考其結構和搭建步驟去搭建實際的工程項目
工程結構簡圖:
項目結構:
---------------------------------------------【 以下是項目的搭建過程 】----------------------------------------------------
創建數據庫:
taotao-parent:
這是父工程,其他子工程都要繼承它,是個POM聚合工程,這里集中定義了jar包的版本號,但并不是實際依賴,其子工程的版本要從這里依賴傳遞,便于維護。
File--New--Maven--Maven Project
Next--Finish
修改pom.xml:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.taotao
taotao-parent
0.0.1-SNAPSHOT
pom
4.12
4.1.3.RELEASE
3.2.8
1.2.2
1.2.15
5.1.32
1.6.4
2.4.2
1.0.9
4.3.5
1.2
2.5
2.0
2.5
3.3.2
1.3.2
3.3
3.4.2-fix
0.9.1
1.3.1
2.7.2
4.10.3
joda-time
joda-time
${joda-time.version}
org.apache.commons
commons-lang3
${commons-lang3.version}
org.apache.commons
commons-io
${commons-io.version}
commons-net
commons-net
${commons-net.version}
com.fasterxml.jackson.core
jackson-databind
${jackson.version}
org.apache.httpcomponents
httpclient
${httpclient.version}
junit
junit
${junit.version}
test
org.slf4j
slf4j-log4j12
${slf4j.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
${mybatis.spring.version}
com.github.miemiedev
mybatis-paginator
${mybatis.paginator.version}
com.github.pagehelper
pagehelper
${pagehelper.version}
mysql
mysql-connector-java
${mysql.version}
com.alibaba
druid
${druid.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-aspects
${spring.version}
jstl
jstl
${jstl.version}
javax.servlet
servlet-api
${servlet-api.version}
provided
javax.servlet
jsp-api
${jsp-api.version}
provided
commons-fileupload
commons-fileupload
${commons-fileupload.version}
redis.clients
jedis
${jedis.version}
org.apache.solr
solr-solrj
${solrj.version}
${project.artifactId}
org.apache.maven.plugins
maven-resources-plugin
2.7
UTF-8
org.apache.maven.plugins
maven-compiler-plugin
3.2
1.7
1.7
UTF-8
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
taotao-common:
同理創建taotao-common,這個工程是工具工程,提供通用的工具類,是個jar工程,此工程要繼承父工程taotao-parent
修改pom.xml:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.taotao
taotao-parent
0.0.1-SNAPSHOT
com.taotao
taotao-common
0.0.1-SNAPSHOT
joda-time
joda-time
org.apache.commons
commons-lang3
org.apache.commons
commons-io
commons-net
commons-net
com.fasterxml.jackson.core
jackson-databind
org.apache.httpcomponents
httpclient
junit
junit
test
org.slf4j
slf4j-log4j12
com.jcraft
jsch
0.1.49
taotao-manager:
這是個POM聚合工程,包括controller/service/mapper工程,此工程要繼承父工程taotao-parent,同時要依賴taotao-common工具工程
修改pom.xml:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.taotao
taotao-parent
0.0.1-SNAPSHOT
com.taotao
taotao-manager
0.0.1-SNAPSHOT
pom
com.taotao
taotao-common
0.0.1-SNAPSHOT
taotao-manager-pojo
taotao-manager-mapper
taotao-manager-service
taotao-manager-web
org.apache.tomcat.maven
tomcat7-maven-plugin
8080
/
taotao-manager-pojo:
這是POJO,是個jar工程,不依賴于任何模塊
在taotao-manager工程上右鍵,New--Other--Maven Module--Next--Finish
pom.xml:無需修改
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.taotao
taotao-manager
0.0.1-SNAPSHOT
com.taotao
taotao-manager-pojo
0.0.1-SNAPSHOT
在src/main/java目錄下,創建package:com.taotao.pojo,使用逆向工程生成POJO類和Mapper接口以及Mapper映射文件,將所有POJO類拷貝到這個package下
taotao-manager-mapper:
同理創建taotao-manager-mapper,這是jar工程,是mapper層,依賴pojo,和數據庫交互,在src/main/java下創建package:com.taotao.mapper,
將逆向工程生成的Mapper接口和Mapper映射文件拷貝至這個package下
修改pom.xml:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.taotao
taotao-manager
0.0.1-SNAPSHOT
com.taotao
taotao-manager-mapper
0.0.1-SNAPSHOT
com.taotao
taotao-manager-pojo
0.0.1-SNAPSHOT
org.mybatis
mybatis
org.mybatis
mybatis-spring
com.github.miemiedev
mybatis-paginator
com.github.pagehelper
pagehelper
mysql
mysql-connector-java
com.alibaba
druid
src/main/java
**/*.properties
**/*.xml
false
taotao-manager-service
同理創建taotao-manager-service,這是jar工程,是service層,調用mapper層
修改pom.xml:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.taotao
taotao-manager
0.0.1-SNAPSHOT
com.taotao
taotao-manager-service
0.0.1-SNAPSHOT
com.taotao
taotao-manager-mapper
0.0.1-SNAPSHOT
org.springframework
spring-context
org.springframework
spring-beans
org.springframework
spring-webmvc
org.springframework
spring-jdbc
org.springframework
spring-aspects
在src/main/java目錄下創建package:com.taotao.service以及com.taotao.service.impl
taotao-manager-web:
同理創建taotao-manager-web,這是表現層Controller,是個war工程,調用service工程
修改pom.xml:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.taotao
taotao-manager
0.0.1-SNAPSHOT
com.taotao
taotao-manager-web
0.0.1-SNAPSHOT
war
com.taotao
taotao-manager-service
0.0.1-SNAPSHOT
jstl
jstl
javax.servlet
servlet-api
provided
javax.servlet
jsp-api
provided
commons-fileupload
commons-fileupload
以下步驟在開發之中根據實際情況調整, 可以先搭建好環境之后再開發controller(service層也一樣)
在src/main/resources目錄下創建三個fold:mybatis、resource、spring
在resource下創建db.properties:
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/數據庫名?useUnicode=true&characterEncoding=utf-8
jdbc.username = 數據庫賬號
jdbc.password =數據庫密碼
在mybatis下創建SqlMapConfig.xml:(這個文件雖然暫時沒有寫入什么東西,但是要有這個文件,否則工程結構不完整,就會報錯)
/p>
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
在spring下創建各個整合的配置文件:
applicationContext-dao.xml:
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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
destroy-method="close">
applicationContext-service.xml:
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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
applicationContext-trans.xml:
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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
pointcut="execution(* com.taotao.service.*.*(..))" />
springmvc.xml:
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: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">
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
在webapp下創建WEB-INF目錄,其下創建web.xml,并把一些靜態資源文件和頁面(css/js/jsp)拷貝到WEB-INF下
web.xml:
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="taotao" version="2.5">
taotao-manager
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
contextConfigLocation
classpath:spring/applicationContext-*.xml
org.springframework.web.context.ContextLoaderListener
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
CharacterEncodingFilter
/*
taotao-manager
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/springmvc.xml
1
taotao-manager
/
在webapp下創建index.jsp:
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
首頁測試尊敬的用戶,如果您看到此頁面,說明taotao-manager正在運行...
到此為止,整個工程的環境已經搭建完畢,可以做測試(在實際開發中要步步為營,每次搭建完一個模塊就要做一次測試):
將各個工程及其繼承的工程按順序install到本地倉庫,然后啟動在taotao-manager工程上右鍵Run As--Maven Build,輸入clean tomcat7:run,運行
如果測試通過,就將項目提交到svn上,這樣,其他組員就可以從svn上檢出項目進行開發
-------------------------------------------以下是將項目提交到svn已經從svn檢出項目的步驟-------------------------------------------------------
提交到svn的順序:taotao-parent, taotao-common, taotao-manager(其子工程不需要提交,因為父工程已經包含其子工程),
以taotao-parent為例,其他同理:
右鍵taotao-parent--Team--Share Project...--SVN--Next--創建新的資源庫位置(第一個工程選這個,后面的工程選擇“使用已有資源庫位置”),
打開VisualSVN Server Manager,在Repositories下右鍵選擇Create New Repository...,為這個項目的倉庫取個名字(如taotao-shop),下一步,Create
就可以創建一個項目的本地倉庫,包含三個文件夾:branches,tags,trunk(這個目錄用于存放java代碼),
右鍵剛剛創建的倉庫,選擇Copy URL to Clipboard,將倉庫的URL復制,粘貼到以下的URL文本框中:
Next
點擊“瀏覽”--路徑指向項目倉庫的trunk目錄,按提示操作,Finish即可,至此,項目并沒有真正地上傳到svn上,只是建立了項目和svn的關系。
把除了src及其子目錄、pom.xml之外的文件全部 ignore 掉(如果把target/.property/.setting等文件也上傳到svn,那么其他人從svn上down下來的項目可能會覆蓋掉他自己本地的文件,這些文件都是個性化的文件,不要上傳到svn!),有些可能要按照順序操作,具體請百度,然后提交到svn,如果沖突就先更新再提交,
其他的項目一樣操作。
這樣項目就全部提交到svn上了, 建議本地的項目(就是您看到的myeclipse中的所有項目)全部都刪除(刪除的時候把本地磁盤的選項也勾上,表示把磁盤上的也刪除),然后從svn上把項目都down下來,下載順序:taotao-parent---taotao-common---taotao-manager(這是聚合工程),然后你會發現taotao-manager下的子工程都沒有,右鍵taotao-manager這個聚合工程--Import--Maven--Existing Maven Projects--Next
路徑指向聚合工程taotao-manager的目錄,就會自動將其子工程列出來,全選,Next,Finish,就會把所有的子工程都導入進來,你會發現taotao-parent, taotao-common, taotao-manager這三個工程都不是Maven工程,需要將其一 一轉成Maven工程,步驟請參考以下鏈接:
http://www.cnblogs.com/josephcnblog/articles/6616563.html
將項目都轉成maven工程之后,至此,整個項目的就搭建完畢!可以進行其他功能的開發了
------------------------------------------以下是一個小demo的開發步驟,其他業務模塊一樣操作--------------------------------------------------
在com.taotao.service下創建Service接口:ItemService.java
package com.taotao.service;
import com.taotao.pojo.TbItem;
/**
* 商品Service
*/
public interface ItemService {
TbItem getItemById(long itemId);
}
在com.taotao.service.impl下創建Service接口實現類:ItemServiceImpl.java
package com.taotao.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.service.ItemService;
/**
* 商品Service實現類
*/
@Service
public class ItemServiceImpl implements ItemService {
// 注入ItemMapper
@Autowired
private TbItemMapper itemMapper;
/**
* 根據商品Id查詢商品對象
*/
@Override
public TbItem getItemById(long itemId) {
TbItem item = itemMapper.selectByPrimaryKey(itemId);
return item;
}
}
在src/main/java目錄下創建package:com.taotao.controller,在此package下創建ItemController.java
package com.taotao.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.taotao.pojo.TbItem;
import com.taotao.service.ItemService;
/**
* 商品Controller
*/
@Controller
@RequestMapping("/item")
public class ItemController {
// 注入ItemService
@Autowired
private ItemService itemService;
@RequestMapping("/{itemId}")
@ResponseBody
public TbItem getItemById(@PathVariable Long itemId) {
TbItem item = itemService.getItemById(itemId);
return item;
}
}
下面是測試,右鍵taotao-manager--Run As--Maven Build,啟動tomcat之后,在瀏覽器地址欄輸入http://localhost:8080/item/536563
其中, 后面的數字是要查詢的商品的 id , 回車:
返回json格式數據, 測試通過!
繼續開發其他的業務模塊吧!