簡單談一下maven搭建 ssm 項目 (使用數據庫oracle,比 mysql 麻煩一點,所以這里談一下)
在創建maven 的web項目時,常常會缺了main/java , main/test 兩個文件夾。
解決方法:
① : 在項目上右鍵選擇properties,然后點擊java build path,在Librarys下,編輯JRE System Library,選擇workspace default jre就可以了。 (推薦使用這種)
② :手動創建 目錄。切換視圖采用Navigator視圖,直接在src/main目錄下建立 Java目錄。
項目目錄結構:
重要的配置文件:
對象模型配置文件: pom.xml
Spring的配置文件:applicationContext.xml
spring MVC配置文件: springmvc.xml
數據庫配置文件: jdbc.properties
日志配置文件: log4j.properties
mybatis配置文件: mybatis-config.xml
網絡程序配置文件:web.xml
首先配置pom.xml
pom.xml 主要描述了項目的maven坐標,依賴關系,自動引入jar包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
< 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/maven-v4_0_0.xsd" > < modelVersion >4.0.0</ modelVersion > < groupId >com.krry</ groupId > < version >0.0.1-SNAPSHOT</ version > < name >maven_SSM</ name > < url >http://maven.apache.org</ url > < dependencies > <!--引入junit --> < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >4.11</ version > < scope >test</ scope > </ dependency > <!--引入servlet --> < dependency > < groupId >javax.servlet</ groupId > < artifactId >servlet-api</ artifactId > < version >3.0-alpha-1</ version > < scope >provided</ scope > </ dependency > <!--引入jstl的包 --> < dependency > < groupId >jstl</ groupId > < artifactId >jstl</ artifactId > < version >1.2</ version > < scope >provided</ scope > </ dependency > <!--引入jsp的編譯依賴 --> < dependency > < groupId >javax.servlet.jsp</ groupId > < artifactId >jsp-api</ artifactId > < version >2.2</ version > < scope >provided</ scope > </ dependency > <!--引入log4j --> < dependency > < groupId >log4j</ groupId > < artifactId >log4j</ artifactId > < version >1.2.17</ version > </ dependency > <!--spring springmvc mybatis --> <!-- spring和springmvc相關的構建 jar --> < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-context</ artifactId > < version >4.2.1.RELEASE</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-jdbc</ artifactId > < version >4.2.1.RELEASE</ version > </ dependency > <!-- springmvc相關 --> < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-webmvc</ artifactId > < version >4.2.1.RELEASE</ version > </ dependency > <!--springmvc需要用到json的轉換包 jackson --> < dependency > < groupId >com.fasterxml.jackson.core</ groupId > < artifactId >jackson-core</ artifactId > < version >2.5.4</ version > </ dependency > < dependency > < groupId >com.fasterxml.jackson.core</ groupId > < artifactId >jackson-annotations</ artifactId > < version >2.5.4</ version > </ dependency > < dependency > < groupId >com.fasterxml.jackson.core</ groupId > < artifactId >jackson-databind</ artifactId > < version >2.5.4</ version > </ dependency > <!--JSR303 后臺校驗 hibernate validator --> < dependency > < groupId >org.hibernate</ groupId > < artifactId >hibernate-validator</ artifactId > < version >5.1.1.Final</ version > </ dependency > <!--上傳文件相關的jar包 --> < dependency > < groupId >commons-io</ groupId > < artifactId >commons-io</ artifactId > < version >2.4</ version > </ dependency > < dependency > < groupId >commons-fileupload</ groupId > < artifactId >commons-fileupload</ artifactId > < version >1.3.1</ version > </ dependency > < dependency > < groupId >org.apache.commons</ groupId > < artifactId >commons-lang3</ artifactId > < version >3.3.2</ version > </ dependency > <!--跟加密算法相關的codeC --> < dependency > < groupId >commons-codec</ groupId > < artifactId >commons-codec</ artifactId > < version >1.9</ version > </ dependency > <!--orm或者jdbc組件需要用到的jar包 mybatis --> <!--oracle數據庫驅動 --> < dependency > < groupId >com.oracle</ groupId > < artifactId >ojdbc6</ artifactId > < version >12.1.0.2.0</ version > </ dependency > <!--mysql數據庫驅動 (這里不用,用的是上面的oracle驅動) --> <!-- <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.8</version> <scope>runtime</scope> </dependency> --> <!-- proxool連接池 --> < dependency > < groupId >com.cloudhopper.proxool</ groupId > < artifactId >proxool</ artifactId > < version >0.9.1</ version > </ dependency > < dependency > < groupId >com.cloudhopper.proxool</ groupId > < artifactId >proxool-cglib</ artifactId > < version >0.9.1</ version > </ dependency > <!--引入mybatis需要的jar包 --> < dependency > < groupId >org.mybatis</ groupId > < artifactId >mybatis</ artifactId > < version >3.3.1</ version > </ dependency > < dependency > < groupId >org.mybatis</ groupId > < artifactId >mybatis-spring</ artifactId > < version >1.2.4</ version > </ dependency > <!-- 分頁管理需要的jar包,這里沒用到 --> < dependency > < groupId >com.github.pagehelper</ groupId > < artifactId >pagehelper</ artifactId > < version >4.2.1</ version > </ dependency > </ dependencies > < build > < plugins > < plugin > < artifactId >maven-compiler-plugin</ artifactId > < configuration > < source >1.7</ source > < target >1.7</ target > </ configuration > </ plugin > < plugin > < artifactId >maven-war-plugin</ artifactId > < version >2.4</ version > < configuration > < version >3.0</ version > </ configuration > </ plugin > </ plugins > < finalName >maven_SSM</ finalName > </ build > </ project > |
這里說一下maven工程利用pom.xml導入oracle驅動包的問題:
由于Oracle授權問題,Maven不提供Oracle JDBC driver,為了在Maven項目中應用Oracle JDBC driver,必須手動添加到本地倉庫。
如果電腦中已經裝有Oracle數據庫,則在安裝路徑下有數據庫的驅動程序,可以直接用。D:\Oracle\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib
也可以直接到Oracle官網上下載Oracle數據庫驅動, 使用SQL語句查詢數據庫驅動的版本: SELECT * FROM v$instance
然后確定版本下載:http://www.oracle.com/technetwork/database/features/jdbc/default-2280470.html
打開windows的命令行界面,進入驅動包ojdbc6的目錄,然后運行:
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=12.1.0.2.0 -Dpackaging=jar -Dfile=ojdbc6.jar
顯示"BUILD SUCCESS" 成功,就會自動導入你的maven本地倉庫。
然后就可以在maven項目里添加dependency,各坐標對應上面這個命令的個元素,如下:
1
2
3
4
5
|
< dependency > < groupId >com.oracle</ groupId > < artifactId >ojdbc6</ artifactId > < version >12.1.0.2.0</ version > </ dependency > |
Spring的配置文件:applicationContext.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
<? 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:aop = "http://www.springframework.org/schema/aop" xmlns:context = "http://www.springframework.org/schema/context" xmlns:tx = "http://www.springframework.org/schema/tx" xmlns:util = "http://www.springframework.org/schema/util" xmlns:p = "http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd "> <!-- 配置包掃描 --> < context:component-scan base-package = "com.krry" ></ context:component-scan > <!-- 導入外部資源文件 --> <!-- <context:property-placeholder location="classpath:jdbc.properties" /> --> < bean class = "com.krry.core.des.EncryptPropertyPlaceholderConfigurer" p:location = "classpath:jdbc.properties" p:fileEncoding = "utf-8" /> <!-- proxool連接池 --> < bean id = "dataSource" class = "org.logicalcobwebs.proxool.ProxoolDataSource" > <!-- 驅動的名字,mysql --> < property name = "driver" value = "${db.driver}" ></ property > <!--proxool 的 url連接串,這個必須確定用戶名和密碼 --> < property name = "driverUrl" value = "${db.url}" ></ property > <!-- 用戶名(proxool沒有使用,但是不能沒有) --> < property name = "user" value = "${db.username}" ></ property > <!-- 密碼(proxool沒有使用,但是不能沒有) --> < property name = "password" value = "${db.password}" ></ property > <!-- proxool自動偵察各個連接狀態的時間間隔(毫秒),偵察到空閑的連接就馬上回收,超時的銷毀 現在設置為4秒) --> < property name = "houseKeepingSleepTime" value = "3000" ></ property > <!-- 自動檢查連接是否斷掉開關 --> < property name = "testBeforeUse" value = "true" ></ property > <!-- 如果發現了空閑的數據庫連接.house keeper 將會用這個語句來測試.這個語句最好非常快的被執行.如果沒有定義,測試過程將會被忽略 --> < property name = "houseKeepingTestSql" value = "SELECT count(1) from dual" ></ property > <!-- 如果housekeeper 檢測到某個線程的活動時間大于這個數值.它將會殺掉這個線程.所以確認一下你的服務器的帶寬.然后定一個合適的值.默認是5分鐘. 現在設置 10 秒--> < property name = "maximumActiveTime" value = "10000" ></ property > <!-- 最少保持的空閑連接數 (現在設置20個) --> < property name = "prototypeCount" value = "20" ></ property > <!-- 最大連接數 (現在設置100個) --> < property name = "maximumConnectionCount" value = "200" ></ property > <!-- 最小連接數 (現在設置50個) --> < property name = "minimumConnectionCount" value = "50" ></ property > <!-- 如果為true,那么每個被執行的SQL語句將會在執行期被log記錄(DEBUG LEVEL).你也可以注冊一個ConnectionListener (參看ProxoolFacade)得到這些信息. --> < property name = "trace" value = "false" ></ property > < property name = "verbose" value = "true" ></ property > </ bean > <!-- 注冊事務管理器 --> < bean id = "txMgr" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > < property name = "dataSource" ref = "dataSource" ></ property > </ bean > <!-- 開啟事務注解驅動 --> < tx:annotation-driven transaction-manager = "txMgr" /> <!-- 配置mybatis的sqlSessionFactory --> < bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > < property name = "dataSource" ref = "dataSource" ></ property > < property name = "configLocation" value = "classpath:mybatis-config.xml" ></ property > </ bean > <!-- 配置可以整體掃描Mapper的一個掃描器 --> < bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" > <!--如果有多個報路徑,用逗號分開即可 --> < property name = "basePackage" value = "com.krry.mapper" ></ property > < property name = "sqlSessionFactoryBeanName" value = "sqlSessionFactory" ></ property > </ bean > </ beans > |
spring MVC配置文件:springmvc.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
<? 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:util = "http://www.springframework.org/schema/util" xmlns:mvc = "http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- 開啟注解模式驅動 --> < bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- 開啟mvc的注解模式 user 還會注冊一個ConversionService 子類FormattingConversionServiceFactoryBean--> < mvc:annotation-driven > < mvc:message-converters register-defaults = "true" > < bean class = "com.krry.core.UTF8StringHttpMessageConverter" > < property name = "supportedMediaTypes" > < list > < value >text/plain;charset=UTF-8</ value > < value >text/html;charset=UTF-8</ value > </ list > </ property > </ bean > < bean class = "org.springframework.http.converter.BufferedImageHttpMessageConverter" /> < bean class = "org.springframework.http.converter.ByteArrayHttpMessageConverter" /> < bean class = "org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" > < property name = "prefixJson" value = "false" /> < property name = "objectMapper" > < bean class = "com.fasterxml.jackson.databind.ObjectMapper" > <!-- 處理responseBody 里面日期類型 --> < property name = "dateFormat" > < bean class = "java.text.SimpleDateFormat" > < constructor-arg type = "java.lang.String" value = "yyyy-MM-dd HH:mm:ss" /> </ bean > </ property > <!-- 為null字段時不顯示 --> < property name = "serializationInclusion" > < value type = "com.fasterxml.jackson.annotation.JsonInclude.Include" >NON_NULL</ value > </ property > </ bean > </ property > < property name = "supportedMediaTypes" > < list > < value >application/json;charset=UTF-8</ value > < value >application/x-www-form-urlencoded;charset=UTF-8</ value > </ list > </ property > </ bean > </ mvc:message-converters > </ mvc:annotation-driven > <!-- 掃包 --> < context:component-scan base-package = "com.krry.controller" ></ context:component-scan > <!--對靜態資源文件的訪問 必須要設置,因為在springmvc的配置中配置了/匹配所有請求, 此工程所有的請求(.do ,addUser,js/image/css)都會被springmvc解析, 必須對所有的靜態資源文件進行過濾放行 --> <!-- 靜態資源過濾 下面二選一--> <!--<mvc:default-servlet-handler/> --> < mvc:resources mapping = "/resourse/**" location = "/resourse/" /> <!-- 攔截器定義 --> < mvc:interceptors > < mvc:interceptor > <!-- 個人中心也需要登陸 以admin開頭的配置都會進行攔截--> < mvc:mapping path = "/admin/**" ></ mvc:mapping > <!-- 這個是設置不會進入攔截器的路徑 --> < mvc:exclude-mapping path = "/resourse/**" /> <!-- 攔截器進入的類,返回false表示不會進入輸入的路徑 --> < bean class = "com.krry.core.filter.LoginInterceptor" /> </ mvc:interceptor > </ mvc:interceptors > <!-- 配置文件解析器 --> < bean id = "multipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding = "utf-8" > < property name = "uploadTempDir" value = "/temp" ></ property > < property name = "maxUploadSize" > < value >209715200</ value > <!-- 200MB --> </ property > < property name = "maxInMemorySize" > < value >4096</ value > <!-- 4KB大小讀寫 --> </ property > </ bean > <!-- 視圖渲染 jsp/freemaker/velocity--> < bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > <!-- 制定頁面存放的路徑 --> < property name = "prefix" value = "/WEB-INF/pages/" ></ property > <!-- 文件的后綴 --> < property name = "suffix" value = ".jsp" ></ property > </ bean > </ beans > |
數據庫配置文件: jdbc.properties
1
2
3
4
|
db.driver=oracle.jdbc.OracleDriver db.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl db.username=4m+la23KCA4= db.password=WWijcIyMPaU\= |
我這里使用了加密算法
日志配置文件: log4j.properties
1
2
3
4
5
6
7
8
9
10
11
12
|
log4j.rootLogger=DEBUG, CONSOLE, FILE log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %10l - %m%n log4j.appender.FILE=org.apache.log4j.RollingFileAppender log4j.appender.FILE.File=D:/logs/log4j.log log4j.appender.FILE.MaxFileSize=1MB log4j.appender.FILE.Append = true log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.ConversionPattern=%d{yyyy/MM/dd/HH:mm:ss} %-5p [%t] %10l - %m%n |
mybatis配置文件: mybatis-config.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<? 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 > < settings > <!-- 全局映射器啟用緩存 --> < setting name = "cacheEnabled" value = "true" /> <!-- 查詢時,關閉關聯對象即時加載以提高性能 --> < setting name = "lazyLoadingEnabled" value = "true" /> <!-- 設置關聯對象加載的形態,此處為按需加載字段(加載字段由SQL指 定),不會加載關聯表的所有字段,以提高性能 --> < setting name = "aggressiveLazyLoading" value = "false" /> <!-- 對于未知的SQL查詢,允許返回不同的結果集以達到通用的效果 --> < setting name = "multipleResultSetsEnabled" value = "true" /> <!-- 允許使用列標簽代替列名 --> < setting name = "useColumnLabel" value = "true" /> <!-- 允許使用自定義的主鍵值(比如由程序生成的UUID 32位編碼作為鍵值),數據表的PK生成策略將被覆蓋 --> < setting name = "useGeneratedKeys" value = "true" /> <!-- 給予被嵌套的resultMap以字段-屬性的映射支持 --> < setting name = "autoMappingBehavior" value = "FULL" /> <!-- 對于批量更新操作緩存SQL以提高性能 --> < setting name = "defaultExecutorType" value = "BATCH" /> <!-- 數據庫超過25000秒仍未響應則超時 --> < setting name = "defaultStatementTimeout" value = "25" /> < setting name = "lazyLoadTriggerMethods" value = "equals,clone,hashCode,toString" /> </ settings > < typeAliases > <!--自定義user對象的別名 --> <!-- <typeAlias type="com.krry.mybatis.sysmanage.entity.User" alias="user"/> --> <!-- 批量定義別名 --> < package name = "com.krry.entity" /> </ typeAliases > </ configuration > |
網絡程序配置文件:web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
<? xml version = "1.0" encoding = "UTF-8" ?> < web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id = "WebApp_ID" version = "3.1" > < display-name >maven_SSM</ display-name > < welcome-file-list > < welcome-file >index</ welcome-file > </ welcome-file-list > <!-- 加載Spring IOC容器 --> < context-param > < param-name >contextConfigLocation</ param-name > < param-value >classpath:applicationContext.xml</ param-value > </ context-param > <!-- spring上下文監聽器 --> < listener > < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class > </ listener > <!-- Introspector緩存清除監聽器 --> < listener > < listener-class >org.springframework.web.util.IntrospectorCleanupListener</ listener-class > </ listener > < filter > < filter-name >encoding</ 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 >encoding</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > <!-- 配置DispatcherServlet --> < servlet > < servlet-name >maven_SSM</ servlet-name > < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > <!-- 配置springMVC的配置文件 --> <!-- 如果不配置下面選項,系統默認加載classpath下面名為[servlet-name]-servlet.xml的文件 springmvc01-servlet.xml --> < init-param > < param-name >contextConfigLocation</ param-name > < param-value >classpath:springmvc.xml</ param-value > </ init-param > </ servlet > < servlet-mapping > < servlet-name >maven_SSM</ servlet-name > < url-pattern >/index</ url-pattern > </ servlet-mapping > <!-- 可以配*.do, *.action(了解) / (重點): 所有的請求都會被spring mvc解析,但必須對靜態資源文件進行過濾放行,建議大家使用這種方式 /* : 不建議大家使用 --> < servlet-mapping > < servlet-name >maven_SSM</ servlet-name > < url-pattern >/</ url-pattern > </ servlet-mapping > </ web-app > |
到這里,基本配置全部完成,jar包也自動依賴。就進行測試和編寫后續 java 的代碼
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/ainyi/p/8547106.html