idea:初学者搭建ssm框架demo,仔细填坑

从eclipse换到idea,SSM的项目跟maven真的不好配置,下面讲解踩坑步骤(感谢A+大佬帮助以及论坛大佬们的demo)

项目结构:

idea:初学者搭建ssm框架demo,仔细填坑_第1张图片

一、设置Maven:

Maven的官网 点击打开链接

下载解压后,到 maven-XXX /conf/ settings.xml 里配置下,;

1,找到localRepository,设置你的jar包储存目录,可自定义

比如我:  

F:\Java\apache-maven-3.3.9\repo

2,配置云端链接,下载jar包的地址,为了防止有墙或者单个云端挂了,

我们设置三个地址保险一点

   
      alimaven  
      aliyun maven  
      http://maven.aliyun.com/nexus/content/groups/public/  
      central  
    

    
      ui
      central
      Human Readable Name for this Mirror.
      http://uk.maven.org/maven2/
    

    
      sprintio
      central
      Human Readable Name for this Mirror.
      https://repo.spring.io/libs-snapshot/
    
二、idea创建项目:

1、next->new window open 在新的窗口打开,

这里暂时不用脚手架 ,我的包名是:com.spring 

idea:初学者搭建ssm框架demo,仔细填坑_第2张图片


填写你的包名跟项目名后,开始搭建框架

ssmwork,ssmdemo都行,

新建后,这里有java,resources文件,pom.xml,这些都是大致文件

idea:初学者搭建ssm框架demo,仔细填坑_第3张图片


2、main.java

package com.spring.controller;


import com.spring.pojo.Admin;
import com.spring.service.AdminService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class main {

    @Resource
    AdminService adminService;
    @RequestMapping("/")
    @ResponseBody
    public String index(){
        return "hello world";
    }

    @RequestMapping(value = "/admin",method = RequestMethod.POST)
    @ResponseBody
    public int insertUser(Admin admin){
       return adminService.insertAdmin(admin);

    }
    @RequestMapping(value = "/admin/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Admin getUser(@PathVariable  Integer id){
        return adminService.getAdmin(id);

    }
}

3、AdminMapper.java (接口)

package com.spring.dao;

import com.spring.pojo.Admin;

public interface AdminMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(Admin record);

    int insertSelective(Admin record);

    Admin selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(Admin record);

    int updateByPrimaryKey(Admin record);
}

4、Admin.java(实体类)

package com.spring.pojo;

public class Admin {
    private Integer id;

    private String name;

    private String password;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }
}

5、AdminService.java(业务逻辑处理层)

package com.spring.service;

import com.spring.pojo.Admin;
import com.spring.dao.AdminMapper;
import com.spring.pojo.Admin;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("AdminService")

public class AdminService {
    @Resource
    protected AdminMapper adminMapper;


    public Admin getAdmin(int id){
        Admin a=this.adminMapper.selectByPrimaryKey(id);
        if(a!=null){
            return a;
        }
        return null;
    }

    public int insertAdmin(Admin admin){
        return this.adminMapper.insert(admin);
    }


}

6、AdminMapper.xml(映射)




  
    
    
    
  
  
    id, name, password
  
  
  
    delete from admin
    where id = #{id,jdbcType=INTEGER}
  
  
    insert into admin (id, name, password
      )
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}
      )
  
  
    insert into admin
    
      
        id,
      
      
        name,
      
      
        password,
      
    
    
      
        #{id,jdbcType=INTEGER},
      
      
        #{name,jdbcType=VARCHAR},
      
      
        #{password,jdbcType=VARCHAR},
      
    
  
  
    update admin
    
      
        name = #{name,jdbcType=VARCHAR},
      
      
        password = #{password,jdbcType=VARCHAR},
      
    
    where id = #{id,jdbcType=INTEGER}
  
  
    update admin
    set name = #{name,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  
三、resources设置

1、jdbc.properties (数据库设置连接层)

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/project?useUnicode=true&characterEncoding=utf-8
username=root
password=123456
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
2、log4j.properties(日志文件记录)
#定义LOG输出级别
log4j.rootLogger=INFO,Console,File
#定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活地指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n

#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定输出目录
log4j.appender.File.File = logs/ssm.log
#定义文件最大大小
log4j.appender.File.MaxFileSize = 10MB
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

3、log4j.xml




    
    
    
    
        
            
        
    

    
    
        
        
    

    
        
        
    
    
        
        
    

    
        
        
    

    
        
        
    

    
        
        
    

    
    
        
        
    

    
    
        
        
    
    
    
    
    
        
        
    

4、mybatis-config.xml




    
    
        
        
    

5、spring-mvc.xml



    
    

    
    
        
            
                text/html;charset=UTF-8
            
        
    
    
    
        
            
                   
            
        
    
    
    
        
        
        
    

    
    
        
        
        
        
        
        
    

6、spring-mybatis.xml



    
    
    
    
        
    

    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
        
    

    
    
        
        
    

    
    
        
    

四、其他资源:

1、webapp下的web.xml

注意你的项目名




    SSMDemoTwo
    
        index.html
        index.jsp
    
    
    
        contextConfigLocation
        classpath:spring-mybatis.xml
    

    
    
        log4jRefreshInterval
        60000
    
    
        org.springframework.web.util.Log4jConfigListener
    


    
    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        

    

    
        encodingFilter
        /*
    

    
    
        org.springframework.web.context.ContextLoaderListener
    
    
    
        org.springframework.web.util.IntrospectorCleanupListener
    


    
    
        mvc-dispatcher
        org.springframework.web.servlet.DispatcherServlet
        
        
        
            contextConfigLocation
            classpath:spring-mvc.xml
        
        1
        true
    


    
    
        mvc-dispatcher
        
        /
    
    

    
    
        15
    

2、pom.xml 注意你的包名

温馨提示:mysql 5.1.46这个jar包可以兼容5.6,5.7的版本

build 里面的 port , 可以自定义端口名,这里是8081,项目名是 ssmdemotwo



    4.0.0

    com.ssm.DemoTwo
    DemoTwo
    1.0-SNAPSHOT

    hugo Maven Webapp
    http://maven.apache.org

    
        UTF-8
        
        4.1.4.RELEASE
        
        3.2.8
        
        1.7.7
        1.2.17
        
        2.5.0
    

    

        
            junit
            junit
            3.8.1
            test
        
        
        
            org.mybatis
            mybatis
            ${mybatis.version}
        
        
        
            org.mybatis
            mybatis-spring
            1.2.2
        

        
        
            org.springframework
            spring-test
            4.1.7.RELEASE
            test
        

        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        

        
        
            org.springframework
            spring-core
            ${spring.version}
        

        
            org.springframework
            spring-beans
            ${spring.version}
        

        
            org.springframework
            spring-context
            ${spring.version}
        

        
            org.springframework
            spring-context-support
            ${spring.version}
        

        
            org.springframework
            spring-tx
            ${spring.version}
        

        
            org.springframework
            spring-web
            ${spring.version}
        

        
            org.springframework
            spring-jdbc
            ${spring.version}
        

        
            javax.servlet
            javax.servlet-api
            3.0.1
            provided
        

        
            javax.servlet.jsp
            jsp-api
            2.2
            provided
        

        
        
            mysql
            mysql-connector-java
            5.1.46
        

        
        
            commons-dbcp
            commons-dbcp
            1.2.2
        

        
        
            jstl
            jstl
            1.2
        
        
        
        
            log4j
            log4j
            ${log4j.version}
        


        
        
            com.alibaba
            fastjson
            1.1.41
        


        
            org.slf4j
            slf4j-api
            ${slf4j.version}
        

        
            org.slf4j
            slf4j-log4j12
            ${slf4j.version}
        
        
        
        
            org.codehaus.jackson
            jackson-mapper-asl
            1.9.13
        

        
            com.fasterxml.jackson.core
            jackson-databind
            2.1.0
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            2.1.0
        

        
        
            commons-fileupload
            commons-fileupload
            1.3.1
        
        
            commons-io
            commons-io
            2.4
        
        
            commons-codec
            commons-codec
            1.9
        

      

        
        
            org.springframework
            spring-test
            4.1.7.RELEASE
        
        
            junit
            junit
            RELEASE
        

        
            org.jsoup
            jsoup
            1.8.1
        

        
            net.sourceforge.htmlunit
            htmlunit
            2.15
        


        
            org.seleniumhq.selenium
            selenium-java
            2.33.0
        

        
            us.codecraft
            webmagic-core
            0.6.1
        
        
            us.codecraft
            webmagic-extension
            0.6.1
        


    


    
        
            
                org.apache.tomcat.maven
                tomcat7-maven-plugin
                2.2
                
                    
                    8081
                    
                    /ssmDemeTwo
                    
                    true
                
            
        
        hugo
        
            
                src/main/java
                
                    **/*.xml
                
            
            
                src/main/resources
                
                    *.*
                
            
        
    


五、idea的配置:

1、maven配置

idea:初学者搭建ssm框架demo,仔细填坑_第4张图片

2、为了防止idea抽风说jdk识别有误

idea:初学者搭建ssm框架demo,仔细填坑_第5张图片

3、为项目添加web包

idea:初学者搭建ssm框架demo,仔细填坑_第6张图片


maven的好处:自动下载jar包,不必手动寻找添加进入lib,

pom.xml 配置即可!

idea:初学者搭建ssm框架demo,仔细填坑_第7张图片


4、添加spring识别(如果已经识别,则不用手动了)

idea:初学者搭建ssm框架demo,仔细填坑_第8张图片

5、配置跑起项目

idea:初学者搭建ssm框架demo,仔细填坑_第9张图片


运行后,打开网址输入:localhost:8081/ssmDemoTwo即可看到hello word

idea:初学者搭建ssm框架demo,仔细填坑_第10张图片














你可能感兴趣的:(SSM框架)