springmvc集成mybatis

转自 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
     < 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" ?>
< web-app  xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
     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" ?>
     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" ?>
     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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值