ssm (ssm 框架集)
ssm(spring+springmvc+mybatis)框架集由spring、springmvc、mybatis三个开源框架整合而成,常作为数据源较简单的web项目的框架。
其中spring是一个轻量级的控制反转(ioc)和面向切面(aop)的容器框架。
springmvc分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
mybatis是一个支持普通sql查询,存储过程和高级映射的优秀持久层框架。
0、系统环境
1)windows 10 企业版
2)jdk 1.8.0_131
3)eclipse java ee ide for web developers version: neon.3 release (4.6.3)
4)tomcat 8.5
1、maven下载及配置
maven的下载地址:http://maven.apache.org/download.cgi
下载后解压放在d:\java路径下
在环境变量中,新增系统变量名:maven_home,变量值:d:\java\maven
在path中追加:%maven_home%\bin;
在命令行窗口中输入:mvn –v,如果看见下图则说明maven安装配置完毕
2、
2.maven仓库的设置
maven仓库默认位置在系统用户目录下的.m2目录中,类似下图
因为众所周知的原因,直接访问maven公共仓库的速度比较慢,所以推荐使用阿里的maven仓库镜像。编辑setting.xml文件,在mirrors节点下,新增如下内容,这样从仓库中下载jar包速度上会快很多。
1
2
3
4
5
6
|
<mirror> <id>nexus-aliyun</id> <mirrorof>*</mirrorof> <name>nexus aliyun</name> <url>http: //maven.aliyun.com/nexus/content/groups/public</url> </mirror> |
3、eclipse整合maven
设置eclipse自带maven整合工具,在preferences中找到maven节点,观察user settings项的设置是否正确
点击installations节点,添加maven runtime
4、创建maven项目时设置jdk
问题描述:eclipse创建maven项目时,显示的jdk默认版本为1.5,实际使用的jdk为1.8,如何修改?
解决方案:找到本机maven仓库存放位置,比如:${user.home}/.m2/路径,编辑settings.xml文件,在profiles节点下配置
1
2
3
4
5
|
<profile> <id>jdk- 1.8 </id> <activation> <activebydefault> true </activebydefault> <jdk> 1.8 </jdk> </activation> <properties> <maven.compiler.source> 1.8 </maven.compiler.source> <maven.compiler.target> 1.8 </maven.compiler.target> <maven.compiler.compilerversion> 1.8 </maven.compiler.compilerversion> </properties> </profile> |
5、使用maven创建ssm项目
1)选择maven project
2)点击next,选择默认工作空间位置
3)选择web类型
4)填写groupid、artifactid
group id:相当于一个组织
artifact id:相当于这个组织下的一个具体项目
packege:根据group id和artifact id生成一个默认的名称
5)创建出的maven项目如下图
6)问题描述:提示错误:
在eclipse中设置server为tomcat,注意jre设置为安装的jdk的jre
在工程上右键,查看工程属性,找到java build path,添加server runtime为tomcat
点击finish后,项目工程变为下图
7)在项目上右键,查看项目信息
默认的dynamic web module为2.3,使用tomcat 8.5,需要修改为3.1
修改方法:
① maven工程所在目录下org.eclipse.wst.common.project.facet.core.xml
编辑内容,如下所示
1
2
3
4
5
6
|
<?xml version= "1.0" encoding= "utf-8" ?> <faceted-project> <fixed facet= "wst.jsdt.web" /> <installed facet= "java" version= "1.8" /> <installed facet= "jst.web" version= "2.3" /> <installed facet= "wst.jsdt.web" version= "1.0" /> </faceted-project> |
改为
1
2
3
4
5
|
<?xml version= "1.0" encoding= "utf-8" ?><faceted-project> <fixed facet= "wst.jsdt.web" /> <installed facet= "java" version= "1.8" /> <installed facet= "jst.web" version= "3.1" /> <installed facet= "wst.jsdt.web" version= "1.0" /> </faceted-project> |
② maven工程下的web.xml文件修改为
1
2
|
<?xml version= "1.0" encoding= "utf-8" ?><web-app xmlns= "http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version= "3.1" metadata-complete= "true" > </web-app> |
③ pom.xml文件中修改build节点,添加如下内容
1
2
3
4
5
6
7
|
<plugins> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-compiler-plugin</artifactid> <configuration> <source> 1.8 </source> <target> 1.8 </target> </configuration> </plugin> </plugins> |
④ 修改后,在项目上右键,找到maven属性下的update project,点击之
⑤ 选择该项目进行更新,如果怕不能强制更新,可以勾选force update of snapshots/releases
⑥ 点击ok后更新maven项目,再观察项目属性,module已经变为3.1
6、下载ssm框架所需jar包
修改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
|
<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>cn.temptation</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.3 . 8 .release</spring.version> </properties> <dependencies> <!-- spring相关包 --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</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-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-tx</artifactid> <version>${spring.version}</version> </dependency> <!-- aop相关包 --> <dependency> <groupid>org.aspectj</groupid> <artifactid>aspectjrt</artifactid> <version> 1.8 . 0 </version> </dependency> <dependency> <groupid>org.aspectj</groupid> <artifactid>aspectjweaver</artifactid> <version> 1.8 . 0 </version> </dependency> <!-- mybatis相关包 --> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis</artifactid> <version> 3.3 . 0 </version> </dependency> <!-- mysql相关包 --> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version> 5.1 . 26 </version> </dependency> <!-- 数据库连接池 --> <dependency> <groupid>com.alibaba</groupid> <artifactid>druid</artifactid> <version> 1.0 . 20 </version> </dependency> <!-- spring集成mybatis --> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis-spring</artifactid> <version> 1.2 . 3 </version> </dependency> <!-- jsp标准标签库 --> <dependency> <groupid>javax.servlet</groupid> <artifactid>jstl</artifactid> <version> 1.2 </version> </dependency> <!-- 日志相关包 --> <dependency> <groupid>log4j</groupid> <artifactid>log4j</artifactid> <version> 1.2 . 17 </version> </dependency> <dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-api</artifactid> <version> 1.7 . 21 </version> </dependency> <!-- 单元测试相关包 --> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version> 3.8 . 1 </version> <scope>test</scope> </dependency> </dependencies> <build> <finalname>ssm</finalname> <plugins> <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> |
保存后,可以看到相关jar包被下载至本地仓库
7、完善项目结构
因为服务端maven项目的标准结构有四个子包:src/main/java、src/main/resources、src/test/java、src/test/resources,这里缺少了src/test/resources,所以手动补上。
在项目中新建source folder
创建src/test/resources目录后,工程如下图所示
右键查看项目属性,点击deployment assembly,移除test和target
移除后如下图
8、创建项目用的配置文件
创建log4j.properties文件,内容如下:
1
2
3
4
5
6
7
8
9
10
11
|
#use this setting for output mybatis`s sql on the console log4j.rootlogger=debug, console #console log4j.appender.console=org.apache.log4j.consoleappender log4j.appender.console.layout=org.apache.log4j.patternlayout log4j.appender.console.layout.conversionpattern=%d [%t] %-5p [%c] - %m%n log4j.logger.java.sql.resultset=info log4j.logger.org.apache=info log4j.logger.java.sql.connection=debug log4j.logger.java.sql.statement=debug log4j.logger.java.sql.preparedstatement=debug |
创建mybatis-config.xml文件,内容如下:
1
2
3
4
5
6
7
8
9
10
|
<?xml version= "1.0" encoding= "utf-8" ?> <!doctype configuration public "-//mybatis.org//dtd config 3.0//en" "http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration> <!-- 设置别名 --> <typealiases> < package name= "cn.temptation.domain" /> </typealiases> </configuration> |
创建spring-mvc.xml文件,内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?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" xmlns:mvc= "http://www.springframework.org/schema/mvc" xsi:schemalocation=" http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http: //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http: //www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 启动自动扫描 --> <context:component-scan base- package = "cn.temptation.*" /> <!-- 注册mvc注解驱动 --> <mvc:annotation-driven /> <!-- 静态资源可访问的设置方式 --> <mvc: default -servlet-handler /> <!-- 配置视图解析器,可以显式设置,也可以不设置,不设置会依据springmvc的默认设置 --> <bean id= "viewresolver" class = "org.springframework.web.servlet.view.internalresourceviewresolver" > <property name= "prefix" value= "/" /> <property name= "suffix" value= ".jsp" /> </bean> </beans> |
创建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
|
<?xml version= "1.0" encoding= "utf-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" 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.xsd"> <!-- 定义数据源bean --> <!-- druid --> <bean id= "datasource" class = "com.alibaba.druid.pool.druiddatasource" > <property name= "url" value= "jdbc:mysql://localhost:3306/test" /> <property name= "username" value= "root" /> <property name= "password" value= "sa" /> </bean> <!-- 注册sqlsessionfactorybean --> <bean id= "sqlsessionfactory" class = "org.mybatis.spring.sqlsessionfactorybean" > <property name= "datasource" ref= "datasource" /> <!-- 自动扫描mappers.xml文件 --> <property name= "mapperlocations" value= "classpath:cn/temptation/dao/*.xml" /> <property name= "configlocation" value= "classpath:mybatis-config.xml" ></property> </bean> <!-- dao接口所在包名,spring会自动查找其下的类 --> <bean class = "org.mybatis.spring.mapper.mapperscannerconfigurer" > <property name= "basepackage" value= "cn.temptation.dao" /> <property name= "sqlsessionfactorybeanname" value= "sqlsessionfactory" /> </bean> </beans> |
创建spring-tx.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
|
<?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" xmlns:aop= "http://www.springframework.org/schema/aop" xmlns:tx= "http://www.springframework.org/schema/tx" xsi:schemalocation=" http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http: //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http: //www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http: //www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 开启aop注解扫描 --> <aop:aspectj-autoproxy proxy-target- class = "true" /> <!-- 事务管理器,依赖于数据源 --> <bean id= "txmanager" class = "org.springframework.jdbc.datasource.datasourcetransactionmanager" > <property name= "datasource" ref= "datasource" /> </bean> <!-- 编写通知:对事务进行增强(通知),需要编写对切入点和具体执行事务细节 --> <tx:advice id= "txadvice" transaction-manager= "txmanager" > <tx:attributes> <!-- 为切入点方法添加事务详情 name:方法名,*表示任意方法名称 propagation:设置传播行为 isolation:设置隔离级别 read-only:是否只读 --> <tx:method name= "add*" propagation= "required" isolation= "default" read-only= "false" rollback- for = "exception" /> <tx:method name= "delete*" propagation= "required" isolation= "default" read-only= "false" rollback- for = "exception" /> <tx:method name= "update*" propagation= "required" isolation= "default" read-only= "false" rollback- for = "exception" /> </tx:attributes> </tx:advice> <!-- 设置aop,让spring自动对目标生成代理,需要使用aspectj表达式 --> <aop:config proxy-target- class = "true" > <!-- 切面:整合切入点和通知 --> <aop:advisor advice-ref= "txadvice" pointcut= "within(cn.temptation.web..*)" /> </aop:config> </beans> |
9、编写服务端代码
编写user实体类,代码如下:
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
|
package cn.temptation.domain; /** * 用户信息 */ public class user { // 成员变量 private integer userid; private string username; private string password; // 构造函数 public user() { super (); } public user(integer userid, string username, string password) { super (); this .userid = userid; this .username = username; this .password = password; } // 成员方法 public integer getuserid() { return userid; } public void setuserid(integer userid) { this .userid = userid; } public string getusername() { return username; } public void setusername(string username) { this .username = username; } public string getpassword() { return password; } public void setpassword(string password) { this .password = password; } } |
编写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
27
28
29
30
31
32
33
34
35
36
37
38
|
package cn.temptation.web; import javax.annotation.resource; import javax.servlet.http.httpsession; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.servlet.modelandview; import cn.temptation.dao.userdao; import cn.temptation.domain.user; /** * 用户控制器 */ @controller @requestmapping (value = "/user" ) public class usercontroller { @resource private userdao userdao; @requestmapping ( "/view" ) public string view() { return "main/login" ; } @requestmapping ( "/indexview" ) public string index() { return "main/index" ; } @requestmapping (value = "/login" , method = requestmethod.post) public modelandview login(user model, httpsession session) { user user = userdao.findbyusername(model.getusername()); if (user == null || !user.getpassword().equals(model.getpassword())) { return new modelandview( "redirect:/login.jsp" ); } else { session.setattribute( "user" , user); modelandview mav = new modelandview(); mav.setviewname( "index" ); return mav; } } } |
编写userdao数据访问层接口,代码如下:
1
2
3
4
5
|
package cn.temptation.dao; import cn.temptation.domain.user; public interface userdao { public abstract user findbyusername(string username); } |
编写usermapper.xml数据访问层映射文件,代码如下:
1
2
3
4
5
6
7
8
|
<?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" > <mapper namespace= "cn.temptation.dao.userdao" > <select id= "findbyusername" parametertype= "string" resulttype= "user" > select * from userinfo where username=#{username} </select> </mapper> |
10、编写客户端代码
编写login.jsp登录页,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<%@ page language= "java" contenttype= "text/html; charset=utf-8" pageencoding= "utf-8" %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd" > <html> <head> <meta http-equiv= "content-type" content= "text/html; charset=utf-8" > <title>登录</title> </head> <body> <form action= "user/login" method= "post" > <label>账号:</label> <input type= "text" id= "txtusername" name= "username" placeholder= "请输入账号" /><br/> <label>密码:</label> <input type= "password" id= "txtpassword" name= "password" placeholder= "请输入密码" /><br/> <input type= "submit" value= "提交" /> <input type= "reset" value= "重置" /> </form> </body> </html> |
编写index.jsp主页,代码如下:
1
2
3
4
5
6
7
8
9
10
11
|
<%@ page language= "java" contenttype= "text/html; charset=utf-8" pageencoding= "utf-8" %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd" > <html> <head> <meta http-equiv= "content-type" content= "text/html; charset=utf-8" > <title>主页</title> </head> <body> <h3>欢迎,${user.username }</h3> </body> </html> |
11、使用maven构建项目
在项目上右键,找到maven属性的update project,也可以alt+f5操作
修改项目编译路径为jre系统类库,否则后续操作出错
项目上右键,找到run as属性,找到maven install
点击执行,结果如下说明构建成功
项目运行还是使用web工程的部署运行方式
运行项目,观察是否报错
登录页面
登录成功,跳转到主页
总结
以上所述是小编给大家介绍的使用eclipse + maven一步步搭建ssm框架教程详解,希望对大家有所帮助如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:https://www.cnblogs.com/knightsu/p/knightsu.html