热门课程

免费试听

上课方式

开班时间

当前位置: 首页 -   文章 -   新闻动态 -   正文

IDEA快速搭建SSM框架(新手必备)

知了堂姐
2024-07-09 11:12:24
0

第一步:新建MAVEN项目

第二步:搭建项目整体架构

项目结构分析:(和三层架构类似)

  1. .controller层:控制层,处理页面请求interceptor:拦截器mapper:后台持久层(相当于三层架构中的Dao层)pojo:实体类service:业务访问层resource:存放SSM中的配置文件,方便管理webapp-static:存放静态文件,js,css,images等WEB-INF-page:存放jsp页面test-java:junit单元测试

第三步: 配置maven:pom.xml文件



    4.0.0
    cn.testssm
    TestSSM
    1.0-SNAPSHOT
    war
    TestSSM Maven Webapp
    http://www.example.com
    
        UTF-8
        1.7
        1.7
        
        UTF-8
        UTF-8
        
        
        5.0.2.RELEASE
        
        3.4.5
        
        1.6.6
        1.2.12
        
        5.1.6
    
    
    
        
        
            org.aspectj
            aspectjweaver
            1.6.8
        
        
            org.springframework
            spring-aop
            ${spring.version}
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-web
            ${spring.version}
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
        
            org.springframework
            spring-test
            ${spring.version}
        
        
            org.springframework
            spring-tx
            ${spring.version}
        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
        
            junit
            junit
            4.12
        
        
        
            mysql
            mysql-connector-java
            ${mysql.version}
        
        
        
            javax.servlet
            servlet-api
            2.5
            provided
        
        
            javax.servlet.jsp
            jsp-api
            2.0
            provided
        
        
        
            jstl
            jstl
            1.2
        
        
        
            log4j
            log4j
            ${log4j.version}
        
        
            org.slf4j
            slf4j-api
            ${slf4j.version}
        
        
            org.slf4j
            slf4j-log4j12
            ${slf4j.version}
        
        
        
        
            org.mybatis
            mybatis
            ${mybatis.version}
        
        
            org.mybatis
            mybatis-spring
            1.3.0
        
        
        
        
            c3p0
            c3p0
            0.9.1.2
            jar
            compile
        
        
        
            com.fasterxml.jackson.core
            jackson-core
            2.9.8
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.9.8
        
        
        
            commons-fileupload
            commons-fileupload
            1.3.1
        
        
            commons-io
            commons-io
            2.6
        
        
        
            com.alibaba
            fastjson
            1.2.60
        
    
    
        TestSSM
        
            
                
                    maven-clean-plugin
                    3.1.0
                
                
                
                    maven-resources-plugin
                    3.0.2
                
                
                    maven-compiler-plugin
                    3.8.0
                
                
                    maven-surefire-plugin
                    2.22.1
                
                
                    maven-war-plugin
                    3.2.2
                
                
                    maven-install-plugin
                    2.5.2
                
                
                    maven-deploy-plugin
                    2.8.2
                
            
        
        
        
            
                src/main/java
                
                    **/*.xml
                
            
            
                src/main/resources
                
                    **/*.xml
                    **/*.properties
                    **/*.html
                
            
        
    

第四步:编写四大配置文件

一.applicationContext.xml



    
    
    
    
        
        
        
        
        
        
        
        
    
    
    
        
        
        
        
        
    
    
    
        
        
        
    
    
    
    
    
    
        
    
    
    
        
            
            
            
            
        
    
    
    
        
    

二:mybatis.xml




    
    
        
        
        
        
    
    
        
        
    

三:spring-mvc.xml



    
    
    
    
    
    
    
        
        
            
                
            
            
            
                
                    
                        text/html;charset=UTF-8
                        application/json
                    
                
                
                    
                        WriteMapNullValue
                        QuoteFieldNames
                    
                
            
        
    
    
    
    
        
            
            
        
    

四:web.xml



    Archetype Created Web Application
    
    
        characterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
        
            forceEncoding
            true
        
    
    
        characterEncodingFilter
        /*
    
    
    
        contextConfigLocation
        
        classpath:applicationContext.xml
    
    
    
        org.springframework.web.context.ContextLoaderListener
    
    
    
        dispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:spring-mvc.xml
        
        
        1
    
    
        dispatcherServlet
        /
    
    
        /WEB-INF/pages/login.jsp
    

第五步: jdbcConfig.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root

第六步: log4j.properties

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=info, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

第七步:编写Junit单元测试

/**
 * 测试类
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class TestSSM {
    @Resource
    private ApplicationContext ac;
    @Test
    public void testUserLogin() {
        UserService userService = (UserService) ac.getBean("userService", UserService.class);
        User user = new User();
        user.setUserName("admin");
        user.setUserPassword("admin");
        Integer result = userService.userLogin(user);
        System.out.println(result);
        System.out.println("送悟空");
    }
}

测试框架整合

ok,框架基本上就是已将搭建完成了,下面创建的测试界面,看框架能否正常运行。

第一步:编写User实体类

package cn.test.pojo;
import java.io.Serializable;
public class User implements Serializable {
    private Integer id;
    private String userName;
    private String userPassword;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getUserPassword() {
        return userPassword;
    }
    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", userPassword='" + userPassword + '\'' +
                '}';
    }
}

第二步: 编写持久层(mapper)

这里因为是刚学习ssm,所以将xml文件和接口放在了同一包下。

  1. UserMapper.java
  2. ```java package cn.test.mapper;
  3. import cn.test.pojo.User;
  4. public interface UserMapper {
/**
 * 用户登录
 *
 * @param user
 * @return
 */
public Integer userLogin(User user);

}

2. UserMapper.xml
   ```xml
   
   
   
       
   

第三步:编写业务层(Service)

UserService.java

```java package cn.test.service;

import cn.test.pojo.User;

public interface UserService {

public Integer userLogin(User user);

}

2. UserServiceImpl.java
   ```java
   package cn.test.service.impl;
   import cn.test.mapper.UserMapper;
   import cn.test.pojo.User;
   import cn.test.service.UserService;
   import org.springframework.stereotype.Service;
   import javax.annotation.Resource;
   @Service("userService")
   public class UserServiceImpl implements UserService {
       @Resource
       private UserMapper userMapper;
       @Override
       public Integer userLogin(User user) {
           return userMapper.userLogin(user);
       }
   }

第四步:编写Controller

package cn.test.controller;
import cn.test.pojo.User;
import cn.test.service.UserService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("/userController")
public class UserController {
    @Resource
    private UserService userService;
    @RequestMapping("/userLogin")
    public String userLogin(User user) {
        System.out.println(user);
        Integer result = userService.userLogin(user);
        return "";
    }
    @RequestMapping("/testAjax")
    @ResponseBody
    public String testAjax(User user) {
        System.out.println(user);
        Map map = new HashMap();
        map.put("data", user);
        return JSONArray.toJSONString(map);
    }
}

第五步:jsp页面

<%--
  Created by IntelliJ IDEA.
  User: Dell
  Date: 2020/8/8
  Time: 21:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>


    Title


测试登录

账号*

密码*

启动服务器,在浏览器地址输入http://localhost:8080/index.jsp

 

 

整合成功

新手可能会出现的问题,以下整理了解决方法:

  1. junit单元测试时提示:Can not find Class :cn.testssm.test 解决方案:因为开发工具使用的时idea,当初在建包时将java名称改成了test,重新改成java就好了

2.我使用的spring版本时5.0.2,spring5开始要求junit单元测试版本必须>=4.1.2

3.Error creating bean with name ‘org.springframework.web.servlet.mvc.method.anRequestMappingHandlerAda

4.jsp页面不识别el表达式

解决方案:在头部加入: isELIgnored=“false”

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

5.控制台输出中文乱码:

-Dfile.encoding=utf-8

解决方案:

end~

大家都在看

被Chatgpt碾压的打工人与大学生,准备反击!

2024-07-09 浏览次数:0

网络安全可以从事什么职业?网络安全相关岗位速览

2024-07-09 浏览次数:0

学Java要学数据结构吗?数据结构的重要性总结

2024-07-09 浏览次数:0

java开发需要掌握的技术有哪些?java开发人...

2024-07-09 浏览次数:0

网络工程师是青春饭吗?揭秘网络安全工程师的职业发...

2024-07-09 浏览次数:0

cisp考试含金量大吗?

2024-07-09 浏览次数:0
最新资讯
IDEA+Mybatis-ge... MyBatis Generator简称MBG,是MyBatis 官方出的代码生成器。MBG能够自动生...
IDEA快速搭建SSM框架(新... .controller层:控制层,处理页面请求interceptor:拦截器mapper:后...
IDEA使用技巧以及如何连接数... IDEA使用技巧以及连接数据库 IDEA真是越用越觉得它强大,它总是在我们写代码的时候,不时给...
IDEA+Mybatis-ge... MyBatis Generator简称MBG,是MyBatis 官方出的代码生成器。MBG能够自动生...
UTF-8 UTF-8 5.0.2.RELEASE 3.4.5 1.6.6 1.2.12 5.1.6 org.aspectj aspectjweaver 1.6.8 org.springframework spring-aop ${spring.version} org.springframework spring-context ${spring.version} org.springframework spring-web ${spring.version} org.springframework spring-webmvc ${spring.version} org.springframework spring-test ${spring.version} org.springframework spring-tx ${spring.version} org.springframework spring-jdbc ${spring.version} junit junit 4.12 mysql mysql-connector-java ${mysql.version} javax.servlet servlet-api 2.5 provided javax.servlet.jsp jsp-api 2.0 provided jstl jstl 1.2 log4j log4j ${log4j.version} org.slf4j slf4j-api ${slf4j.version} org.slf4j slf4j-log4j12 ${slf4j.version} org.mybatis mybatis ${mybatis.version} org.mybatis mybatis-spring 1.3.0 c3p0 c3p0 0.9.1.2 jar compile com.fasterxml.jackson.core jackson-core 2.9.8 com.fasterxml.jackson.core jackson-databind 2.9.8 commons-fileupload commons-fileupload 1.3.1 commons-io commons-io 2.6 com.alibaba fastjson 1.2.60 TestSSM maven-clean-plugin 3.1.0 maven-resources-plugin 3.0.2 maven-compiler-plugin 3.8.0 maven-surefire-plugin 2.22.1 maven-war-plugin 3.2.2 maven-install-plugin 2.5.2 maven-deploy-plugin 2.8.2 src/main/java **/*.xml src/main/resources **/*.xml **/*.properties **/*.html

第四步:编写四大配置文件

一.applicationContext.xml



    
    
    
    
        
        
        
        
        
        
        
        
    
    
    
        
        
        
        
        
    
    
    
        
        
        
    
    
    
    
    
    
        
    
    
    
        
            
            
            
            
        
    
    
    
        
    

二:mybatis.xml




    
    
        
        
        
        
    
    
        
        
    

三:spring-mvc.xml



    
    
    
    
    
    
    
        
        
            
                
            
            
            
                
                    
                        text/html;charset=UTF-8
                        application/json
                    
                
                
                    
                        WriteMapNullValue
                        QuoteFieldNames
                    
                
            
        
    
    
    
    
        
            
            
        
    

四:web.xml



    Archetype Created Web Application
    
    
        characterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
        
            forceEncoding
            true
        
    
    
        characterEncodingFilter
        /*
    
    
    
        contextConfigLocation
        
        classpath:applicationContext.xml
    
    
    
        org.springframework.web.context.ContextLoaderListener
    
    
    
        dispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:spring-mvc.xml
        
        
        1
    
    
        dispatcherServlet
        /
    
    
        /WEB-INF/pages/login.jsp
    

第五步: jdbcConfig.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root

第六步: log4j.properties

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=info, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

第七步:编写Junit单元测试

/**
 * 测试类
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class TestSSM {
    @Resource
    private ApplicationContext ac;
    @Test
    public void testUserLogin() {
        UserService userService = (UserService) ac.getBean("userService", UserService.class);
        User user = new User();
        user.setUserName("admin");
        user.setUserPassword("admin");
        Integer result = userService.userLogin(user);
        System.out.println(result);
        System.out.println("送悟空");
    }
}

测试框架整合

ok,框架基本上就是已将搭建完成了,下面创建的测试界面,看框架能否正常运行。

第一步:编写User实体类

package cn.test.pojo;
import java.io.Serializable;
public class User implements Serializable {
    private Integer id;
    private String userName;
    private String userPassword;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getUserPassword() {
        return userPassword;
    }
    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", userPassword='" + userPassword + '\'' +
                '}';
    }
}

第二步: 编写持久层(mapper)

这里因为是刚学习ssm,所以将xml文件和接口放在了同一包下。

  1. UserMapper.java
  2. ```java package cn.test.mapper;
  3. import cn.test.pojo.User;
  4. public interface UserMapper {
/**
 * 用户登录
 *
 * @param user
 * @return
 */
public Integer userLogin(User user);

}

2. UserMapper.xml
   ```xml
   
   
   
       
   

第三步:编写业务层(Service)

UserService.java

```java package cn.test.service;

import cn.test.pojo.User;

public interface UserService {

public Integer userLogin(User user);

}

2. UserServiceImpl.java
   ```java
   package cn.test.service.impl;
   import cn.test.mapper.UserMapper;
   import cn.test.pojo.User;
   import cn.test.service.UserService;
   import org.springframework.stereotype.Service;
   import javax.annotation.Resource;
   @Service("userService")
   public class UserServiceImpl implements UserService {
       @Resource
       private UserMapper userMapper;
       @Override
       public Integer userLogin(User user) {
           return userMapper.userLogin(user);
       }
   }

第四步:编写Controller

package cn.test.controller;
import cn.test.pojo.User;
import cn.test.service.UserService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("/userController")
public class UserController {
    @Resource
    private UserService userService;
    @RequestMapping("/userLogin")
    public String userLogin(User user) {
        System.out.println(user);
        Integer result = userService.userLogin(user);
        return "";
    }
    @RequestMapping("/testAjax")
    @ResponseBody
    public String testAjax(User user) {
        System.out.println(user);
        Map map = new HashMap();
        map.put("data", user);
        return JSONArray.toJSONString(map);
    }
}

第五步:jsp页面

<%--
  Created by IntelliJ IDEA.
  User: Dell
  Date: 2020/8/8
  Time: 21:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>


    Title


测试登录

账号*

密码*

启动服务器,在浏览器地址输入http://localhost:8080/index.jsp

 

 

整合成功

新手可能会出现的问题,以下整理了解决方法:

  1. junit单元测试时提示:Can not find Class :cn.testssm.test 解决方案:因为开发工具使用的时idea,当初在建包时将java名称改成了test,重新改成java就好了

2.我使用的spring版本时5.0.2,spring5开始要求junit单元测试版本必须>=4.1.2

3.Error creating bean with name ‘org.springframework.web.servlet.mvc.method.anRequestMappingHandlerAda

4.jsp页面不识别el表达式

解决方案:在头部加入: isELIgnored=“false”

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

5.控制台输出中文乱码:

-Dfile.encoding=utf-8

解决方案:

end~

IDEA

上一篇:Nginx基础知识

下一篇:MybatisPlus—kotlin代码生成

相关内容

IDEA+Mybatis-...
MyBatis Generator简称MBG,是MyBatis 官...
2024-07-09 11:12:24
IDEA快速搭建SSM框架...
.controller层:控制层,处理页面请求interc...
2024-07-09 11:12:24
IDEA使用技巧以及如何连...
IDEA使用技巧以及连接数据库 IDEA真是越用越觉得它强大...
2024-07-09 11:12:24
IDEA+Mybatis-...
MyBatis Generator简称MBG,是MyBatis 官...
2024-07-08 17:22:16

热门资讯

就业课程介绍(Java+前端+... Java+大数据,前端全栈,信息安全
关于我们 请输入文章描述
0基础转行信安,他如何做到月薪... 转行并非简单的换份工作,而是我们在职场进行自我认同、重塑身份的一个过程。今天知了小姐姐为大家介绍一位...
【前端每日一题】什么是BFC?... 秋招马上就要开始了,小伙伴们最近在准备面试的东西没呢,知了姐今天将蛋糕哥整理的前端面试题共享出来,同...
cisp考试费用多少?cisp...   随着网络技术的快速发展,网络安全问题变得越来越重要。那么,CISP考试费用多少?CISP报名条件...
pythone 文件和数据格式... 关于 Python 对文件的处理,以下选项中描述错误的是
img标签的onerror事件... 1.img 标签除了 onerror 属性外,还有其他获取管理员路径的办法吗? src 指定一个远程...
网络安全运维岗面试题及答案详解... 在当今数字化时代,网络安全运维工程师的角色变得愈发重要。为了保障网络安全,各个企业都需要拥有一支经验...
知了堂官网V3第一版内测邀请 经过几个月的加班加点,我们终于迎来了知了堂官网3.1.0版本 现正招募内测中
川农第一次线下拓展精彩瞬间 5月15日,知了堂的哥哥姐姐们携手企业拓展教练浩浩荡荡奔赴川农。
-->