快速搭建SSM框架【详细】

这篇具有很好参考价值的文章主要介绍了快速搭建SSM框架【详细】。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、开发环境准备

  • JDK1.8
  • MySQL8
  • idea2021
  • Tomcat8.5.87
  • Apache-Maven3.9

二、搭建SSM

2.1新建Maven项目

【File】-> 【new Project】-> 【选择Archetype】
ssm框架搭建,Java框架,mybatis,java,spring

2.2项目整体结构

项目的整体结构: 包括resources目录下的xml和webapp目录下文件。如果不包含某些文件,就自己创建。

MVC结构、spring-config.xml(applicationContext.xml)、jdbc.properties(数据源)、mybatis-config.xml(mybatis配置类)、spring-mvc.xml(springMVC前端控制器配置类)、web.xml(web项目配置类)

ssm框架搭建,Java框架,mybatis,java,spring

2.3spring-config.xml配置

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!--引入配置类-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--声明数据源DataSource-->
    <bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="url" value="${database.url}"/>
        <property name="username" value="${database.username}"/>
        <property name="password" value="${database.password}"/>
    </bean>

    <!--声明SqlSessionFactoryBean,在这个类的内部,创建SqlSessionFactory-->
    <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--指定数据源-->
        <property name="dataSource" ref="myDataSource"/>
        <!--指定mybatis主配置文件
                resource可以直接使用value赋值
        -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

     <!-- 使用MapperScannerConfigurer自定生成代理实现类
    MapperScan
    nerConfigurer是spring和mybatis整合的mybatis-spring的jar包中提供的一个类。
    -->

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--指定SqlSessionFactory的名称-->
        <property name="sqlSessionFactoryBeanName" value="factory"/>
        <!-- 给指定包下的接口生成代理实现类 -->
        <property name="basePackage" value="com.shenxm.mapper"/>
    </bean>
</beans>

2.4jdbc.properties配置

database.driver=com.mysql.cj.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/demo
database.username=root
database.password=123456

2.5mybatis-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>
    <!--引入jdbc.properties配置文件-->
    <properties resource="jdbc.properties"/>

    <settings>
        <!--配置运行的sql在控制台打印输出-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>

        <!--设置映射方式 autoMappingBehavior属性
                NONE:不自动进行映射
                PARTIAL:默认值,对没有嵌套结果集自动进行映射
                FILL:对所有结果集进行自动映射,一般情况下,如果有结果集我们也使用ReaultMap,所以FULL很少用
        -->
        <setting name="autoMappingBehavior" value="PARTIAL"/>

        <!--驼峰映射
                开启驼峰映射后:例如数据库字段中的nick_name,在PO中属性却是nickName也可以自动匹配上(mybatis会自动将下划线更改成驼峰类型)
        -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>

        <!--开启延迟加载的开关 默认是false -->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!-- &lt;!&ndash;发生延迟加载的时候, 是否完全加载  默认是 false (3.1.4版本之后)  一般不配置 用默认的就可以&ndash;&gt;
         <setting name="aggressiveLazyLoading" value="true"/>-->
    </settings>

    <!--注入映射器-->
    <mappers>
        <!--映射器(xml)所在包-->
        <package name="com.shenxm.mapper"/>
    </mappers>
</configuration>

2.6spring-mvc.xml配置

<?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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 自动注册组件 -->
    <mvc:annotation-driven />
    <!--指定组件扫描路径-->
    <context:component-scan base-package="com.shenxm"/>

</beans>

2.7web.xml配置

<?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_4_0.xsd"
         version="4.0">

    <!--配置全局过滤filter-->
    <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>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--配置springMVC的前端控制器-->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <!--  contextConfigLocation:名称是固定的,表示自定义的spring配置文件的路径  -->
        <param-name>contextConfigLocation</param-name>
        <!--    自定义配置文件的路径  -->
        <param-value>classpath:spring-config.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

2.8index.jsp

<%@ page contentType="text/html;charset=UTF-8"  pageEncoding="utf-8" language="java" isELIgnored="false" %>
<html>
<body>
<h2>测试</h2>
<p>创建学生用户信息</p>
<form action="${pageContext.request.contextPath}/test/add" method="post">
    姓名:<input type="text" name="name"><br/>
    年龄:<input type="text" name="age"><br/>
    <input type="submit" value="注册学生">
</form>
</body>
</html>

2.9ok.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>成功</title>
    <h2>添加成功,这是一个跳转页面!!!</h2>
</head>
<body>
</body>
</html>

2.10Java代码

controller层

package com.shenxm.controller;

import com.shenxm.po.Test;
import com.shenxm.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/test")
public class DemoController {
    @Autowired
    private TestService testService;

    @RequestMapping("/add")
    public ModelAndView addOneTest(Test test){
        ModelAndView modelAndView = new ModelAndView();
        testService.addTest(test);
        System.out.println("addTest方法执行完成");
        modelAndView.setViewName("/ok.jsp");
        return modelAndView;
    }
}

service层接口类:

package com.shenxm.service;
import com.shenxm.po.Test;

public interface TestService {
    int addTest(Test test);
}

service层实现类:

package com.shenxm.service.impl;

import com.shenxm.mapper.TestMapper;
import com.shenxm.po.Test;
import com.shenxm.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TestServiceImpl implements TestService {

    @Autowired
    private TestMapper testMapper;
    @Override
    public int addTest(Test test) {
        int row = testMapper.addTest(test);
        return row;
    }
}

mapper层:

TestMapper.java

package com.shenxm.mapper;
import com.shenxm.po.Test;
import org.springframework.stereotype.Component;

@Component
public interface TestMapper {
    int addTest(Test test);
}

TestMapper.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.shenxm.mapper.TestMapper">
    <insert id="addTest" parameterType="com.shenxm.po.Test" useGeneratedKeys="true" keyProperty="id">
        insert into test values (null,#{name},#{age})
    </insert>
</mapper>

po包:

package com.shenxm.po;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Test {
    /** 自增的id */
    private Integer id;
    /** 姓名 */
    private String name;
    /** 年龄 */
    private Integer age;
}

2.11测试

tomcat8.5配置启动

ssm框架搭建,Java框架,mybatis,java,spring

访问页面:(成功)

ssm框架搭建,Java框架,mybatis,java,spring文章来源地址https://www.toymoban.com/news/detail-709229.html

到了这里,关于快速搭建SSM框架【详细】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • SSM框架的学习与应用(Spring + Spring MVC + MyBatis)-Java EE企业级应用开发学习记录(第五天)MyBatis的注解开发

    ​ 昨天我们深入学习了 MyBatis多表之间的关联映射,了解掌握了一对一关联映射,一对多关联映射,嵌套查询方式以及嵌套结果方式,掌握了缓存机制的一级缓存,二级缓存等概念,也使用了代码进行复现理解 。但是都是基于XML配置文件的方式来实现的,现在我们要学习一下

    2024年02月11日
    浏览(44)
  • SSM框架的学习与应用(Spring + Spring MVC + MyBatis)-Java EE企业级应用开发学习记录(第三天)动态SQL

    昨天我们深入学习了 Mybatis的核心对象SqlSessionFactoryBuilder , 掌握MyBatis核心配置文件以及元素的使用 ,也掌握MyBatis映射文件及其元素的使用。那么今天我们需要掌握的是更加复杂的查询操作。 学会编写MyBatis中动态SQL 学会MyBatis的条件查询操作 学会MyBatis的更新操作 学会MyBati

    2024年02月11日
    浏览(41)
  • IDEA版SSM入门到实战(Maven+MyBatis+Spring+SpringMVC) -Spring搭建框架步骤

    第一章 初识Spring 1.1 Spring简介 Spring是一个为简化企业级开发而生的 开源框架 。 Spring是一个 IOC(DI) 和 AOP 容器框架。 IOC全称:Inversion of Control【控制反转】 将对象【万物皆对象】控制权交个Spring DI全称:(Dependency Injection):依赖注入 AOP全称:Aspect-Oriented Programming,面向切面编

    2024年02月04日
    浏览(34)
  • (第十一天)初识SpringMVC SSM框架的学习与应用(Spring + Spring MVC + MyBatis)-Java EE企业级应用开发学习记录

    今天我们要来学习一下SSM框架的最后一个框架SpringMVC 一、初认SpringMVC 基本概念: ​ Spring MVC(Model-View-Controller)是一个用于构建Java Web应用程序的开源框架,它提供了一种基于MVC架构的方式来开发Web应用 。 ​ SpringMVC是Spring Framework的一部分,它是一种基于模型-视图-控制器(

    2024年02月07日
    浏览(55)
  • Java语言开发在线小说推荐网 小说推荐系统 基于用户、物品的协同过滤推荐算法 SSM(Spring+SpringMVC+Mybatis)开发框架 大数据、人工智能、机器学习开发

    1、开发工具和使用技术 MyEclipse10/Eclipse/IDEA,jdk1.8,mysql5.5/mysql8,navicat数据库管理工具,tomcat,SSM(spring+springmvc+mybatis)开发框架,jsp页面,javascript脚本,jquery脚本,bootstrap前端框架(用户端),layui前端框架(管理员端),layer弹窗组件等。 2、实现功能 前台用户包含:注

    2023年04月26日
    浏览(57)
  • 【java】【SSM框架系列】【一】Spring

    目录 一、简介 1.1 为什么学 1.2 学什么  1.3 怎么学 1.4 初识Spring  1.5 Spring发展史 1.6 Spring Framework系统架构图 1.7 Spring Framework学习线路 二、核心概念(IoC/DI,IoC容器,Bean) 2.1 概念 2.2 IoC入门案例 2.2.1 IoC入门案例思路分析 2.2.2 IoC入门案例  2.3 DI入门案例 2.3.1 DI入门案例思路与

    2024年02月09日
    浏览(35)
  • SSM框架(Spring + SpringMVC + Mybatis)

    MVC即model view controller。(模型,视图,控制器) 用于存放我们的实体类,类中定义了多个类属性,并与数据库表的字段保持一致,一张表对应一个类。 主要用于定义与数据库对象应的属性,提供get/set方法,tostring方法,有参无参构造函数。 数据持久层,先设计接口,然后在配

    2024年02月03日
    浏览(41)
  • Java框架学习(二)SSM体系:Spring、SpringMVC、MybatisPlus

    在原始的分层架构实现中,负责响应请求的Controller层依赖于业务逻辑处理的Service层,而业务逻辑处理的service层又依赖与数据访问Dao层。上下层间相互依赖耦合,耦合的缺陷在于牵一发而动全身,不利于后期维护拓展。 为了分层解耦,Spring采用IoC控制反转和DI依赖注入,来解

    2024年02月11日
    浏览(36)
  • SSM(Spring+SpringMVC+MyBatis)框架集成

    进行SSM(Spring+SpringMVC+MyBatis)集成的主要原因是为了提高开发效率和代码可维护性。SSM是一套非常流行的Java Web开发框架,它集成了Spring框架、SpringMVC框架和MyBatis框架,各自发挥优势,形成了一个完整的开发框架。 首先,使用Spring框架可以实现组件的解耦和依赖注入,通过配

    2024年02月08日
    浏览(48)
  • SSM框架和Spring Boot+Mybatis框架的性能比较?

    SSM框架和Spring Boot+Mybatis框架的性能比较,没有一个绝对的答案,因为它们的性能受到很多因素的影响,例如项目的规模、复杂度、需求、技术栈、团队水平、测试环境、测试方法等。因此,我们不能简单地说哪个框架的性能更好,而是需要根据具体的场景和目标来进行评估和

    2024年02月11日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包