我是靠谱客的博主 明理小丸子,这篇文章主要介绍eclipse手动搭建SSM框架,现在分享给大家,希望可以做个参考。

搭建SSM框架的大概步骤如下:

  1. 创建maven项目
  2. 导入Spring+SpringMVC+Mybatis及相关的jar包(也可引入maven依赖)
  3. 在spring的配置文件中配置Mybatis
  4. 写mapper映射器与对应的xml文件,完后测试能否成功访问数据库
    注:以上为dao层,接下来是service层与controller层
  5. 写service层以及controller层

接下来开始具体实现:
一、导包(maven导包则引入依赖即可)
  我是手动导入jar包,也可以使用maven导包,手动导包的步骤如下:
  1.1) 在webapp/WEB-INF/下创建一个lib目录
  1.2) 将所需jar包拷入lib中
  1.3) 选中jar包右键–>Build Path–>Add to Build Path即可
下面是我使用的jar包链接,需要的可以下载:
链接:https://pan.baidu.com/s/1WxHEO10xIk6WIf6srxbkxw
提取码:dhsa

二、准备数据库及测试表
  注:此处我使用的是mysql数据库

复制代码
1
2
3
4
5
6
7
8
9
10
11
CREATE DATABASE `testssm`; use `testssm`; DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `user_id` varchar(100) NOT NULL COMMENT '用户ID', `user_name` varchar(100) DEFAULT NULL COMMENT '用户名', `user_password` varchar(100) DEFAULT NULL COMMENT '密码', `user_nick` varchar(100) DEFAULT NULL COMMENT '昵称', PRIMARY KEY (`user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

三、在spring的配置文件中配置mybatis
  3.1) 首先准备一份空白的配置文件,命名为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
<?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:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/tx/spring-jms-3.2.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/tx/spring-lang-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/util http://www.springframework.org/schema/util/spring-util-2.0.xsd"> </beans>

  3.2) 配置数据库连接及连接池BasicDataSource:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
<!-- 配置数据库连接及连接池 (Mysql) --> <bean id="bds" class="org.apache.commons.dbcp.BasicDataSource"> <!-- 账号 --> <property name="username" value="root"></property> <!-- 密码 --> <property name="password" value="root"></property> <!-- 驱动 --> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <!-- url --> <property name="url" value="jdbc:mysql://localhost:3306/cloud_note"></property> </bean>

注:此处我使用的是mysql数据库,不同的数据库更换不同的URL与驱动即可

  3.3) 配置SqlSessionFactoryBean,即Mybatis的信息:

复制代码
1
2
3
4
5
6
7
8
<!-- 配置SqlSessionFactoryBean --> <bean id="ssfb" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 引用上面配置好的连接池,id为"bds"--> <property name="dataSource" ref="bds"></property> <!-- 配置mapper.xml的位置 --> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> </bean>

  3.4) 配置MapperScannerConfigurer ,即Mapper扫描:

复制代码
1
2
3
4
5
<!-- 配置Mapper扫描 (MapperScannerConfigurer) --> <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="dao"></property> </bean>

四、写实体类,mapper映射器、以及mapper配置文件
  4.1) 在main/java/下创建entity包,用于存放与表对应的实体类
 创建User.java实体类,加上get/set方法、并重写toString:

复制代码
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
package entity; import java.io.Serializable; /** * @author * 用户实体类,变量名与数据库中的字段名一致 */ public class User implements Serializable { private String user_id; private String user_name; private String user_password; private String user_nick; public User() { } public User(String user_id, String user_name, String user_password, String user_nick) { super(); this.user_id = user_id; this.user_name = user_name; this.user_password = user_password; this.user_nick = user_nick; } public String getUser_id() { return user_id; } public void setUser_id(String user_id) { this.user_id = user_id; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getUser_password() { return user_password; } public void setUser_password(String user_password) { this.user_password = user_password; } public String getUser_nick() { return user_nick; } public void setUser_nick(String user_nick) { this.user_nick = user_nick; } @Override public String toString() { return "User [user_id=" + user_id + ", user_name=" + user_name + ", user_password=" + user_password + ", user_nick=" + user_nick + "]"; } }

注:类字段名需与表字段名一致,就不必写过多的操作
  如不一致,需在mapper映射器中配置resultMap(此处非本文章重点)

  4.2) 在main/java/下创建dao包,用于存放mapper映射器(即接口)
注:一张表对应一个实体类,一个实体类对应一个mapper.xml文件
 创建UserDao.java接口,并定义方法,如下:

复制代码
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
package dao; import java.util.Map; import entity.User; /** * mapper映射器 * @author 陈新得 * */ public interface UserDao { //添加,对应insert操作 public void addUser(User user); //查询,对应select操作 public User findUserById(String userId); /** * 修改,对应update操作 * 在mapper映射器中,方法的参数只能有一个,若向使用多个参数可将其封入集合中,再传入集合即可 * 如此处值只要求修改密码,map中就有password、userId两个key,其余类推 */ public void updateUser(Map<String,String> params); //删除,对应delete操作 public void removeUserById(String userId); }

  4.3) 在main/resource/下创建mapper包,用于存放*mapper.xml
 创建UserMapper.xml,并配置其与mapper映射器的关系:

复制代码
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
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <!-- namespace为命名空间,需与映射器全名一致 --> <mapper namespace="dao.UserDao"> <!-- id必须与映射器中的方法名一致 parameterType用于指定参数传入的类型 resultType用于指定返回值类型,如果返回实体类,也必须使用全名 --> <insert id="addUser" parameterType="entity.User"> <!-- 当参数为实体类时,spring表达式中的字段必须与实体类中属性名一致 --> insert into user values(#{user_id},#{user_name},#{user_password},#{user_nick}) </insert> <select id="findUserById" parameterType="String" resultType="entity.User"> <!-- 当参数为实体类时,spring表达式中的字段可与实体类中属性名不一致 --> select *from user where user_id=#{userId} </select> <update id="updateUser" parameterType="Map"> update user set user_password=#{password} where user_id=#{userId} </update> <delete id="removeUserById" parameterType="String"> delete from user where user_id=#{userId} </delete> </mapper>

写了这么多先停下,进行dao层测试,看是否配置成功,能否连接到数据库
   在test/java/下创建test包,用于存放测试类
 创建TestDao.java类,用于测试数据库连接以及dao层的访问

复制代码
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
package test; import java.sql.Connection; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSource; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import dao.UserDao; import entity.User; public class TestDao { private ApplicationContext ac; private UserDao userDao; @Before public void init() { String[] cfg = {"spring-mybatis.xml"}; ac = new ClassPathXmlApplicationContext(cfg); userDao = ac.getBean("userDao", UserDao.class); } /** * 测试能否连接到数据库 * 正常结果应输出数据库的连接信息 * @throws SQLException */ @Test public void testDataSource() throws SQLException { DataSource ds = ac.getBean("bds",BasicDataSource.class); Connection conn = ds.getConnection(); System.out.println(conn); conn.close(); } /** * 测试添加用户 * 正常结果为:数据成功添加到数据库 */ @Test public void testAddUser() { User user = new User("111","张三","123456","老张"); userDao.addUser(user); } /** * 测试查询用户 * 正常结果应输出该用户的信息 */ @Test public void testFindUser() { User user = userDao.findUserById("111"); System.out.println(user); } /** * 测试修改用户信息 * 正常结果应为数据库中的密码修改成功 */ @Test public void testUpdateUser() { //有多个参数时,可使用容器封装多个参数,再传入映射器中 Map<String,String> params = new HashMap<String,String>(); params.put("userId", "111"); params.put("password", "888888"); userDao.updateUser(params); } /** * 测试修改用户信息 * 正常结果应为数据库中的该用户信息被成功删除 */ @Test public void testDeleteUser() { userDao.removeUserById("111"); } }

以上测试结果均正常后再进行下一步操作
这样的目的是进行分步分块测试,当代码量大时便于排错

五、写service、controller层并配置访问路径
   5.1) 在java/main/下创建service包,用于存放业务层代码
 创建UserService.java接口及其实现类UserServiceImpl.java
UserService.java代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
package service; public interface UserService { //添加用户 public void addUser(String username,String password,String nick); /* * 其余删除、查询、修改操作这儿就不写了,你们可以练习下 */ }

UserServiceImpl.java代码如下:

复制代码
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
package service; import java.util.UUID; import javax.annotation.Resource; import org.springframework.stereotype.Service; import dao.UserDao; import entity.User; //纳入spring容器中 @Service("userService") public class UserServiceImpl implements UserService { @Resource private UserDao userDao; /** * 业务----添加用户 * @param username * @param password * @param nick */ public void addUser(String username,String password,String nick) { //UUID用于获生成唯一的字符串,可用于主键的生成 UUID id = UUID.randomUUID(); String userId = id.toString(); User user = new User(userId,username,password,nick); userDao.addUser(user); } }

当业务层代码比较复杂时也应向上面dao层一样做单元测试,这儿我就省略了

   5.2) 在java/main/下创建controller包,用于存放控制层代码(这儿也可以向service层一样分为接口与实现类,便于扩展,此处我就不写了)
 创建UserController.java,如下:

复制代码
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
package controller; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import service.UserService; @RequestMapping("/user") @Controller public class UserController { @Resource private UserService userService; @RequestMapping("/addUser.do") //向页面返回json数据 @ResponseBody public String addUser(String username,String password,String nick) { //当请求路径为/user/addUser.do时,执行添加用户操作 userService.addUser(username, password, nick); System.out.println("username:"+username+"npassword:"+password+"nnick:"+nick); /* * 后续可做返回页面或是返回json数据等其它操作 * 此处向页面返回一个json数据 * 如果前端页面乱码需再做相关配置,此处不是重点,便省略 */ return "username:"+username; } /** * 其余请求操作这儿省略,你们可以当作练习 */ }

   5.3) 在java/resource/下创建spring-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
25
26
27
28
29
<?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:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/tx/spring-jms-3.2.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/tx/spring-lang-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/util http://www.springframework.org/schema/util/spring-util-2.0.xsd"> <!-- 配置注解的组件扫描 --> <context:component-scan base-package="service" /> <context:component-scan base-package="controller" /> </beans>

   5.4) 在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
<?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_2_5.xsd" version="2.5"> <display-name>TestSSM</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>

经过以上操作后,一个简单的基于SSM框架的项目就完成了,接下来进行访问测试,在浏览器地址栏输入请求信息进行测试,查看返回数据及数据库中的数据即可,测试路径如下:

复制代码
1
2
http://localhost:8088/TestSSM/user/addUser.do?username=张三&password=111111&nick=老张

注:对代码进行分层dao+service+controller…,也是基于MVC的设计思想,每一层做自己的事,简单来说就是便于维护和扩展

我的示例源码可点链接:
链接:https://pan.baidu.com/s/1AXqEggqYrFbD77K83VNwNQ
提取码:6d7a

打字不易,谢谢浏览,如有不懂的地方可以回复,我给你们解答

最后

以上就是明理小丸子最近收集整理的关于eclipse手动搭建SSM框架的全部内容,更多相关eclipse手动搭建SSM框架内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(75)

评论列表共有 0 条评论

立即
投稿
返回
顶部