springMVC+mybatis+jsp.docx
《springMVC+mybatis+jsp.docx》由会员分享,可在线阅读,更多相关《springMVC+mybatis+jsp.docx(32页珍藏版)》请在冰豆网上搜索。
springMVC+mybatis+jsp
阅读目录
∙1.工程目录结构整理清楚
∙2.引入依赖包
∙3.配置数据库连接属性
∙4.配置spring配置文件
∙5.java代码编写(model,dao,service层代码)
∙6.mybatis配置
∙7.junit测试插入功能
∙8.springMVC模块搭建
∙9.log4j日志记录搭建
∙10.测试运行
∙整体包下载地址:
springMVC+mybatis框架搭建
首先我们先要弄清搭建项目的一般流程,需要注意哪些方面,想要什么样的效果,自己的功能有哪些?
(假设效果:
项目目录结构清晰,能够查询到本地数据库中的内容。
。
)
1. 工程目录结构整理清楚
在src/main/java文件夹中,新建包cn.springmvc.model(存放javabean),
cn.springmvc.dao(存放spring与mybatis连接接口),
cn.springmvc.service(service接口),
cn.springmvc.service.impl(service接口的实现),
cn.springmvc.controller(存放控制层controller)
在src/main/resource文件夹中,新建包conf(存放配置文件),
mapper(mybatis的mapper文件)
在src/test/java文件夹中,新建包cn.springmvc.test(存放测试文件)
在WEB-INF文件夹下新建jsp文件夹(存放jsp文件)
这样项目结构基本完成了
回到顶部
2. 引入依赖包
打开maven的pom文件,对本次开发所需使用的架包依次导入(maven项目管理的优势)
查找依赖结构有个不错的网站,http:
//search.maven.org/ 只要输入包名即可查找引来关系
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
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
//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.0http:
//maven.apache.org/maven-v4_0_0.xsd">
4.0.0
eyas.springmvc
springmvc
war
0.0.1-SNAPSHOT
springmvcMavenWebapp
http:
//maven.apache.org
--spring版本号-->
3.2.4.RELEASE
--mybatis版本号-->
3.2.4
--log4j日志文件管理包版本-->
1.6.6
1.2.9
--spring核心包-->
--springframestart-->
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-oxm
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-test
${spring.version}
--springframeend-->
--mybatis核心包-->
org.mybatis
mybatis
${mybatis.version}
--mybatis/spring包-->
org.mybatis
mybatis-spring
1.2.2
--mysql驱动包-->
mysql
mysql-connector-java
5.1.29
--junit测试包-->
junit
junit
4.11
test
--阿里巴巴数据源包-->
com.alibaba
druid
1.0.2
--json数据-->
org.codehaus.jackson
jackson-mapper-asl
1.9.13
--日志文件管理包-->
--logstart-->
log4j
log4j
${log4j.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
--logend-->
springmvc
回到顶部
3.配置数据库连接属性
conf/jdbc.properties(jdbc配置文件)
?
1
2
3
4
jdbc_driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:
mysql:
//localhost:
3306/mydays?
useUnicode=true&characterEncoding=utf-8
jdbc_username=root
jdbc_password=root
回到顶部
4. 配置spring配置文件
conf/spring.xml(spring配置文件的扫描)
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
xmlversion="1.0"encoding="UTF-8"?
>
//www.springframework.org/schema/beans"
xmlns:
context="http:
//www.springframework.org/schema/context"
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-3.0.xsd
http:
//www.springframework.org/schema/context
http:
//www.springframework.org/schema/context/spring-context.xsd">
--引入jdbc配置文件-->
property-placeholderlocation="classpath:
conf/jdbc.properties"/>
--扫描文件(自动将servicec层注入)-->
component-scanbase-package="cn.springmvc.service"/>
conf/spring-mybatis.xml(spring与mybatis连接属性)
?
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
4
xmlversion="1.0"encoding="UTF-8"?
>
//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:
aop="http:
//www.springframework.org/schema/aop"
xmlns:
tx="http:
//www.springframework.org/schema/tx"
xmlns:
util="http:
//www.springframework.org/schema/util"
xsi:
schemaLocation="http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-3.2.xsd
http:
//www.springframework.org/schema/context
http:
//www.springframework.org/schema/context/spring-context-3.2.xsd
http:
//www.springframework.org/schema/tx
http:
//www.springframework.org/schema/tx/spring-tx-3.2.xsd
http:
//www.springframework.org/schema/aop
http:
//www.springframework.org/schema/aop/spring-aop-3.2.xsd
http:
//www.springframework.org/schema/util
http:
//www.springframework.org/schema/util/spring-util-3.2.xsd">
destroy-method="close">
${jdbc_driverClassName}
${jdbc_url}
${jdbc_username}
${jdbc_password}
--连接池最大使用连接数-->
20
--初始化连接大小-->
1
--获取连接最大等待时间-->
60000
--连接池最大空闲-->
20
--连接池最小空闲-->
3
--自动清除无用连接-->
<