SSM框架入门和搭建 十部曲

2024-09-04 04:32
文章标签 ssm 入门 搭建 框架 十部

本文主要是介绍SSM框架入门和搭建 十部曲,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

SSM框架,顾名思义,就是Spring+SpringMVC+mybatis。

通过Spring来将各层进行整合,

通过spring来管理持久层(mybatis),

通过spring来管理handler。

总之,spring是将各层进行整合。

废话不说了,来搭建吧。

共十个步骤,有点啰嗦,但是我觉得挺仔细的。不足之处,指正。

第一步,建立一个动态的web项目。
第二步,建立各个包,并导入各种jar包,我是从网上下载的。如下图:

第三步,建立model类吧。我这边建立一个很简单的类,先不进行配置,配置有点头晕。

建立user类,自建get和set方法,并构造方法:

1 package com.model;
2 
3 public class User {
4     private int id;
5     private String username;
6     private String age;
7 }
第四步,容我先配置一下mybatis的xml文件。

建一个userMapper.xml和UserMapper.java 接口文件 。

先做一个select * from d_user;

因为数据库是这个样子滴,如下图:

配置代码如下:

1 package com.mapper;
2 
3 import java.util.List;
4 
5 import com.model.User;
6 
7 public interface UserMapper {
8     List<User> findAllUser();
9 }

xml文件如下:

<?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="com.mapper.UserMapper"><!-- 解决表名与字段不匹配 --><resultMap type="User" id="userResultMap"><id property="id" column="user_id"/><result property="username" column="user_name"/><result property="age" column="user_age"/></resultMap><select id="findAllUser" resultMap="userResultMap" resultType="User">select * from d_user</select>
</mapper>

mybatis,算是完成一半了,后面的会继续,不会停的。

需要考虑到service了。这就有了第五步了。

第五步,配置service。

首先,要看到我们之前建的两个包,一个是com.service和com.service.impl。

在com.service中,要建立一个UserService类。代码如下:

package com.service;import java.util.List;
import com.model.User;public interface UserService {List<User> findAllUser();
}

另一个,需要用到这个接口,implements它吧。

UserServiceImpl.java代码如下:

package com.service.impl;import java.util.List;import javax.annotation.Resource;import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import com.mapper.UserMapper;
import com.model.User;
import com.service.UserService;@Service
@Transactional
public class UserServiceImpl implements UserService{   @Resourcepublic UserMapper userMapper;@Overridepublic List<User> findAllUser() {// TODO Auto-generated method stubList<User> findAllUser = userMapper.findAllUser();return findAllUser;}
}
@后面的注解一定要加的,不然,会出错的。第六步:开始写controller吧 controller,从单词上就能看到,控制。 写一个UserController类吧。 
 1 package com.controller;2 3 import java.util.List;4 5 import javax.servlet.http.HttpServletRequest;6 7 import org.springframework.beans.factory.annotation.Autowired;8 import org.springframework.stereotype.Controller;9 import org.springframework.web.bind.annotation.RequestMapping;
10 
11 import com.model.User;
12 import com.service.UserService;
13 
14 @Controller
15 @RequestMapping("/user")
16 public class UserController {
17     
18     @Autowired
19     private UserService userService;
20     
21     @RequestMapping("/findAllUser")
22     public String findAllUser(HttpServletRequest request){
23         List<User> listUser =  userService.findAllUser();
24         request.setAttribute("listUser", listUser);
25         return "/allUser";
26     }
27 }

去看他们之间的类,去了解他们之间的关系。我觉得了解很重要。

第七步,开始配置xml文件吧,把文件放在config中。

mybatis-config.xml,配置如下:

<?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><typeAlias alias="User" type="com.model.User"/></typeAliases><mappers><mapper resource="com/mapper/userMapper.xml" /></mappers>
</configuration>

其实,这些代码是在配置mybatis的时候提前敲好的,

<mapper resource="com/mapper/userMapper.xml" />
这行代码是在写完userMapper.xml去写上的。然后就是去写spring配置了:spring-common.xml和spring-mvc.xml依次如下。其实代码都是我百度的,然后自己修改一下,谢谢分享。
  1 <?xml version="1.0" encoding="UTF-8"?>2 <beans xmlns="http://www.springframework.org/schema/beans"3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"4     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"5     xsi:schemaLocation="6         http://www.springframework.org/schema/beans7         http://www.springframework.org/schema/beans/spring-beans-4.0.xsd8         http://www.springframework.org/schema/context9         http://www.springframework.org/schema/context/spring-context-4.0.xsd
10         http://www.springframework.org/schema/tx
11         http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
12 
13     <!-- 1. 数据源 : DriverManagerDataSource -->
14     <bean id="dataSource"
15         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
16         <property name="driverClassName" value="com.mysql.jdbc.Driver" />
17         <property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
18         <property name="username" value="root" />
19         <property name="password" value="root" />
20     </bean>
21 
22     <!--
23         2. mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:引用数据源
24 
25         MyBatis定义数据源,同意加载配置
26     -->
27     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
28         <property name="dataSource" ref="dataSource"></property>
29         <property name="configLocation" value="classpath:config/mybatis-config.xml" /> 
30     </bean>
31 
32     <!--
33         3. mybatis自动扫描加载Sql映射文件/接口 : MapperScannerConfigurer sqlSessionFactory
34 
35         basePackage:指定sql映射文件/接口所在的包(自动扫描)
36     -->
37     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
38         <property name="basePackage" value="com.mapper"></property>
39         <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
40     </bean>
41 
42     <!--
43         4. 事务管理 : DataSourceTransactionManager dataSource:引用上面定义的数据源
44     -->
45     <bean id="txManager"
46         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
47         <property name="dataSource" ref="dataSource"></property>
48     </bean>
49 
50     <!-- 5. 使用声明式事务
51          transaction-manager:引用上面定义的事务管理器
52      -->
53     <tx:annotation-driven transaction-manager="txManager" />
54 
55 </beans>View Code
  1 <?xml version="1.0" encoding="UTF-8"?>2 <beans xmlns="http://www.springframework.org/schema/beans"3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"4     xmlns:mvc="http://www.springframework.org/schema/mvc"5     xsi:schemaLocation="http://www.springframework.org/schema/beans 6     http://www.springframework.org/schema/beans/spring-beans.xsd7     http://www.springframework.org/schema/context8     http://www.springframework.org/schema/context/spring-context-4.0.xsd9     http://www.springframework.org/schema/mvc
10     http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
11 
12     <!-- 注解扫描包 -->
13     <context:component-scan base-package="com" />
14 
15     <!-- 开启注解 -->
16     <mvc:annotation-driven />
17 
18     <!--
19         配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd
20     -->
21     <mvc:resources mapping="/img/**" location="/img/" />
22     <mvc:resources mapping="/js/**" location="/js/" />
23     <mvc:resources mapping="/css/**" location="/css/" />
24     <mvc:resources mapping="/html/**" location="/html/" />
25 
26 
27 
28     <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
29     <bean id="viewResolver"
30         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
31         <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
32         <property name="prefix" value="/WEB-INF/jsp/" />
33         <property name="suffix" value=".jsp" />
34     </bean>
35 </beans>View Code

第八步,就是web.xml文件了。网上一大堆,随便下,我也是从网上拷贝的。然后,在么有错误的情况下,随着性子改。

  1 <?xml version="1.0" encoding="UTF-8"?>2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 5     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">6 7     <!-- 加载Spring容器配置 -->8     <listener>9         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
10     </listener>
11 
12     <!-- 设置Spring容器加载所有的配置文件的路径 -->
13     <context-param>
14         <param-name>contextConfigLocation</param-name>
15         <param-value>classpath*:config/spring-*.xml</param-value>
16     </context-param>
17 
18     <!-- 配置SpringMVC核心控制器 -->
19     <servlet>
20         <servlet-name>springMVC</servlet-name>
21         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
22         
23         <init-param>
24             <param-name>contextConfigLocation</param-name>
25             <param-value>classpath*:config/spring-mvc.xml</param-value>
26         </init-param>
27         <!-- 启动加载一次 -->  
28         <load-on-startup>1</load-on-startup>
29     </servlet>
30 
31     <!--为DispatcherServlet建立映射 -->
32     <servlet-mapping>
33         <servlet-name>springMVC</servlet-name>
34         <!-- 此处可以可以配置成*.do -->
35         <url-pattern>/</url-pattern>
36     </servlet-mapping>
37 
38     <!-- 防止Spring内存溢出监听器 -->
39     <listener>
40         <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
41     </listener>
42 
43     <!-- 解决工程编码过滤器 -->
44     <filter>
45         <filter-name>encodingFilter</filter-name>
46         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
47         <init-param>
48             <param-name>encoding</param-name>
49             <param-value>UTF-8</param-value>
50         </init-param>
51         <init-param>
52             <param-name>forceEncoding</param-name>
53             <param-value>true</param-value>
54         </init-param>
55     </filter>
56     
57     <filter-mapping>
58         <filter-name>encodingFilter</filter-name>
59         <url-pattern>/*</url-pattern>
60     </filter-mapping>
61 
62     <welcome-file-list>
63         <welcome-file>index.jsp</welcome-file>
64     </welcome-file-list>
65 </web-app>View Code
第九步,写一个jsp文件吧,因为才controller中,return的是allUser。就写一个allUser文件吧。
  1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>2 <%3 String path = request.getContextPath();4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";5 %>6 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">8 <html>9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>ssm</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26    <table border="1">
27         <tbody>
28             <tr>
29                 <th>姓名</th>
30                 <th>年龄</th>
31             </tr>
32             <c:if test="${!empty listUser }">
33                 <c:forEach items="${listUser}" var="list">
34                     <tr>
35                         <td>${list.username }</td>
36                         <td>${list.age }</td>
37                         
38                     </tr>                
39                 </c:forEach>
40             </c:if>
41         </tbody>
42     </table>
43   </body>
44 </html>View Code
第十步,执行吧,开启tomcat服务器,输入:

http://localhost:8080/SSM/user/findAllUser

得到的如下图:

把数据库的数据都显示出来了。

这篇关于SSM框架入门和搭建 十部曲的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1135002

相关文章

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1

基于Flask框架添加多个AI模型的API并进行交互

《基于Flask框架添加多个AI模型的API并进行交互》:本文主要介绍如何基于Flask框架开发AI模型API管理系统,允许用户添加、删除不同AI模型的API密钥,感兴趣的可以了解下... 目录1. 概述2. 后端代码说明2.1 依赖库导入2.2 应用初始化2.3 API 存储字典2.4 路由函数2.5 应

Python GUI框架中的PyQt详解

《PythonGUI框架中的PyQt详解》PyQt是Python语言中最强大且广泛应用的GUI框架之一,基于Qt库的Python绑定实现,本文将深入解析PyQt的核心模块,并通过代码示例展示其应用场... 目录一、PyQt核心模块概览二、核心模块详解与示例1. QtCore - 核心基础模块2. QtWid

最新Spring Security实战教程之Spring Security安全框架指南

《最新SpringSecurity实战教程之SpringSecurity安全框架指南》SpringSecurity是Spring生态系统中的核心组件,提供认证、授权和防护机制,以保护应用免受各种安... 目录前言什么是Spring Security?同类框架对比Spring Security典型应用场景传统

使用DeepSeek搭建个人知识库(在笔记本电脑上)

《使用DeepSeek搭建个人知识库(在笔记本电脑上)》本文介绍了如何在笔记本电脑上使用DeepSeek和开源工具搭建个人知识库,通过安装DeepSeek和RAGFlow,并使用CherryStudi... 目录部署环境软件清单安装DeepSeek安装Cherry Studio安装RAGFlow设置知识库总

Linux搭建Mysql主从同步的教程

《Linux搭建Mysql主从同步的教程》:本文主要介绍Linux搭建Mysql主从同步的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux搭建mysql主从同步1.启动mysql服务2.修改Mysql主库配置文件/etc/my.cnf3.重启主库my