簡單之美,springmvc,mybatis就是一個很好的簡單集成方案,能夠滿足一般的項目需求。閑暇時間把項目配置文件共享出來,供大家參看:
1.首先我們來看下依賴的pom:
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
|
<!-- spring --> < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-core</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-beans</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-context</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-tx</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-web</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-webmvc</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-jdbc</ artifactId > < version >${spring.version}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-test</ artifactId > < version >${spring.version}</ version > < scope >test</ scope > </ dependency > <!-- mybatis 包 --> < dependency > < groupId >org.mybatis</ groupId > < artifactId >mybatis</ artifactId > < version >3.2.8</ version > </ dependency > <!--mybatis spring 插件 --> < dependency > < groupId >org.mybatis</ groupId > < artifactId >mybatis-spring</ artifactId > < version >1.2.2</ version > </ dependency > <!-- mysql連接 --> < dependency > < groupId >mysql</ groupId > < artifactId >mysql-connector-java</ artifactId > < version >5.1.34</ version > </ dependency > <!-- 數據源 --> < dependency > < groupId >com.alibaba</ groupId > < artifactId >druid</ artifactId > < version >1.0.12</ version > </ dependency > < dependency > < groupId >org.aspectj</ groupId > < artifactId >aspectjweaver</ artifactId > < version >1.8.4</ version > </ dependency > <!-- log4j --> < dependency > < groupId >log4j</ groupId > < artifactId >log4j</ artifactId > < version >1.2.17</ version > </ dependency > <!-- servlet --> < dependency > < groupId >javax.servlet</ groupId > < artifactId >servlet-api</ artifactId > < version >3.0-alpha-1</ version > </ dependency > < dependency > < groupId >javax.servlet</ groupId > < artifactId >jstl</ artifactId > < version >1.2</ version > </ dependency > <!-- json --> < dependency > < groupId >org.codehaus.jackson</ groupId > < artifactId >jackson-mapper-asl</ artifactId > < version >1.9.13</ version > </ dependency > < dependency > < groupId >com.alibaba</ groupId > < artifactId >fastjson</ artifactId > < version >1.2.3</ version > </ dependency > < dependency > < groupId >com.fasterxml.jackson.core</ groupId > < artifactId >jackson-annotations</ artifactId > < version >${jackson.version}</ version > </ dependency > < dependency > < groupId >com.fasterxml.jackson.core</ groupId > < artifactId >jackson-core</ artifactId > < version >${jackson.version}</ version > </ dependency > < dependency > < groupId >com.fasterxml.jackson.core</ groupId > < artifactId >jackson-databind</ artifactId > < version >${jackson.version}</ version > </ dependency > <!-- 文件上傳 --> < 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.2.2</ version > </ dependency > |
spring 選用的是4.1.4的版本,根據系統需要我們可以選擇自己適合的版本。
2.相關的配置文件:
a)spring.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<? 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:context = "http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <!--引入配置屬性文件 --> < context:property-placeholder location = "classpath:config.properties" /> <!--自動掃描含有@Service將其注入為bean --> < context:component-scan base-package = "com.demo.report.web.service" /> |
b)spring-mvc.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
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:mvc = "http://www.springframework.org/schema/mvc" 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 自動掃描controller包下的所有類,如果@Controller注入為bean --> < context:component-scan base-package = "com.demo.report.web.controller" /> <!-- 避免IE執行AJAX時,返回JSON出現下載文件 --> < bean id = "mappingJacksonHttpMessageConverter" class = "org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" > < property name = "supportedMediaTypes" > < list > < value >text/html;charset=UTF-8</ value > </ list > </ property > </ bean > <!-- 啟動Spring MVC的注解功能,完成請求和注解POJO的映射 --> < bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > < property name = "messageConverters" > < list > <!-- json轉換器 --> < ref bean = "mappingJacksonHttpMessageConverter" /> </ list > </ property > </ bean > <!-- 對模型視圖名稱的解析,即在模型視圖名稱添加前后綴 --> < bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > < property name = "viewClass" value = "org.springframework.web.servlet.view.JstlView" /> < property name = "prefix" value = "" /> < property name = "suffix" value = "" /> </ bean > <!-- 配置多文件上傳 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding"> <value>UTF-8</value> </property> <property name="maxUploadSize"> <value>32505856</value> </property> <property name="maxInMemorySize"> <value>4096</value> </property> </bean>--> </ beans > |
c)spring-mybatis.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
|
<? 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:tx = "http://www.springframework.org/schema/tx" xmlns:aop = "http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd "> <!-- 配置數據源 使用的是Druid數據源 --> < bean name = "dataSource" class = "com.alibaba.druid.pool.DruidDataSource" init-method = "init" destroy-method = "close" > < property name = "url" value = "${jdbc.url}" /> < property name = "username" value = "${jdbc.username}" /> < property name = "password" value = "${jdbc.password}" /> <!-- 初始化連接大小 --> < property name = "initialSize" value = "0" /> <!-- 連接池最大使用連接數量 --> < property name = "maxActive" value = "20" /> <!-- 連接池最小空閑 --> < property name = "minIdle" value = "0" /> <!-- 獲取連接最大等待時間 --> < property name = "maxWait" value = "60000" /> < property name = "poolPreparedStatements" value = "true" /> < property name = "maxPoolPreparedStatementPerConnectionSize" value = "33" /> <!-- 用來檢測有效sql --> < property name = "validationQuery" value = "${validationQuery}" /> < property name = "testOnBorrow" value = "false" /> < property name = "testOnReturn" value = "false" /> < property name = "testWhileIdle" value = "true" /> <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 --> < property name = "timeBetweenEvictionRunsMillis" value = "60000" /> <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 --> < property name = "minEvictableIdleTimeMillis" value = "25200000" /> <!-- 打開removeAbandoned功能 --> < property name = "removeAbandoned" value = "true" /> <!-- 1800秒,也就是30分鐘 --> < property name = "removeAbandonedTimeout" value = "1800" /> <!-- 關閉abanded連接時輸出錯誤日志 --> < property name = "logAbandoned" value = "true" /> <!-- 監控數據庫 --> < property name = "filters" value = "mergeStat" /> </ bean > <!-- myBatis文件 --> < bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > < property name = "dataSource" ref = "dataSource" /> <!-- 自動掃描entity目錄, 省掉Configuration.xml里的手工配置 --> < property name = "mapperLocations" value = "classpath:com/demo/report/web/mapper/*.xml" /> </ bean > < bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" > < property name = "basePackage" value = "com.feidai.report.web.mapper" /> < property name = "sqlSessionFactoryBeanName" value = "sqlSessionFactory" /> </ bean > <!-- 配置事務管理器 --> < bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > < property name = "dataSource" ref = "dataSource" /> </ bean > |
d)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
64
65
66
67
|
< display-name >springmvc_mybatis_demo</ display-name > < context-param > < param-name >contextConfigLocation</ param-name > < param-value >classpath:spring.xml,classpath:spring-mybatis.xml</ param-value > </ context-param > < filter > < filter-name >encodingFilter</ 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 > < init-param > < param-name >forceEncoding</ param-name > < param-value >true</ param-value > </ init-param > </ filter > < filter-mapping > < filter-name >encodingFilter</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > < listener > < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class > </ listener > <!-- 防止spring內存溢出監聽器 --> < listener > < listener-class >org.springframework.web.util.IntrospectorCleanupListener</ listener-class > </ listener > < servlet > < description >spring mvc servlet</ description > < servlet-name >rest</ servlet-name > < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > < init-param > < param-name >contextConfigLocation</ param-name > < param-value > classpath:spring-mvc.xml </ param-value > </ init-param > < load-on-startup >1</ load-on-startup > </ servlet > < servlet-mapping > < servlet-name >rest</ servlet-name > < url-pattern >/</ url-pattern > </ servlet-mapping > < servlet > < servlet-name >DruidStatView</ servlet-name > < servlet-class >com.alibaba.druid.support.http.StatViewServlet</ servlet-class > </ servlet > < servlet-mapping > < servlet-name >DruidStatView</ servlet-name > < url-pattern >/druid/*</ url-pattern > </ servlet-mapping > <!-- 配置session超時時間,單位分鐘 --> < session-config > < session-timeout >30</ session-timeout > </ session-config > < welcome-file-list > < welcome-file >index.jsp</ welcome-file > </ welcome-file-list > |
使用了druid的數據源,在web中的詳細配置可以參看代碼。
以上就是對springmvc mybatis集成配置的資料整理,后續繼續補充相關資料,謝謝大家對本站的支持!