Maven+SSM框架搭建【spring+springmvc+mybatis】

 

 本案例用到:ssm【spring+springmvc+mybatis】框架

 数据库:mysql (推荐使用mysql 或者 sqlserver  。oracle太大,一般大型项目才会用到)

 开发工具:idea  (myEclipse也行)

 项目管理工具:maven

 

 

(1)     Maven设置

Maven是一个项目管理工具,本项目中主要用来对jar包的管理

 

File – Settings -- Maven

 Maven+SSM框架搭建【spring+springmvc+mybatis】_第1张图片

 Maven+SSM框架搭建【spring+springmvc+mybatis】_第2张图片

Maven+SSM框架搭建【spring+springmvc+mybatis】_第3张图片

 

Maven中conf 中 settings.xml设置

 Maven+SSM框架搭建【spring+springmvc+mybatis】_第4张图片

Maven+SSM框架搭建【spring+springmvc+mybatis】_第5张图片

 

 

(2)     创建maven项目

File—New--Project

Maven+SSM框架搭建【spring+springmvc+mybatis】_第6张图片

 

 Maven+SSM框架搭建【spring+springmvc+mybatis】_第7张图片

 接着下一步,这里需要注在Properties中添加一个参数 archetypeCatalog=internal,不加这个参数,在maven生成骨架的时候将会非常慢,有时候直接卡住。

 Maven+SSM框架搭建【spring+springmvc+mybatis】_第8张图片

Maven+SSM框架搭建【spring+springmvc+mybatis】_第9张图片

 

创建maven项目结果

Maven+SSM框架搭建【spring+springmvc+mybatis】_第10张图片

 

 

为项目添加java包

File-- Project  Structure---Modules

 

将java 文件变成source文件夹,参考

http://www.jianshu.com/p/099eeca28c0f

 

(3)     pom.xml设置

 

 Maven+SSM框架搭建【spring+springmvc+mybatis】_第11张图片

 

 


  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  4.0.0
  com.bian.web
  web-bian
  war
  1.0-SNAPSHOT
  web-bian 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-bian
  


 

 

 

设置成果后,刷新maven,发现下载到所有jar包,如下图

 Maven+SSM框架搭建【spring+springmvc+mybatis】_第12张图片

 

 

(4)     spring配置文件设置:

 Maven+SSM框架搭建【spring+springmvc+mybatis】_第13张图片

 

4.1  jdbc.properties配置:

 

 Maven+SSM框架搭建【spring+springmvc+mybatis】_第14张图片

 

driverClasss=com.mysql.jdbc.Driver

jdbcUrl=jdbc:mysql://192.168.1.102:3306/netInfoPlatform?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull

username=root

password=123456



#定义初始连接数

initialSize=0

#定义最大连接数

maxActive=20

#定义最大空闲

maxIdle=20

#定义最小空闲

minIdle=1

#定义最长等待时间

maxWait=60000

 

 

4.2  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

 

 

 

4.3  spring-mvc配置:

 

 Maven+SSM框架搭建【spring+springmvc+mybatis】_第15张图片

 

 





       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

       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-4.0.xsd

                        http://www.springframework.org/schema/context

                        http://www.springframework.org/schema/context/spring-context-4.0.xsd

                        http://www.springframework.org/schema/mvc

                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">



    

    package="com.fzu.edu.controller"/>



    

    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

        

            

                text/html;charset=UTF-8

            

        

    

    

    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

        

            

                 

            

        

    





    

    class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        

        

    



    

    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

        

        

        

        

        

        

    

 

 

 

4.4  spring-mybatis配置:

Maven+SSM框架搭建【spring+springmvc+mybatis】_第16张图片

 

 Maven+SSM框架搭建【spring+springmvc+mybatis】_第17张图片





       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:context="http://www.springframework.org/schema/context" 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.1.xsd

                        http://www.springframework.org/schema/context

                        http://www.springframework.org/schema/context/spring-context-3.1.xsd

                        http://www.springframework.org/schema/tx

                        http://www.springframework.org/schema/tx/spring-tx.xsd">



    

    package="com.fzu.edu"/>



    

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

        

    





    



    

    class="org.apache.commons.dbcp.BasicDataSource"

          destroy-method="close">

        

        

        

        

        

        

        

        

        

        

        

        

        

        

    



    

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

        

        

        

    



    

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

        

        

    





    

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

        

    



    

    

 

 

 

 

(5)     MVC框架设置

5.1  M层:用于放类比如User类

 Maven+SSM框架搭建【spring+springmvc+mybatis】_第18张图片

 

 

package com.fzu.edu.model;



import java.io.Serializable;

import java.util.Date;



/**

 * Created by bianbian on 2017/10/25.

 */

public class User implements Serializable {





    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;

    }

}

 

 

 

 

5.2  Dao层:是链接数据库的接口层

 Maven+SSM框架搭建【spring+springmvc+mybatis】_第19张图片

 

 

package com.fzu.edu.dao;



import com.fzu.edu.model.User;

import org.springframework.stereotype.Repository;



import java.util.List;



/**

 * Created by bianbian on 2017/10/25.

 */

@Repository

public interface UserDao {



    List selectAllUser();

}
 

 

 

5.3  Service层:是控制层与Dao层之间的桥梁,主要用于逻辑的编写

 Maven+SSM框架搭建【spring+springmvc+mybatis】_第20张图片

Maven+SSM框架搭建【spring+springmvc+mybatis】_第21张图片

Maven+SSM框架搭建【spring+springmvc+mybatis】_第22张图片

 

 

5.4  控制层:是前段与后台的连接口

 

 Maven+SSM框架搭建【spring+springmvc+mybatis】_第23张图片

 

package com.fzu.edu.controller;



import com.fzu.edu.model.User;

import com.fzu.edu.service.UserService;

import com.fzu.edu.service.impl.UserServiceImpl;

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 bianbian on 2017/10/25.

 */

@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";

    }



}
 

 

 

(6)     jdk设置

 Maven+SSM框架搭建【spring+springmvc+mybatis】_第24张图片

 

 

(7)     tomcat设置

 Maven+SSM框架搭建【spring+springmvc+mybatis】_第25张图片

Maven+SSM框架搭建【spring+springmvc+mybatis】_第26张图片

Maven+SSM框架搭建【spring+springmvc+mybatis】_第27张图片

 

 Maven+SSM框架搭建【spring+springmvc+mybatis】_第28张图片

 

 

 

 

 

 

(8)     数据库设置:本案例以mysql为例

mysql搭建过程:自行百度

 Maven+SSM框架搭建【spring+springmvc+mybatis】_第29张图片

 

 

 

sql语言:

 

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', '卞', 18120861228, '[email protected]', '05126a423a9379d529e4ee61a212fa55', 'KJUYT5', '2017-10-25 23:38:56', '2017-10-25 23:39:09', '0');

INSERT INTO `t_user` VALUES ('2', '李', 18120861228, '[email protected]', '98bd3a1bebde01ad363d3c5a0d1e56da', '656JHU', '2017-10-25 23:39:01', '2017-10-25 23:39:13', '0');

INSERT INTO `t_user` VALUES ('3', '管', 18120861228, '[email protected]', '5470db9b63c354f6c8d628b80ae2f3c3', '89UIKQ', '2017-10-25 23:39:05', '2017-10-25 23:39:16', '0');
 

 

 

 

运行项目:恭喜你运行成功

Maven+SSM框架搭建【spring+springmvc+mybatis】_第30张图片

 

转载于:https://www.cnblogs.com/1qazse4/p/7732541.html

你可能感兴趣的:(Maven+SSM框架搭建【spring+springmvc+mybatis】)