IDEA2017 SSM框架整合搭建 Maven + Spring + SpringMVC + MyBatis

寄语:本文采用IDEA开发,建议按着文章顺序先搭建项目,只要按着目录结构先把目录建好就是复制粘贴的问题。内里代码也有注释,先搭建好,在慢慢看着代码思考,想想问什么这么做?即使你是初学者,按着一步一步也是可以搭建整合的。


步骤目录
1.创建Maven项目
1.1-1.6 看图操作
2.配置文件
2.1pom.xml
2.2jdbc.properties
2.3log4j.properties
2.4spring-mvc.xml
2.5spring-mybatis.xml
2.6web.xml
3.创建demo
3.1Controller
3.2 Dao
3.3 model
3.3.1数据库文件
3.4 Service
3.4.1 ServiceImpl
4.项目部署
4.1 识别目录
4.3 创建一个Tomca
4.4 配置访问根路径
4.5 浏览器访问

1. 创建maven项目

1.1 File -> New Module,进入创建项目窗口。

IDEA2017 SSM框架整合搭建 Maven + Spring + SpringMVC + MyBatis_第1张图片

1.2 点击Next,填写GroupId、ArtifactId和Version

IDEA2017 SSM框架整合搭建 Maven + Spring + SpringMVC + MyBatis_第2张图片

1.3 接着下一步,这里需要注在Properties中添加一个参数archetypeCatalog=internal

IDEA2017 SSM框架整合搭建 Maven + Spring + SpringMVC + MyBatis_第3张图片

1.4 填写Module name

创建完成


IDEA2017 SSM框架整合搭建 Maven + Spring + SpringMVC + MyBatis_第4张图片

生成项目骨架:Maven在自动下载所需要的jar包,以下代码段可以选择跳过,为控制台输出Maven的log

C:\Develop\jdk1.8.0_25\bin\java -Dmaven.multiModuleProjectDirectory=C:\Users\NINTH\AppData\Local\Temp\archetypetmp -Dmaven.home=C:\Develop\IDEA_2017.2.5\plugins\maven\lib\maven3 -Dclassworlds.conf=C:\Develop\IDEA_2017.2.5\plugins\maven\lib\maven3\bin\m2.conf -Dfile.encoding=UTF-8 -classpath C:\Develop\IDEA_2017.2.5\plugins\maven\lib\maven3\boot\plexus-classworlds-2.5.2.jar org.codehaus.classworlds.Launcher -Didea.version=2017.2.5 -DinteractiveMode=false -DgroupId=com.ninth.web -DartifactId=web-ssm -Dversion=1.0-SNAPSHOT -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=RELEASE org.apache.maven.plugins:maven-archetype-plugin:RELEASE:generate
[INFO] Scanning for projects...
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml (825 B at 0.2 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/3.0.1/maven-archetype-plugin-3.0.1.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/3.0.1/maven-archetype-plugin-3.0.1.pom (10 KB at 21.2 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetype/maven-archetype/3.0.1/maven-archetype-3.0.1.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/archetype/maven-archetype/3.0.1/maven-archetype-3.0.1.pom (13 KB at 20.8 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/30/maven-parent-30.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/30/maven-parent-30.pom (41 KB at 52.7 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/apache/18/apache-18.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/apache/18/apache-18.pom (16 KB at 30.6 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/3.0.1/maven-archetype-plugin-3.0.1.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/3.0.1/maven-archetype-plugin-3.0.1.jar (94 KB at 82.6 KB/sec)
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetype/archetype-catalog/3.0.1/archetype-catalog-3.0.1.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/archetype/archetype-catalog/3.0.1/archetype-catalog-3.0.1.pom (2 KB at 4.6 KB/sec)
....................中间省略一大部分.........................
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:RELEASE
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: C:\Users\NINTH\AppData\Local\Temp\archetypetmp
[INFO] Parameter: package, Value: com.ninth.web
[INFO] Parameter: groupId, Value: com.ninth.web
[INFO] Parameter: artifactId, Value: web-ssm
[INFO] Parameter: packageName, Value: com.ninth.web
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\Users\NINTH\AppData\Local\Temp\archetypetmp\web-ssm
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:29 min
[INFO] Finished at: 2017-10-27T16:41:21+08:00
[INFO] Final Memory: 15M/287M
[INFO] ------------------------------------------------------------------------
[INFO] Maven execution finished

1.6 Maven骨架搭建之后,个人建议可以先按着以下目录图把项目所有需要的文件准备好,继续看下去就是把代码复制到各个文件,非常简单方便。

IDEA2017 SSM框架整合搭建 Maven + Spring + SpringMVC + MyBatis_第5张图片
项目完整目录

2.项目配置

2.1项目配置文件 pom.xml


  4.0.0
  com.heitian.web
  web-ssm
  war
  1.0-SNAPSHOT
  web-ssm Maven Webapp
  http://maven.apache.org
  
    UTF-8
    UTF-8

    
    4.2.5.RELEASE

    
    3.2.8

    
    5.1.29

    
    1.7.18
    1.2.17

  


  
    
    
      jstl
      jstl
      1.2
    

    
      javax
      javaee-api
      7.0
    

    
    
      junit
      junit
      4.11
      
      test
    

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

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

    
    
      org.mybatis
      mybatis
      ${mybatis.version}
    

    
    
      org.mybatis
      mybatis-spring
      1.2.2
    

    
    
      mysql
      mysql-connector-java
      ${mysql-driver.version}
    
    
    
      commons-dbcp
      commons-dbcp
      1.2.2
    

    
    
      com.alibaba
      fastjson
      1.1.41
    

    
    
      log4j
      log4j
      ${log4j.version}
    
    
      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-core
      2.8.0
    
    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.8.0
    

    
      commons-fileupload
      commons-fileupload
      1.3.1
    

    
      commons-io
      commons-io
      2.4
    

    
      commons-codec
      commons-codec
      1.9
    
  

  
    web-ssm
  

2.2 配置JDBC文件 jdbc.properties

driverClasss=com.mysql.jdbc.Driver  
jdbcUrl=jdbc:mysql://localhost:3306/db_ssm?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull  
username=root  
password=root  
  
#定义初始连接数  
initialSize=0  
#定义最大连接数  
maxActive=20  
#定义最大空闲  
maxIdle=20  
#定义最小空闲  
minIdle=1  
#定义最长等待时间  
maxWait=60000  

2.3 log4j 配置文件 log4j.properties

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=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%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][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n  

2.4配置 springmvc文件 spring-mvc.xml




    
    

    
    
        
            
                text/html;charset=UTF-8
            
        
    
    
    
        
            
                 
            
        
    


    
    
        
        
    

    
    
        
        
        
        
        
        
    

2.5 配置spring和mybatis整合所需的配置文件 spring-mybatis.xml




    
    

    
    
        
    


    

    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
    

    
    
        
        
    


    
    
        
    

    
    

2.6web.xml配置文件 web.xml




    web-ssm
    
    
        contextConfigLocation
        classpath:spring-mybatis.xml
    

    
        log4jConfigLocation
        classpath:log4j.properties
    

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

    
    
        org.springframework.web.context.ContextLoaderListener
    

    
    
        org.springframework.web.util.IntrospectorCleanupListener
    


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

    
        /index.jsp
    

    
    
        15
    


3.做一个查询小demo

3.1Controller文件 UserController

package com.ninth.ssm.controller;

import com.ninth.ssm.model.User;
import com.ninth.ssm.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Ninth on 2017-10-28 15:29.
 * 
**/


@Controller
@RequestMapping("/user")
public class UserController {

    private Logger log = Logger.getLogger(UserController.class);
    @Resource
    private UserService userService;

    @RequestMapping("/showUser")
    public String showUser(HttpServletRequest request, Model model){
        log.info("查询所有用户信息");
        List userList = userService.getAllUser();
        model.addAttribute("userList",userList);
        return "showUser";
    }
}

3.2 dao文件 UserDao

package com.ninth.ssm.dao;

import com.ninth.ssm.model.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by Ninth on 2017-10-28 15:29.
 * 
**/


@Repository
public interface UserDao {

    User selectUserById(@Param("userId") Long userId);

    User selectUserByPhoneOrEmail(@Param("emailOrPhone") String emailOrPhone, @Param("state") Short state);

    List selectAllUser();
}

3.3 model文件 User

package com.ninth.ssm.model;

import java.util.Date;

/**
 * Created by Ninth on 2017-10-28 15:29.
 *
**/

public class User {

    private Long id;
    private String userName;
    private String userPhone;
    private String userEmail;
    private String userPwd;
    private String pwdSalt;
    private Date createTime;
    private Date modifyTime;
    private Short isDelete;

    public Long getId() {
        return id;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPhone() {
        return userPhone;
    }

    public void setUserPhone(String userPhone) {
        this.userPhone = userPhone;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public String getUserPwd() {
        return userPwd;
    }

    public void setUserPwd(String userPwd) {
        this.userPwd = userPwd;
    }

    public String getPwdSalt() {
        return pwdSalt;
    }

    public void setPwdSalt(String pwdSalt) {
        this.pwdSalt = pwdSalt;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getModifyTime() {
        return modifyTime;
    }

    public void setModifyTime(Date modifyTime) {
        this.modifyTime = modifyTime;
    }

    public Short getIsDelete() {
        return isDelete;
    }

    public void setIsDelete(Short isDelete) {
        this.isDelete = isDelete;
    }
}

3.3.1数据库文件

在sql创建数据库db_ssm,然后运行目录下的sql文件,自动生成表,当然,你也可以手动建表。

IDEA2017 SSM框架整合搭建 Maven + Spring + SpringMVC + MyBatis_第6张图片
SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `t_user`
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
  `id` bigint(11) NOT NULL auto_increment,
  `user_name` varchar(255) default NULL COMMENT '用户名',
  `user_phone` varchar(20) default NULL COMMENT '手机号',
  `user_email` varchar(255) default NULL COMMENT '邮箱地址',
  `user_pwd` varchar(32) default NULL COMMENT '加盐后用户密码',
  `pwd_salt` varchar(6) default NULL COMMENT 'MD5盐',
  `create_time` datetime default NULL COMMENT '创建时间',
  `modify_time` datetime default NULL COMMENT '最后修改时间',
  `is_delete` tinyint(4) default NULL COMMENT '是否删除,0-未删除;1-已删除',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='用户登录表';

-- ----------------------------
-- Records of t_user
-- ----------------------------
INSERT INTO `t_user` VALUES ('1', '赵大宝', '13285250574', '[email protected]', '05126a423a9379d529e4ee61a212fa55', 'KJUYT5', '2016-07-15 23:38:56', '2016-07-15 23:39:09', '0');
INSERT INTO `t_user` VALUES ('2', '张三丰', '15985250574', '[email protected]', '98bd3a1bebde01ad363d3c5a0d1e56da', '656JHU', '2016-07-15 23:39:01', '2016-07-15 23:39:13', '0');
INSERT INTO `t_user` VALUES ('3', '王尼玛', '13685250574', '[email protected]', '5470db9b63c354f6c8d628b80ae2f3c3', '89UIKQ', '2016-07-15 23:39:05', '2016-07-15 23:39:16', '0');

3.4 Service文件 UserService

package com.ninth.ssm.service;

import com.ninth.ssm.model.User;
import java.util.List;

/**
 * Created by Ninth on 2017-10-28 15:30.
 * 
**/

public interface UserService {

    List getAllUser();

    User getUserByPhoneOrEmail(String emailOrPhone, Short state);

    User getUserById(Long userId);
}

3.4.1 Service实现类 IUserServiceimpl

package com.ninth.ssm.service.impl;

import com.ninth.ssm.dao.UserDao;
import com.ninth.ssm.model.User;
import com.ninth.ssm.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Ninth on 2017-10-28 15:30.
 * 
**/


@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    public User getUserById(Long userId) {
        return userDao.selectUserById(userId);
    }

    public User getUserByPhoneOrEmail(String emailOrPhone, Short state) {
        return userDao.selectUserByPhoneOrEmail(emailOrPhone,state);
    }

    public List getAllUser() {
        return userDao.selectAllUser();
    }
}

3.5 Mapper文件 UserMapper.xml





    
        
        
        
        
        
        
        
        
        
    

    

    

    


4.项目部署

4.1 识别目录作用,选择File-Project Structure

IDEA2017 SSM框架整合搭建 Maven + Spring + SpringMVC + MyBatis_第7张图片

4.3 创建一个Tomcat容器实例,并把项目部署进去

IDEA2017 SSM框架整合搭建 Maven + Spring + SpringMVC + MyBatis_第8张图片

4.4 项目所需配置好项目访问的根路径,然后启动Tomcat。

IDEA2017 SSM框架整合搭建 Maven + Spring + SpringMVC + MyBatis_第9张图片

4.5 浏览器访问验证 浏览器输入http://localhost:8080/maver-ssm/user/showUser,出现下图,恭喜你

IDEA2017 SSM框架整合搭建 Maven + Spring + SpringMVC + MyBatis_第10张图片

你可能感兴趣的:(IDEA2017 SSM框架整合搭建 Maven + Spring + SpringMVC + MyBatis)