SSM整合
创始人
2024-03-02 13:06:43
0

整合的思路是:

   先创建spring框架

   通过spring整合spring mvc

   通过spring整合mybatis

工程创建

   创建Maven工程–>create for archtype–>webapp

创建项目结构

在recourses目录下创建 dbconfig.propertieslog4j.properties、mysqlConfig.xml、springmvc.xml、applicationContext.xml

在pom.xml添加相关依赖

 junitjunit4.12commons-loggingcommons-logging1.2log4jlog4j1.2.17mysqlmysql-connector-java5.1.41com.mchangec3p00.9.5.1com.mchangemchange-commons-java0.2.10org.mybatismybatis3.4.2org.mybatismybatis-spring1.3.1taglibsstandard1.1.2jarjavax.servletjstl1.2jarjavax.servlet.jspjavax.servlet.jsp-api2.3.3providedjavax.servletjavax.servlet-api4.0.1providedcom.fasterxml.jackson.corejackson-annotations2.8.6com.fasterxml.jackson.corejackson-core2.8.1com.fasterxml.jackson.corejackson-databind2.7.5org.springframeworkspring-core4.3.7.RELEASEorg.springframeworkspring-beans4.3.7.RELEASEorg.springframeworkspring-context4.3.7.RELEASEorg.springframeworkspring-expression4.3.7.RELEASEorg.springframeworkspring-aop4.3.7.RELEASEorg.springframeworkspring-aspects4.3.7.RELEASEorg.aspectjaspectjrt1.8.13org.aspectjaspectjweaver1.8.13aopallianceaopalliance1.0org.springframeworkspring-jdbc4.3.7.RELEASEorg.springframeworkspring-tx4.3.7.RELEASEorg.springframeworkspring-web4.3.7.RELEASEorg.springframeworkspring-webmvc4.3.7.RELEASEorg.springframeworkspring-test4.3.7.RELEASE

基本思路

建立spring框架

1.创建实体

public class user implements Serializable {private Integer id;private String name;private String gender;private String email;@Overridepublic String toString() {return "user{" +"id=" + id +", name='" + name + '\'' +", gender='" + gender + '\'' +", email='" + email + '\'' +'}';}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}
}

 2.创建DAO

public interface userDao {public List findAll();
}

3.userService的接口

package club.twzw.service;import club.twzw.bean.User;
import org.springframework.stereotype.Service;import java.util.List;public interface UserService {public List findAll();}

4.实现userServiceImpl,并给userServiceImpl 起别名userService

@Service("userService")		
public class userServiceImpl implements userService {@Overridepublic List findAll() {System.out.println("查询所有用户。。。");return null;//service.findAll();}
}

5.在resources文件下创建文件applicationContext.xml



6.书写测试方法

    @Testpublic void testQueryUserList() {ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");userService service = (userService) ac.getBean("userService"); // 因为给service起了别名,所以通过id的方式获取classservice.findAll();}

运行效果:

建立spring mvc

在web.xml文件中修改并配置过滤器,中文乱码过滤


spring-dispatcherorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:springmvc.xml1spring-dispatcher/characterEncodingFilterorg.springframework.web.filter.CharacterEncodingFilterencodingUTF-8characterEncodingFilter/

在resources文件夹下创建springmvc.xml文件,开启注解扫描,视图解析器以及过滤静态资源和springmvc注解支持



书写controller

package club.twzw.controller;import club.twzw.bean.User;
import club.twzw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;import java.util.List;@Controller
public class UserController {@Autowiredprivate UserService service;@RequestMapping("/findAll")public String findAll(Model model){System.out.println("success");List all = service.findAll();model.addAttribute("user",all);return "list";}
}

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
查看所有用户

success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
scuccess

scuccess

页面如果做成显示,自此已成功添加spring mvc框架

在整合之前需要明白,我们需要在controller中调用service,最快捷的便是使用依赖注入,而至今使用Tomcat服务器只加载了springmvc.xml文件,并没有applicationContext.xml的加载(也就是spring并没有被加载),所以可以通过监听ServeltContext域对象,在创建时加载spring的配置文件(applicationContext.xml)

配置监听器

在web.xml文件下添加listener,context-param设置监听和applicationContext.xml的文件路径


    					<<<<------org.springframework.web.context.ContextLoaderListener											<<<<------contextConfigLocationclasspath:applicationContext.xmlspring-dispatcherorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:springmvc.xml1spring-dispatcher/characterEncodingFilterorg.springframework.web.filter.CharacterEncodingFilterencodingUTF-8characterEncodingFilter/

这样我们就可以使用依赖注入了

使用依赖注入并从新发布,如果正常,就可以在控制台看到两句话

package club.twzw.controller;
import club.twzw.service.impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class UserController {@Autowiredprivate UserService service;@RequestMapping("/findAll")public String findAll(){System.out.println("success");service.findAll();return "success";}
}

这样spring mvc 就已经整合完毕了

建立mybatis环境

在UserDao中使用注解查询

package club.twzw.dao;import club.twzw.bean.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;import java.util.List;
@Repository
public interface UserDao {@Select("select * from user")public List findAll();}

创建mybatis的核心配置文件mysqlConfig.xml




书写测试方法

    @Testpublic void test() throws IOException {InputStream resourceAsStream = Resources.getResourceAsStream("mysqlConfig.xml");SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();SqlSessionFactory factory = builder.build(resourceAsStream);SqlSession session = factory.openSession(true);UserDao mapper = session.getMapper(UserDao.class);List all = mapper.findAll();for (User user : all) {System.out.println(user);}}

输出结果:

说明了mybatis可用,那么可以开始整合了
整理mybatis,思路相同,同样使用依赖注入,将mysqlConfig.xml添加到容器中,并自动注入

在spring的文件中整合mybatis,配置连接池,factory,dao所在的包,此时有无将mysqlConfig.xml都不重要!




使用依赖注入

在dao类上添加@Repository注解

@Repository
public interface UserDao {@Select("select * from user")public List findAll();
}

在serviceimpl中注入接口

package club.twzw.service.impl;import club.twzw.bean.User;
import club.twzw.dao.UserDao;
import club.twzw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;
@Service("UserService")
public class UserServiceImpl implements UserService {@Autowiredprivate UserDao dao;public List findAll() {System.out.println("查询所有用户。。。");return dao.findAll();}
}

修改controllerfindAll方法

package club.twzw.controller;import club.twzw.bean.User;
import club.twzw.service.impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;import java.util.List;@Controller
public class UserController {@Autowiredprivate UserService service;@RequestMapping("/findAll")public String findAll(Model model){System.out.println("success");List all = service.findAll();model.addAttribute("user",all);return "list";}
}

添加list.jsp

<%--Created by IntelliJ IDEA.User: AdministratorDate: 2019/10/29Time: 21:07To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Title

${user.id}${user.name}${user.gender}${user.email}

实现访问

实现插入

controller

    @RequestMapping("/save")public String save(Model model){System.out.println("success");User u = new User();u.setEmail("184611875@qq.com");u.setGender("男");u.setName("comi");boolean b = service.Save(u);System.out.println(b);return "success";}

UserService

package club.twzw.service;import club.twzw.bean.User;
import org.springframework.stereotype.Service;import java.util.List;public interface UserService {public List findAll();public boolean Save(User u);
}

UserServiceImpl

package club.twzw.service.impl;import club.twzw.bean.User;
import club.twzw.dao.UserDao;
import club.twzw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;
@Service("UserService")
public class UserServiceImpl implements UserService {@Autowiredprivate UserDao dao;public List findAll() {System.out.println("查询所有用户。。。");return dao.findAll();}@Overridepublic boolean Save(User u) {return dao.Save(u);}
}

dao

package club.twzw.dao;import club.twzw.bean.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;import java.util.List;
@Repository
public interface UserDao {@Select("select * from user")public List findAll();@Insert("insert into user (name,gender,email) values(#{name},#{gender},#{email})")public boolean Save(User u);
}

输出:

 访问路径(http://localhost:8080/ssmWork_war_exploded/save)
这样我们的ssm框架就完成整合了,可以去干大事了!!!!
码云开源库:码云链接


本文参考自:手把手教你 SSM 整合(非常非常非常非常非常详细)_风吹走了我脑壳后面的秀发的博客-CSDN博客_ssm整合

相关内容

热门资讯

AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AWR报告解读 WORKLOAD REPOSITORY PDB report (PDB snapshots) AW...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
Azure构建流程(Power... 这可能是由于配置错误导致的问题。请检查构建流程任务中的“发布构建制品”步骤,确保正确配置了“Arti...
群晖外网访问终极解决方法:IP... 写在前面的话 受够了群晖的quickconnet的小水管了,急需一个新的解决方法&#x...
AWSECS:哪种网络模式具有... 使用AWS ECS中的awsvpc网络模式来获得最佳性能。awsvpc网络模式允许ECS任务直接在V...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...