idea搭建ssm框架(超详细)

一。创建项目

先附上测试的数据库

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `name` varchar(10) COLLATE utf8_bin DEFAULT NULL,
  `password` varchar(10) COLLATE utf8_bin DEFAULT NULL,
  `remark` varchar(50) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'zhangsan', '123456', null);
INSERT INTO `user` VALUES ('2', 'lisi', '123', null);
INSERT INTO `user` VALUES ('3', 'wangan', '456', null);
INSERT INTO `user` VALUES ('4', 'xinxi', '000', null);

1.new->project出现如下

idea搭建ssm框架(超详细)_第1张图片

点击next后出现如下填写GroupId和ArtifactId在点击next直至finish

idea搭建ssm框架(超详细)_第2张图片

2.构建目录结构

在main下新建java和resources目录如下并将java目录标记为Sources Root,resources标记为Resources Root

idea搭建ssm框架(超详细)_第3张图片

 

idea搭建ssm框架(超详细)_第4张图片

在java下新建如下包package

idea搭建ssm框架(超详细)_第5张图片

二。Spring MVC

1.首先引入Spring MVC依赖


    
      org.springframework
      spring-core
      ${spring.version}
    
    
      org.springframework
      spring-aop
      ${spring.version}
    
    
      org.springframework
      spring-web
      ${spring.version}
    
    
      org.springframework
      spring-webmvc
      ${spring.version}
    
    
      org.springframework
      spring-jdbc
      ${spring.version}
    
    
      org.springframework
      spring-tx
      ${spring.version}
    

并添加4.3.8.RELEASE

2.在controller新建TestController类,在WEB-INF下新建static目录,还在WEB-INF下新建jsp目录并新建test.jsp页面

idea搭建ssm框架(超详细)_第6张图片

 

3.在web.xml添加dispatcherServlet

"1.0" encoding="UTF-8"?>
"http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">

  SSM2
  

  

  
  
    SpringEncodingFilter
    class>org.springframework.web.filter.CharacterEncodingFilterclass>
    
      encoding
      UTF-8
    
    
      forceEncoding
      true
    
  
  
    SpringEncodingFilter
    /*
  



  
  
    dispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      
      /WEB-INF/spring-mvc.xml
    
    1
  
  
  
    dispatcherServlet
    /
  


  
    index.jsp
  

4.在WEB-INF下新建spring-mvc.xml

"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    
    base-package="com.exp.controller" use-default-filters="false">
        "annotation" expression="org.springframework.stereotype.Controller"/>

    

    

    
    
    "contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        
        "favorPathExtension" value="true"/>
        
        "favorParameter" value="true"/>
        "parameterName" value="format"/>
        
        "ignoreAcceptHeader" value="false"/>

        "mediaTypes"> 
            
                json=application/json
                xml=application/xml
                html=text/html
            
        
        
        "defaultContentType" value="text/html"/>
    


    
    default-servlet-handler />
    
    "/static/**" location="/WEB-INF/static/"/>


    
    "viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>


5.部署

点击如下图

idea搭建ssm框架(超详细)_第7张图片

点击+号找到Local

idea搭建ssm框架(超详细)_第8张图片

接下来修改名字,选择Deployment点击+号,选择ssm2:war exploded,另外Application context填写的如果是ssm2,那么访问的时候就是localhost:8080/ssm2,部署结束。

idea搭建ssm框架(超详细)_第9张图片

6.启动测试

启动后默认访问的是index.jsp页面,可以访问http://localhost:8080/ssm2/test

idea搭建ssm框架(超详细)_第10张图片

如果页面乱码在头部加上<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>

三。Spring与Mybatis

1.引入相关依赖


      
          org.mybatis
          mybatis
          3.3.0
      
      
      
          mysql
          mysql-connector-java
          5.1.26
      
      
      
          com.alibaba
          druid
          1.0.20
      
      
      
          com.alibaba
          fastjson
          1.2.31
      

      
      
          org.mybatis
          mybatis-spring
          1.2.3
      

      
      
          javax.servlet
          jstl
          1.2
      

      
      
          log4j
          log4j
          1.2.17
      
      
          org.slf4j
          slf4j-api
          1.7.21
      
2.生成实体与mybatis配置文件
在resources下新建generator目录并新建generatorConfig.xml文件

DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">


    
    <classPathEntry
            location="D:\OfficeWave\configdata\repository\mysql\mysql-connector-java\5.1.26\mysql-connector-java-5.1.26.jar"/>
    
        
        
        
        
        
                        connectionURL="jdbc:mysql://localhost:3306/user"
                        userId="root"
                        password="root">
        
        
        
            
            
        
        
        
            
        

        
        
                             targetProject="src/main/java">
            

            
        
        

idea搭建ssm框架(超详细)_第11张图片

在pom.xml中的标签下添加如下


          
              org.mybatis.generator
              mybatis-generator-maven-plugin
              1.3.2
              
                  ${basedir}/src/main/resources/generator/generatorConfig.xml
                  true
                  true
              
          
      

双击运行即可

idea搭建ssm框架(超详细)_第12张图片

3.web.xml中引入spring相关配置

   
           class>org.springframework.web.context.ContextLoaderListenerclass>
      
   
  contextConfigLocation
  classpath:applicationContext.xml

4.在resources下新建applicationContext.xml



       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
">
    
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        
    

    
    package="com.exp">
        
    

    <import resource="classpath:spring-mybatis.xml" />

5.在resources下新建jdbc.properties文件和spring-mybatis.xml

jdbc.properties

jdbc_driverClassName =com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf8
jdbc_username=root
jdbc_password=root

spring-mybatis.xml



       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    
    class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        
        
        
        
        

        
        
        
        

        
        
        
        

        
        

        
        
        
        

        
        
        

        
        
    

    
    class="org.mybatis.spring.SqlSessionFactoryBean">
        
        
        
    


    
    class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        
    

    
    
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
    

    
    
    
    


6.编写代码

在service下新建接口UserService

package com.exp.service;

import com.exp.model.User;

import java.util.List;

public interface UserService {

    List selectAll();
}
 在UserServiceImpl下新建类UserServiceImpl
 
     
package com.exp.service.impl;

import com.exp.mapper.UserMapper;
import com.exp.model.User;
import com.exp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserServiceImpl implements UserService {

@Autowired
private UserMapper userMapper;
@Override
public List selectAll() {
List userList = userMapper.selectAll();
return userList;
}
}
 
类TestController
package com.exp.controller;

import com.exp.model.User;
import com.exp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/")
public class TestController {

    @Autowired
    private UserService userService;

    @RequestMapping("/test")
    public String test(){
        List userList = userService.selectAll();
        for (User user:userList){
            System.out.println(user);
}
return "test"; } }

7.测试

访问http://localhost:8080/ssm2/test

在server打印如下即搭建成功

idea搭建ssm框架(超详细)_第13张图片

 



转载于:https://www.cnblogs.com/xinyijiu/p/10920993.html

你可能感兴趣的:(idea搭建ssm框架(超详细))