转自 http://www.demohot.com/demo/4
整合spring+mybatis
使用前,需执行提供的sql脚本
更改jdbc.properties文件
主要配置文件
pom.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
102
103
104
105
106
107
108
109
|
<
project
xmlns
=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
<
modelVersion
>4.0.0</
modelVersion
>
<
groupId
>com.demohot.demos</
groupId
>
<
artifactId
>springmvc-mybatis-demo</
artifactId
>
<
version
>0.0.1-SNAPSHOT</
version
>
<
packaging
>war</
packaging
>
<
dependencies
>
<
dependency
>
<
groupId
>javax.servlet</
groupId
>
<
artifactId
>javax.servlet-api</
artifactId
>
<
version
>3.1.0</
version
>
<
scope
>provided</
scope
>
</
dependency
>
<
dependency
>
<
groupId
>jstl</
groupId
>
<
artifactId
>jstl</
artifactId
>
<
version
>1.2</
version
>
</
dependency
>
<
dependency
>
<
groupId
>junit</
groupId
>
<
artifactId
>junit</
artifactId
>
<
version
>4.12</
version
>
<
scope
>test</
scope
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-test</
artifactId
>
<
version
>4.2.5.RELEASE</
version
>
<
scope
>test</
scope
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-webmvc</
artifactId
>
<
version
>4.2.5.RELEASE</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-jdbc</
artifactId
>
<
version
>4.2.5.RELEASE</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-tx</
artifactId
>
<
version
>4.2.5.RELEASE</
version
>
</
dependency
>
<!-- jdbc mysql driver -->
<
dependency
>
<
groupId
>mysql</
groupId
>
<
artifactId
>mysql-connector-java</
artifactId
>
<
version
>5.1.38</
version
>
</
dependency
>
<!-- datasource -->
<
dependency
>
<
groupId
>com.mchange</
groupId
>
<
artifactId
>c3p0</
artifactId
>
<
version
>0.9.5.2</
version
>
</
dependency
>
<!-- mybatis -->
<
dependency
>
<
groupId
>org.mybatis</
groupId
>
<
artifactId
>mybatis</
artifactId
>
<
version
>3.4.0</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.mybatis</
groupId
>
<
artifactId
>mybatis-spring</
artifactId
>
<
version
>1.3.0</
version
>
</
dependency
>
<
dependency
>
<
groupId
>com.alibaba</
groupId
>
<
artifactId
>fastjson</
artifactId
>
<
version
>1.2.11</
version
>
</
dependency
>
</
dependencies
>
<
build
>
<
finalName
>springmvc-mybatis-demo</
finalName
>
<
plugins
>
<!-- 指定jdk版本 -->
<
plugin
>
<
groupId
>org.apache.maven.plugins</
groupId
>
<
artifactId
>maven-compiler-plugin</
artifactId
>
<
configuration
>
<
source
>1.8</
source
>
<
target
>1.8</
target
>
</
configuration
>
</
plugin
>
</
plugins
>
</
build
>
</
project
>
|
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
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
xsi:schemaLocation
=
"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version
=
"3.1"
>
<
display-name
>springmvc-mybatis-demo</
display-name
>
<
context-param
>
<
param-name
>contextConfigLocation</
param-name
>
<
param-value
>classpath:beans-app.xml</
param-value
>
</
context-param
>
<!-- spring beans管理 -->
<
listener
>
<
listener-class
>org.springframework.web.context.ContextLoaderListener</
listener-class
>
</
listener
>
<!-- 处理中文乱码 -->
<
filter
>
<
filter-name
>characterEncodingFilter</
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
>characterEncodingFilter</
filter-name
>
<
url-pattern
>/*</
url-pattern
>
</
filter-mapping
>
<!-- springmvc入口 -->
<
servlet
>
<
servlet-name
>dispatcher</
servlet-name
>
<
servlet-class
>org.springframework.web.servlet.DispatcherServlet</
servlet-class
>
<
init-param
>
<
param-name
>contextConfigLocation</
param-name
>
<
param-value
></
param-value
>
</
init-param
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>dispatcher</
servlet-name
>
<
url-pattern
>/</
url-pattern
>
</
servlet-mapping
>
</
web-app
>
|
beans-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
|
<?
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="
">
<
mvc:annotation-driven
/>
<
mvc:default-servlet-handler
/>
<
bean
id
=
"jspViewResolver"
class
=
"org.springframework.web.servlet.view.InternalResourceViewResolver"
>
<
property
name
=
"viewClass"
value
=
"org.springframework.web.servlet.view.JstlView"
/>
<
property
name
=
"prefix"
value
=
"/WEB-INF/view/"
/>
</
bean
>
</
beans
>
|
beans-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
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:context
=
"http://www.springframework.org/schema/context"
xsi:schemaLocation="
">
<
context:property-placeholder
location
=
"classpath:jdbc.properties"
/>
<!-- datasource -->
<
bean
id
=
"dataSource"
class
=
"com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method
=
"close"
>
<
property
name
=
"driverClass"
value
=
"${jdbc.driverClassName}"
/>
<
property
name
=
"jdbcUrl"
value
=
"${jdbc.url}"
/>
<
property
name
=
"user"
value
=
"${jdbc.username}"
/>
<
property
name
=
"password"
value
=
"${jdbc.password}"
/>
<
property
name
=
"initialPoolSize"
value
=
"${jdbc.initialPoolSize}"
/>
<
property
name
=
"minPoolSize"
value
=
"${jdbc.minPoolSize}"
/>
<
property
name
=
"maxPoolSize"
value
=
"${jdbc.maxPoolSize}"
/>
<
property
name
=
"maxIdleTime"
value
=
"25000"
/>
<!-- 小于mysqld默认时间28800s=8小时 -->
<
property
name
=
"checkoutTimeout"
value
=
"0"
/>
<!-- 连接用完时,等待获取新连接的时间0ms -->
<
property
name
=
"acquireIncrement"
value
=
"2"
/>
<!-- 连接数不够时,每次获取n个连接 -->
<
property
name
=
"acquireRetryAttempts"
value
=
"30"
/>
<!-- 默认1000ms尝试获取一次新连接,获取新连接失败时尝试次数 -->
<
property
name
=
"idleConnectionTestPeriod"
value
=
"60"
/>
<!-- 60s检查一次idle连接是否有效 -->
</
bean
>
<!-- 事务配置,基于注解 -->
<
tx:annotation-driven
transaction-manager
=
"txManager"
/>
<
bean
id
=
"txManager"
class
=
"org.springframework.jdbc.datasource.DataSourceTransactionManager"
>
<
property
name
=
"dataSource"
ref
=
"dataSource"
/>
</
bean
>
<!-- mybatis -->
<
bean
id
=
"sqlSessionFactory"
class
=
"org.mybatis.spring.SqlSessionFactoryBean"
>
<
property
name
=
"dataSource"
ref
=
"dataSource"
/>
<
property
name
=
"mapperLocations"
value
=
"classpath:mybatis-mapper/**/*.xml"
/>
<
property
name
=
"typeAliasesPackage"
value="
com.demohot.demos.module.user.po,
com.demohot.demos.module.demo.po
" />
<
property
name
=
"typeHandlersPackage"
value="
com.demohot.demos.module.user.po.enums.handler,
com.demohot.demos.module.demo.po.enums.handler
" />
</
bean
>
<
bean
class
=
"org.mybatis.spring.mapper.MapperScannerConfigurer"
>
<
property
name
=
"basePackage"
value="
com.demohot.demos.module.user.dao,
com.demohot.demos.module.demo.dao
" />
</
bean
>
</
beans
>
|
包含主要页面
/
/users
/user/new
/user/edit