IDEA搭建SSM三大框架详细步骤教程,纯干货(亲测)

一、环境准备

1.IntelliJ IDEA 2017.3.3 (Ultimate Edition)

2.JDK1.8

3.Apache-Maven-3.1.1

4.Mysql-5.6.26

5.Apache-Tomcat-7.0.65

二、开始搭建

1.项目结构

IDEA搭建SSM三大框架详细步骤教程,纯干货(亲测)_第1张图片

2.pom.xml




  4.0.0

  com.tydic
  SSMDemo01
  1.0-SNAPSHOT
  war
  
    
    
      org.springframework
      spring-beans
      4.3.2.RELEASE
    
    
      org.springframework
      spring-core
      4.3.2.RELEASE
    
    
      org.springframework
      spring-context
      4.3.2.RELEASE
    
    
      org.springframework
      spring-expression
      4.3.2.RELEASE
    
    
      org.apache.commons
      com.springsource.org.apache.commons.logging
      1.1.1
    
    
    
      org.springframework
      spring-aop
      4.3.2.RELEASE
    
    
    
      org.aspectj
      com.springsource.org.aspectj.weaver
      1.6.6.RELEASE
    
    
    
      org.springframework
      spring-aspects
      4.3.2.RELEASE
    
    
      org.aopalliance
      com.springsource.org.aopalliance
      1.0.0
    
    
    
      mysql
      mysql-connector-java
      5.1.6
    
    
    
    
      com.mchange
      c3p0
      0.9.5.2
    
    
    
      org.apache.commons
      com.springsource.org.apache.commons.dbcp
      1.2.2.osgi
    
    
      org.apache.commons
      com.springsource.org.apache.commons.pool
      1.5.3
    
    
    
      org.springframework
      spring-jdbc
      4.2.6.RELEASE
    
    
    
      org.springframework
      spring-tx
      4.2.6.RELEASE
    
    
    
    
      org.springframework
      spring-web
      4.3.2.RELEASE
    
    
      org.springframework
      spring-webmvc
      4.3.2.RELEASE
    
    
      javax.servlet
      servlet-api
      2.5
    
    
    
      javax.servlet
      jstl
      1.2
    
    
      com.fasterxml.jackson.core
      jackson-core
      2.8.8
    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.8.8
    
    
      com.fasterxml.jackson.core
      jackson-annotations
      2.8.8
    
    
    
      org.mybatis
      mybatis
      3.4.6
    
    
      org.bgee.log4jdbc-log4j2
      log4jdbc-log4j2-jdbc4.1
      1.16
    
    
      org.slf4j
      slf4j-api
      1.7.13
    
    
      org.slf4j
      slf4j-log4j12
      1.7.5
    
    
      log4j
      log4j
      1.2.16
    
    
      junit
      junit
      RELEASE
    
    
      org.mybatis
      mybatis-spring
      1.3.0
    
  
  
    
      
        src/main/java
        
          **/*.xml
        
      
    
  

3.SqlMapConfig.xml(mybatis的配置文件交给Spring管理了,所以在此就配置了下面的东西)




    
    
        
        
        
        
        
    
    
    
        
    

4.Spring的配置文件application.xml



    
    
    
    
    
    
        
        
        
        
        
        
        
        
        
    
    
    
        
        
        
        
    
    
    
        
        
        
    
    
    
        
        
    
    
    

5.SpringMvc的配置文件



    
    
    
    
    
    
    
        
        
        
        
    

6.db.properties

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
db.username=root
db.password=123456
#最大连接数
c3p0.maxPoolSize=30
#最小连接数
c3p0.minPoolSize=10
#关闭连接后不自动commit
c3p0.autoCommitOnClose=false
#获取连接超时时间
c3p0.checkoutTimeout=10000
#当获取连接失败重试次数
c3p0.acquireRetryAttempts=2

7.web.xml



  
  
    index.jsp
  

  
  
    org.springframework.web.context.ContextLoaderListener
  
  
  
    contextConfigLocation
    classpath:application.xml
  
  
  
    springMvc
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:SpringMvc.xml
    
  
  
    springMvc
    /rest/*
  

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

8.controller(UserController)

package com.tydic.controller;

import com.tydic.pojo.User;
import com.tydic.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

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

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

    @Resource
    private UserService userService;

    @RequestMapping("queryAll")
    public  String queryAll(Model model){
        List list = userService.queryAll();
        model.addAttribute("list",list);
        return "userList";
    }
}

9.service(UserService和UserServiceImpl)

9.1 UserService

package com.tydic.service;

import com.tydic.pojo.User;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface UserService {
    //1.根据id查询用户
    public User findUserById(int id);
    //2.根据用户名字模糊查询用户信息
    public List findUserByName(String name);
    //3.插入用户
    public boolean insertUser(User user);
    //4.修改用户,通过注解传多个参数,通过这个注解就可以把参数注入到userMapper.xml中了
    public int updateUser(@Param("uname") String name, @Param("id") int id);
    //5.查询所有
    public List queryAll();
}

9.2 UserServiceImpl

package com.tydic.service;

import com.tydic.mapper.UserMapper;
import com.tydic.pojo.User;
import org.springframework.stereotype.Service;

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

@Service
public class UserServiceImpl implements UserService {

    //注入UserMapper
    @Resource
    private UserMapper userMapper;

    public User findUserById(int id) {
        return userMapper.findUserById(id);
    }

    public List findUserByName(String name) {
        return userMapper.findUserByName(name);
    }

    public boolean insertUser(User user) {
        return userMapper.insertUser(user);
    }

    public int updateUser(String name, int id) {
        return userMapper.updateUser(name,id);
    }

    public List queryAll() {
        return userMapper.queryAll();
    }
}

10.mapper(UserMapper和UserMapper.xml)

10.1 UserMapper

package com.tydic.mapper;

import com.tydic.pojo.User;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface UserMapper {
    //1.根据id查询用户
    public User findUserById(int id);
    //2.根据用户名字模糊查询用户信息
    public List findUserByName(String name);
    //3.插入用户
    public boolean insertUser(User user);
    //4.修改用户,通过注解传多个参数,通过这个注解就可以把参数注入到userMapper.xml中了
    public int updateUser(@Param("uname") String name, @Param("id") int id);
    //5.查询所有
    public List queryAll();
}

10.2 UserMapper.xml





    
    

11.pojo(User)

package com.tydic.pojo;

import java.util.Date;

public class User {
    private Integer id;
    private String uname;
    private char sex;
    private String address;
    private Date birthday;

    public Integer getId() {
        return id;
    }

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

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", uname='" + uname + '\'' +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                ", birthday=" + birthday +
                '}';
    }
}

11.jsp(创建在WEB-INF下面的jsp文件夹中的userList.jsp)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>




    

    My JSP 'userList.jsp' starting page

    
    
    
    
    
    




id uname birthday sex address 操作
${li.id } ${li.uname } ${li.birthday} ${li.sex} ${li.address }

12.mysql数据库测试数据准备

 12.1建表语句

CREATE TABLE `t_user` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `uname` varchar(20) DEFAULT NULL,
  `sex` char(20) DEFAULT NULL,
  `address` varchar(20) DEFAULT NULL,
  `birthday` date DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

12.2插入数据

INSERT INTO `t_user` (`id`, `uname`, `sex`, `address`, `birthday`) VALUES (1, '张三', '1', '河南', '2019-2-8');
INSERT INTO `t_user` (`id`, `uname`, `sex`, `address`, `birthday`) VALUES (2, '小红', '1', '上海', '2019-2-8');
INSERT INTO `t_user` (`id`, `uname`, `sex`, `address`, `birthday`) VALUES (3, '修改1', '2', '北京', '2019-2-8');
INSERT INTO `t_user` (`id`, `uname`, `sex`, `address`, `birthday`) VALUES (4, '小明', '2', '上海', '2019-2-8');

13.启动项目测试

结果(完美输出):

IDEA搭建SSM三大框架详细步骤教程,纯干货(亲测)_第2张图片

14.idea与tomcat整合下篇详细讲;

Always keep the faith!!!

你可能感兴趣的:(ssm)