Java开发之手把手教你搭建企业级工程SSM框架

关于Spring + Spring MVC + MyBatis的搭建

1.在IDEA界面中创建MavenWeb工程

选中下图maven-archetype-webapp

Java开发之手把手教你搭建企业级工程SSM框架_第1张图片

2.在pom.xml中添加如下相关依赖



  4.0.0
  org.example
  ssm
  1.0-SNAPSHOT
  war
  ssm Maven Webapp
  
  http://www.example.com
  
    UTF-8
    1.8
    1.8
  
  
    
    
      org.springframework
      spring-webmvc
      5.3.9
    
    
    
      org.springframework
      spring-jdbc
      5.3.9
    
    
    
      org.springframework
      spring-aop
      5.3.9
    
    
      org.springframework
      spring-aspects
      5.3.9
    
    
    
      org.mybatis
      mybatis-spring
      2.0.6
    
    
    
      org.mybatis
      mybatis
      3.4.5
    
    
      mysql
      mysql-connector-java
      8.0.11
    
    
    
      c3p0
      c3p0
      0.9.1.2
    
    
    
      org.projectlombok
      lombok
      1.18.20
    
    
    
      jstl
      jstl
      1.2
    
    
    
      javax.servlet
      javax.servlet-api
      4.0.1
    
  
  
    ssm
    
      
        
          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
        
      
    
  

3.web.xml 配置 Spring MVC、Spring

设置字符编码过滤器、加载静态资源。

 

  Archetype Created Web Application 
  
  
    contextConfigLocation
    classpath:spring.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  
  
  
    dispatcher
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:springmvc.xml
    
    
  
    dispatcher
    /
   
  
  
    character
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      utf-8
    
    
      forceRequestEncoding
      true
    
    
      forceResponseEncoding
      true
    
  
  
    character
    /*
  
  
  
    default
    *.js
  
  
    default
    *.png
  
  
    default
    *.css
  

4.分别在main目录下创建resource包

并创建spring,springmvc,mybatis.xml文件

Java开发之手把手教你搭建企业级工程SSM框架_第2张图片

5.在spring.xml中连接数据库

将user和password改成自己的数据库用户名和密码,并将3306/后面改成连接数据库的包名,紧接着配置SqlSessionFactory,扫描Mapper接口,事务管理等。



    
        
        
        
        
    
    
    
        
        
        
    
    
    
        
    
    
    
        
    
    
        
            
        
      
    
        
        
    
 

6.springmvc.xml中配置驱动和前后缀表达式


   
    
    
    
        
        
    

7.配置打印sql语句和指定实体类,让idea搜索需要的javaBean




    
        
        
    
    
        
        
    

8.创建与数据库相对应的实体类

Java开发之手把手教你搭建企业级工程SSM框架_第3张图片

9.Handler

package com.south.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.south.service.UserService;
@Controller
@RequestMapping("/user")
public class UserHandler {
    @Autowired
    private UserService userService;
    @GetMapping("/index")
    public ModelAndView index(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("index");
        modelAndView.addObject("list",userService.findAll());
        return modelAndView;
    }
}

10.Service及其接口

package com.south.service.impl;
import com.south.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.south.repository.UserRepository;
import com.south.service.UserService;
import java.util.List;
@Service
public class UserServiceimpl implements UserService {
    @Autowired
    private UserRepository userRepository;
    @Override
    public List findAll() {
        return userRepository.findAll();
    }
}
package com.south.service;
import com.south.entity.User; 
import java.util.List;
public interface UserService {
    public List findAll();
}

11.Repository




    

12.测试所用的jsp

<%@page contentType="text/html;charset=UTF-8" %>
<%@page isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>



    Title

    
        ${user.id}-${user.username}-${user.password}-${user.age}

最后放出根目录的图片,方便大家学习参考

Java开发之手把手教你搭建企业级工程SSM框架_第4张图片

以上就是Java教程手把手教你搭建企业级工程SSM框架的详细内容,更多关于Java教程搭建企业级SSM框架的资料请关注脚本之家其它相关文章!

你可能感兴趣的:(Java开发之手把手教你搭建企业级工程SSM框架)