原生SSM框架整合(没有使用Maven)

mac2026-05-28  4

首先,SSM框架的整合除了需要各大框架本身的jar包之外,

还需要一个mybatis-spring的jar包,不然无法完成整合操作

首先来看整个项目的结构:

jsp文件夹:由于我们编写的jsp文件里面可能包含业务逻辑,因此为了保证安全性,将其放置在客户端无法访问的WEB-INF目录下

resource静态资源文件:由于静态资源一般都是些css,html或者JavaScript文件,客户端访问网站的时候回自动下载到客户机上,因此并没有安全性可言,所以我们不必将resource文件夹放置在WEB-INF目录下,只需将其放置在项目的根目录下即可

 Web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <!-- Spring配置文件 --> <!-- 配置Spring配置文件的名称和路径 --> <context-param> <param-name>contextConfigLocation</param-name> <!-- 配置Spring配置文件的完整路径 --> <param-value>classpath:com/config/Spring/Spring.xml</param-value> </context-param> <!-- 配置ContextLoaderListener监听器 --> <!-- 当 web项目启动的时候,会把 Spring IOC 容器获取到,并放入 application 域中 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- SpringMVC配置文件 --> <!-- 配置SpringMVC的拦截器DispatcherServlet类,用以拦截所有的URL请求 --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 若不配置<contextConfigureLocation>节点,则SpringMVC配置文件的命名规则必须满足 文件名-servlet.xml,比如你的配置文件名为:SpringMVC,那完整的文件名必须为:SpringMVC-servlet.xml 否则将无法扫描到对应的配置文件! --> <init-param> <!-- <contextConfigureLocation>节点配置的是SpringMVC配置文件的位置 --> <param-name>contextConfigLocation</param-name> <!-- "classpath:" 路径对应的是工程目录下的src文件 --> <!-- 我的SpringMVC文件放在com.config包下,所以对应的路径即为: classpath:com/config/SpringMVC/springMVC-servlet.xml --> <param-value>classpath:com/config/SpringMVC/springMVC-servlet.xml</param-value> </init-param> <!-- 这个配置是使DispatchServlet拦截器随着Web服务启动的时候启动,而不是当客户端发送请求时才启动 --> <load-on-startup>1</load-on-startup> </servlet> <!-- 配置<servlet-mapping>节点 --> <servlet-mapping> <!-- 注意:这里的<servlet-name>节点的值需要与<servlet>节点的值保持一致 --> <servlet-name>springMVC</servlet-name> <!-- 配置想要拦截的url请求格式, "/" 表示拦截所有请求 --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 过滤器,用于处理表单传值中的POST方式传值出现的中文乱码 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>

Spring配置文件

<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 这个是Spring的配置文件 --> <!-- 开启Spring的注解,若不添加该项,将来使用@Autowired注解时会报错--> <context:annotation-config /> <!-- 扫描包,将指定包下的含有特殊注解的对象(如@Component,@Service等...)交给Spring IOC容器管理 --> <context:component-scan base-package="com.service" /> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/shop?characterEncoding=UTF-8&amp;useSSL=false"></property> <property name="username" value="root"></property> <property name="password" value="admin"></property> </bean> <!-- 配置sqlSession属性 --> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- typeAliasesPackage:它一般对应我们的实体类所在的包,这个时候会自动取对应包中不包括包名的 简单类名作为包括包名的别名。多个package之间可以用逗号或者分号等来进行分隔。(value的值一定要是包的全名) --> <property name="typeAliasesPackage" value="com.pojo"></property> <!-- 这个属性是必须的 --> <property name="dataSource" ref="dataSource"></property> <!-- mappLocations: 它表示我们存放Mapper.xml文件的位置 --> <property name="mapperLocations" value="classpath:com/mapper/*.xml"></property> </bean> <!-- 自动扫描 将Mapper接口生成代理注入到Spring Mybatis在与Spring集成的时候可以配置MapperFactoryBean来生成Mapper接口的代理 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.mapper"></property> </bean> </beans>

SpringMVC配置文件

<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 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-4.3.xsd"> <context:annotation-config/> <!-- 自动扫描包,我们只扫描com.controller包下的类即可,其他的由Spring配置文件管理即可 --> <context:component-scan base-package="com.controller"></context:component-scan> <!-- 注解驱动(代替了处理器映射器和处理器适配器) --> <mvc:annotation-driven/> <!-- 处理静态资源请求 --> <mvc:default-servlet-handler/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>

数据表脚本代码:

CREATE TABLE `s_user` ( `userid` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, `email` varchar(255) DEFAULT NULL, `phone` varchar(255) DEFAULT NULL, `status` varchar(255) NOT NULL DEFAULT '0', `code` varchar(255) DEFAULT NULL, PRIMARY KEY (`userid`) ) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;

实体类(即POJO)User:

package com.pojo; public class User { private Integer userid; private String username; private String password; private String email; private String phone; private Integer status; private String code; 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; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } @Override public String toString() { return "User [userid=" + userid + ", username=" + username + ", password=" + password + ", email=" + email + ", phone=" + phone + ", status=" + status + ", code=" + code + "]"; } }

UserMapper.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"> <select id="getAllUsers" resultType="User"> SELECT * FROM s_user </select> <select id="getUserById" parameterType="int" resultType="User"> SELECT * FROM s_user WHERE userid = #{userid} </select> <insert id="insertUser" parameterType="User" useGeneratedKeys="true" keyProperty="userid"> INSERT INTO s_user(username,password,email,phone) VALUES(#{username},#{password},#{email},#{phone}) </insert> <update id="updateUserById" parameterType="User"> UPDATE s_user SET username = #{username}, password = #{password}, email = #{email}, phone = #{phone} WHERE userid = #{userid} </update> <delete id="deleteUserById" parameterType="int"> DELETE FROM s_user WHERE userid = #{userid} </delete> </mapper>

UserMapper.java接口:

package com.mapper; import com.pojo.User; import java.util.*; public interface UserMapper { public List<User> getAllUsers(); public User getUserById(int userid); public boolean deleteUserById(int userid); public boolean insertUser(User user); public boolean updateUserById(User user); }

注意:XXMapper.xml配置文件和XXMapper.java接口必须放置在同一包下,否则初始化Spring的时候可能会报错;

业务逻辑层

1.UserService接口:

package com.service; import com.pojo.User; import java.util.*; public interface UserService { public List<User> getAllUsers(); public User getUserById(int userid); public boolean deleteUserById(int userid); public boolean insertUser(User user); public boolean updateUserById(User user); }

2.UserServiceImpl接口实现类:

 

package com.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.mapper.UserMapper; import com.pojo.User; import com.service.UserService; //使用@Service注解标记该类,表明该类属于业务逻辑层,并由Spring IOC容器接手其生命周期 @Service public class UserServiceImpl implements UserService { //使用@Autowired注解标记该对象后,在之后使用该对象则会有IOC分配,无需手动new出来; @Autowired UserMapper userMapper; @Override public List<User> getAllUsers() { // TODO Auto-generated method stub return userMapper.getAllUsers(); } @Override public User getUserById(int userid) { // TODO Auto-generated method stub return userMapper.getUserById(userid); } @Override public boolean deleteUserById(int userid) { // TODO Auto-generated method stub return userMapper.deleteUserById(userid); } @Override public boolean insertUser(User user) { // TODO Auto-generated method stub return userMapper.insertUser(user); } @Override public boolean updateUserById(User user) { // TODO Auto-generated method stub return userMapper.updateUserById(user); } }

Controller控制层

1.UserController控制器类

package com.controller; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import com.pojo.User; import com.service.UserService; import com.service.impl.UserServiceImpl; //使用@Controller注解标注该类,表明此类为控制器类; @Controller public class UserController { @Autowired UserService userService; @RequestMapping(value="/user") public String user(Map<Object,Object> map) { List<User> userList = userService.getAllUsers(); map.put("user", userList); return "user"; } }

user.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>User</title> </head> <body> <h1>${requestScope.user}</h1> </body> </html>

启动服务器并打开浏览器,在浏览器地址栏输入:

http://localhost:8080/Shop/user

得到以下结果:

至此,最基础的SSM框架搭建完成,后续学习了Maven可以再用Maven搭建一次SSM框架~~~

 

转自:https://blog.csdn.net/weixin_39549527/article/details/81089435

最新回复(0)