基本概念
1.1、spring
spring是一个开源框架,spring是于2003 年兴起的一个轻量级的java 开发框架,由rod johnson 在其著作expert one-on-one j2ee development and design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。spring使用基本的javabean来完成以前只可能由ejb完成的事情。然而,spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何java应用都可以从spring中受益。 简单来说,spring是一个轻量级的控制反转(ioc)和面向切面(aop)的容器框架。
1.2、springmvc
spring mvc属于springframework的后续产品,已经融合在spring web flow里面。spring mvc 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
1.3、mybatis
mybatis 本是apache的一个开源项目ibatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为mybatis 。mybatis是一个基于java的持久层框架。ibatis提供的持久层框架包括sql maps和data access objects(dao)mybatis 消除了几乎所有的jdbc代码和参数的手工设置以及结果集的检索。mybatis 使用简单的 xml或注解用于配置和原始映射,将接口和 java 的pojos(plain old java objects,普通的 java对象)映射成数据库中的记录。
第一步:通过maven建立一个web项目。
第二步:pom文件导入jar包
(1) pom.xml
我把我pom文件整个都拷贝来了,有些初始化就有的东西你要去掉,保留你自己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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
<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.test</groupid> <artifactid>ssm</artifactid> <packaging>war</packaging> <version> 0.0 . 1 -snapshot</version> <name>ssm maven webapp</name> <url>http: //maven.apache.org</url> <properties> <!-- spring版本号 --> <spring.version> 4.0 . 2 .release</spring.version> <!-- mybatis版本号 --> <mybatis.version> 3.2 . 6 </mybatis.version> <!-- log4j日志文件管理包版本 --> <slf4j.version> 1.7 . 7 </slf4j.version> <log4j.version> 1.2 . 17 </log4j.version> </properties> <dependencies> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version> 3.8 . 1 </version> <scope>test</scope> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</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-oxm</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-jdbc</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-aop</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context-support</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-test</artifactid> <version>${spring.version}</version> </dependency> <!-- mybatis核心包 --> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis</artifactid> <version>${mybatis.version}</version> </dependency> <!-- mybatis/spring包 --> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis-spring</artifactid> <version> 1.2 . 2 </version> </dependency> <!-- 导入java ee jar 包 --> <dependency> <groupid>javax</groupid> <artifactid>javaee-api</artifactid> <version> 7.0 </version> </dependency> <!-- 导入mysql数据库链接jar包 --> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version> 5.1 . 30 </version> </dependency> <!-- 导入dbcp的jar包,用来在applicationcontext.xml中配置数据库 --> <dependency> <groupid>commons-dbcp</groupid> <artifactid>commons-dbcp</artifactid> <version> 1.2 . 2 </version> </dependency> <!-- jstl标签类 --> <dependency> <groupid>jstl</groupid> <artifactid>jstl</artifactid> <version> 1.2 </version> </dependency> <!-- 日志文件管理包 --> <!-- log start --> <dependency> <groupid>log4j</groupid> <artifactid>log4j</artifactid> <version>${log4j.version}</version> </dependency> <!-- 格式化对象,方便输出日志 --> <dependency> <groupid>com.alibaba</groupid> <artifactid>fastjson</artifactid> <version> 1.1 . 41 </version> </dependency> <dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-api</artifactid> <version>${slf4j.version}</version> </dependency> <dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-log4j12</artifactid> <version>${slf4j.version}</version> </dependency> <!-- log end --> <!-- 映入json --> <dependency> <groupid>org.codehaus.jackson</groupid> <artifactid>jackson-mapper-asl</artifactid> <version> 1.9 . 13 </version> </dependency> <!-- 上传组件包 --> <dependency> <groupid>commons-fileupload</groupid> <artifactid>commons-fileupload</artifactid> <version> 1.3 . 1 </version> </dependency> <dependency> <groupid>commons-io</groupid> <artifactid>commons-io</artifactid> <version> 2.4 </version> </dependency> <dependency> <groupid>commons-codec</groupid> <artifactid>commons-codec</artifactid> <version> 1.9 </version> </dependency> </dependencies> <build> <finalname>ssm</finalname> </build> </project> |
第三步:看整体的项目架构,先说明下我这里还没有有关springmvc的相关东西,因为我先整合sping-mybatis,在测试是否成功,成功在整合springmvc
第四步:建立model类
1
2
3
4
5
6
7
|
public class user { private int id; private string name; private string password; private string password2; /*提供set和get方法,tostring方法*/ } |
第五步:创建数据库,userdao接口和mapper映射文件
(1)建立很简单的一张表
(2)userdao接口
1
2
3
|
public interface userdao { user finduserbyid(user user); } |
(3)uesrmapper.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
|
<?xml version= "1.0" encoding= "utf-8" ?> <!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <!-- namespace命名空间,指向userdao全路径--> <mapper namespace= "com.ssm.dao.userdao" > <!-- 根据id查询用户信息 --> <!-- id:唯一标识 一个statement,和userdao方法名一致 #{}:表示 一个占位符,如果#{}中传入简单类型的参数,#{}中的名称随意 parametertype:输入 参数的类型,和userdao()里参数一致 resulttype:输出结果 类型,和userdao()返回类型一致 --> <select id= "finduserbyid" parametertype= "com.ssm.model.user" resulttype= "com.ssm.model.user" > select * from user <where> < if test= "name != null" > and name= #{name} </ if > < if test= "password != null" > and password= #{password} </ if > </where> </select> </mapper> |
第五步:创建userservice和userserviceimpl
(1)userservice
1
2
3
|
public interface userservice { public user getuserbyid(user user); } |
(2)userserviceimpl
1
2
3
4
5
6
7
8
9
10
11
12
|
import org.springframework.stereotype.service; import com.ssm.dao.userdao; import com.ssm.model.user; import com.ssm.service.userservice; @service ( "userservice" ) public class userserverimpl implements userservice { @resource private userdao userdao; public user getuserbyid(user user) { return this .userdao.finduserbyid(user); } } |
第六步:创建jdbc文件和log日志文件
(1)jdbc.properties
1
2
3
4
|
jdbc.driver=com.mysql.jdbc.driver jdbc.url=jdbc\:mysql\: //localhost\:3306/test?useunicode=true&&characterencoding=utf-8 jdbc.username=root jdbc.password=root |
(2)log4j.properties
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
log4j.rootlogger=info,console,file #定义日志输出目的地为控制台 log4j.appender.console=org.apache.log4j.consoleappender log4j.appender.console.target=system.out #可以灵活地指定日志输出格式,下面一行是指定具体的格式 log4j.appender.console.layout = org.apache.log4j.patternlayout log4j.appender.console.layout.conversionpattern=[%c] - %m%n #文件大小到达指定尺寸的时候产生一个新的文件 log4j.appender.file = org.apache.log4j.rollingfileappender #指定输出目录 log4j.appender.file.file = logs/ssm.log #定义文件最大大小 log4j.appender.file.maxfilesize = 10mb # 输出所以日志,如果换成debug表示输出debug以上级别日志 log4j.appender.file.threshold = all log4j.appender.file.layout = org.apache.log4j.patternlayout log4j.appender.file.layout.conversionpattern =[%p] [%d{yyyy-mm-dd hh\:mm\:ss}][%c]%m%n |
第七步:整合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
|
<?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:mvc= "http://www.springframework.org/schema/mvc" xsi:schemalocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans-3.1.xsd http: //www.springframework.org/schema/context http: //www.springframework.org/schema/context/spring-context-3.1.xsd http: //www.springframework.org/schema/mvc http: //www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 自动扫描 --> <context:component-scan base- package = "com.ssm" /> <!-- 引入实现jdbc配置文件 --> <bean id= "propertyconfigurer" class = "org.springframework.beans.factory.config.propertyplaceholderconfigurer" > <property name= "location" value= "classpath:jdbc.properties" /> </bean> <!-- 2 .数据库连接池 --> <bean id= "datasource" class = "org.apache.commons.dbcp.basicdatasource" destroy-method= "close" > <property name= "driverclassname" value= "${jdbc.driver}" /> <property name= "url" value= "${jdbc.url}" /> <property name= "username" value= "${jdbc.username}" /> <property name= "password" value= "${jdbc.password}" /> </bean> <!-- spring和mybatis整合,通过spring来管理mybatis的sqlsessionfactory会话工厂 --> <bean id= "sqlsessionfactory" class = "org.mybatis.spring.sqlsessionfactorybean" > <!-- 指定数据库连接池引用 --> <property name= "datasource" ref= "datasource" /> <!-- 自动扫描mapping.xml文件 --> <property name= "mapperlocations" value= "classpath:com/ssm/mapper/*.xml" ></property> </bean> <!-- dao接口所在包名,spring会自动查找其下的类 --> <bean class = "org.mybatis.spring.mapper.mapperscannerconfigurer" > <property name= "basepackage" value= "com.ssm.dao" /> <property name= "sqlsessionfactorybeanname" value= "sqlsessionfactory" ></property> </bean> <!-- (事务管理)transaction manager, use jtatransactionmanager for global tx --> <bean id= "transactionmanager" class = "org.springframework.jdbc.datasource.datasourcetransactionmanager" > <property name= "datasource" ref= "datasource" /> </bean> </beans> |
第八步:建立测试类
经过以上步骤(log4j不配也没影响),我们已经完成了spring和mybatis的整合,这样我们就可以编写一段测试代码来试试是否成功了。
测试类在src/test/java中建立,如果测试成功,表示spring和mybatis已经整合成功了。输出信息使用的是log4j打印到控制台。
(1)testmybatis测试类
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
|
package ssm; import javax.annotation.resource; import org.apache.log4j.logger; import org.junit.test; import org.junit.runner.runwith; import org.springframework.test.context.contextconfiguration; import org.springframework.test.context.junit4.springjunit4classrunner; import com.ssm.model.user; import com.ssm.service.userservice; /*让测试运行于spring测试环境*/ @runwith(springjunit4classrunner.class) /*用来指定加载的spring配置文件的位置,会加载默认配置文件*/ @contextconfiguration (locations = { "classpath:spring-mybatis.xml" }) public class testmybatis { @resource (name = "userservice" ) private userservice userservice; @test public void test1() { user user= new user(); user.setname( "张三" ); user.setpassword( "123" ); user user1 = userservice.getuserbyid(user); system.out.println(user1.tostring()); } } |
最后!关键看后台输出,也是见证奇迹的时候,如果输出对象那说明你配置整合成功!
那接下来我们开始整合springmvc
第九步:配置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
|
<?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:mvc= "http://www.springframework.org/schema/mvc" xsi:schemalocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans-3.1.xsd http: //www.springframework.org/schema/context http: //www.springframework.org/schema/context/spring-context-3.1.xsd http: //www.springframework.org/schema/mvc http: //www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 自动扫描该包,使springmvc认为包下用了 @controller 注解的类是控制器 --> <context:component-scan base- package = "com.ssm.controller" /> <!--避免ie执行ajax时,返回json出现下载文件 --> <bean id= "mappingjacksonhttpmessageconverter" class = "org.springframework.http.converter.json.mappingjacksonhttpmessageconverter" > <property name= "supportedmediatypes" > <list> <value>text/html;charset=utf- 8 </value> </list> </property> </bean> <!-- 启动springmvc的注解功能,完成请求和注解pojo的映射 --> <bean class = "org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter" > <property name= "messageconverters" > <list> <ref bean= "mappingjacksonhttpmessageconverter" /> <!-- json转换器 --> </list> </property> </bean> <!-- 定义跳转的文件的前后缀 ,视图模式配置--> <bean class = "org.springframework.web.servlet.view.internalresourceviewresolver" > <!-- 这里的配置我的理解是自动给后面action的方法 return 的字符串加上前缀和后缀,变成一个 可用的url地址 --> <property name= "prefix" value= "/web-inf/jsp/" /> <property name= "suffix" value= ".jsp" /> </bean> <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 --> <bean id= "multipartresolver" class = "org.springframework.web.multipart.commons.commonsmultipartresolver" > <!-- 默认编码 --> <property name= "defaultencoding" value= "utf-8" /> <!-- 文件大小最大值 --> <property name= "maxuploadsize" value= "10485760000" /> <!-- 内存中的最大值 --> <property name= "maxinmemorysize" value= "40960" /> </bean> </beans> |
第十步:配置web.xml文件
这里面对spring-mybatis.xml的引入以及配置的spring-mvc的servlet就是为了完成ssm整合,之前2框架整合不需要在此处进行任何配置。
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
|
<?xml version= "1.0" encoding= "utf-8" ?> <web-app xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xmlns= "http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version= "3.0" > <display-name>archetype created web application</display-name> <!-- spring和mybatis的配置文件 --> <context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:spring-mybatis.xml</param-value> </context-param> <!-- 编码过滤器 --> <filter> <filter-name>encodingfilter</filter-name> <filter- class >org.springframework.web.filter.characterencodingfilter</filter- class > <async-supported> true </async-supported> <init-param> <param-name>encoding</param-name> <param-value>utf- 8 </param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- spring监听器 --> <listener> <listener- class >org.springframework.web.context.contextloaderlistener</listener- class > </listener> <!-- 防止spring内存溢出监听器 --> <listener> <listener- class >org.springframework.web.util.introspectorcleanuplistener</listener- class > </listener> <!-- spring mvc servlet --> <servlet> <servlet-name>springmvc</servlet-name> <servlet- class >org.springframework.web.servlet.dispatcherservlet</servlet- class > <init-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup> 1 </load-on-startup> <async-supported> true </async-supported> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 此处可以可以配置成*. do ,对应struts的后缀习惯 --> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> </web-app> |
第十一步最后测试:
(1) 先写login.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<%@ page language= "java" contenttype= "text/html; charset=utf-8" pageencoding= "utf-8" %> <html> <head> <title>用户登录:</title> </head> <body> <form action= "login" > <label>用户名:</label> <input type= "text" id= "username" name= "username" ></input><br><br> <label>密码:</label> <input type= "text" id= "password" name= "password" ></input><br><br> <input type= "submit" value= "登录" /> </form> </body> </html> |
界面:
(2)在写usercontroller
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
|
import javax.servlet.http.httpservletrequest; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import com.ssm.dao.userdao; import com.ssm.model.user; @controller public class usercontroller { @resource private userdao userdao; @requestmapping ( "/jsp/login" ) public string login(httpservletrequest request){ string username=request.getparameter( "username" ); string password=request.getparameter( "password" ); user user= new user(); //根据用户名和学生查询数据库 user.setname(username); user.setpassword(password); user users=userdao.finduserbyid(user); //如果有学生就说明登录成功 if (users!= null ){ return "susscss" ; } //这个jsp我没有写,大家知道就好,你们可以自己写 return "err" ; } } |
(3)最后susscss.jsp
1
2
3
4
5
6
7
|
<%@ page language= "java" contenttype= "text/html; charset=utf-8" pageencoding= "utf-8" %> <html> <body> <p>用户账号密码正确,登录成功</p> </body> </html> |
完美!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/qdhxhz/p/8032314.html