快速搭建SSM框架【详细】

文章目录

  • 一、开发环境准备
  • 二、搭建SSM
    • 2.1新建Maven项目
    • 2.2项目整体结构
    • 2.3spring-config.xml配置
    • 2.4jdbc.properties配置
    • 2.5mybatis-config.xml配置
    • 2.6spring-mvc.xml配置
    • 2.7web.xml配置
    • 2.8index.jsp
    • 2.9ok.jsp
    • 2.10Java代码
    • 2.11测试

一、开发环境准备

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

二、搭建SSM

2.1新建Maven项目

【File】-> 【new Project】-> 【选择Archetype】
快速搭建SSM框架【详细】_第1张图片

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框架【详细】_第2张图片

2.3spring-config.xml配置


<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"/>

    
    <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>

    
    <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="myDataSource"/>
        
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    bean>

     

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        <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配置


DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    
    <properties resource="jdbc.properties"/>

    <settings>
        
        <setting name="logImpl" value="STDOUT_LOGGING"/>

        
        <setting name="autoMappingBehavior" value="PARTIAL"/>

        
        <setting name="mapUnderscoreToCamelCase" value="true"/>

        
        <setting name="lazyLoadingEnabled" value="true"/>
        
    settings>

    
    <mappers>
        
        <package name="com.shenxm.mapper"/>
    mappers>
configuration>

2.6spring-mvc.xml配置


<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配置


<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-name>CharacterEncodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

    
    <servlet>
        <servlet-name>DispatcherServletservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring-mvc.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServletservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>

    <context-param>
        
        <param-name>contextConfigLocationparam-name>
        
        <param-value>classpath:spring-config.xmlparam-value>
    context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
web-app>

2.8index.jsp

<%@ page contentType="text/html;charset=UTF-8"  pageEncoding="utf-8" language="java" isELIgnored="false" %>


测试

创建学生用户信息

姓名:
年龄:

2.9ok.jsp

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


    成功
    

添加成功,这是一个跳转页面!!!

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


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框架【详细】_第3张图片

访问页面:(成功)

快速搭建SSM框架【详细】_第4张图片

你可能感兴趣的:(Java框架,mybatis,java,spring)