SSM框架-------项目环境搭建(小Demo运行)

框架整合步骤

创建User表(使用SQLyog)

创建Maven项目(Webapp)添加依赖

配置Web.xml

数据库的配置文件db.properties

Mybatis 配置文件

spring-dao层配置文件

spring-service配置文件

spring-web配置文件

创建User实体

创建UserDao

创建 UserService

创建UserController

简单的JSP

Dao层测试


具体步骤

创建User表(使用SQLyog)

SSM框架-------项目环境搭建(小Demo运行)_第1张图片

CREATE TABLE `user` (
  `id` int(12) NOT NULL AUTO_INCREMENT,
  `username` varchar(32) DEFAULT NULL,
  `birthday` date DEFAULT NULL,
  `gender` varchar(2) DEFAULT NULL,
  `address` varchar(128) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8

创建Maven项目(Webapp)添加依赖




  4.0.0

  com.springmvc
  day03-springmvc
  1.0-SNAPSHOT
  war

  day03-springmvc Maven Webapp
  
  http://www.example.com

  
    org.springframework
    5.0.1.RELEASE
    3.5.2
    1.3.2
    
    1.7.7
    1.2.17
  
  
    
      junit
      junit
      4.12
      test
    

    
    
      
      ${spring.groupId}
      spring-core
      ${spring.version}
    
    
    
    
      ${spring.groupId}
      spring-beans
      ${spring.version}
    
    
    
      ${spring.groupId}
      spring-web
      ${spring.version}
    
    
      ${spring.groupId}
      spring-oxm
      ${spring.version}
    
    
    
      ${spring.groupId}
      spring-tx
      ${spring.version}
    
    
    
      ${spring.groupId}
      spring-jdbc
      ${spring.version}
    
    
    
      ${spring.groupId}
      spring-webmvc
      ${spring.version}
    
    
      ${spring.groupId}
      spring-aop
      ${spring.version}
    
    
    
      ${spring.groupId}
      spring-context
      ${spring.version}
    
    
    
      ${spring.groupId}
      spring-test
      ${spring.version}
    
    

    
    
      org.mybatis
      mybatis
      ${mybatis.version}
    
    
      org.mybatis
      mybatis-spring
      ${mybatis.spring.version}
    

    
    
      mysql
      mysql-connector-java
      5.1.37
    
    
    
      c3p0
      c3p0
      0.9.1.2
    
    
    
      commons-dbcp
      commons-dbcp
      1.2.2
    
    
    
      com.alibaba
      druid
      1.1.12
    

    
    
      redis.clients
      jedis
      3.0.1
    

    
    
      jstl
      jstl
      1.2
    

    
    
      log4j
      log4j
      ${log4j.version}
    

    
      ch.qos.logback
      logback-classic
      1.2.3
    

    
      org.codehaus.jackson
      jackson-mapper-asl
      1.9.13
    

    
    
    
      commons-fileupload
      commons-fileupload
      1.3.3
    

    
    
      commons-beanutils
      commons-beanutils
      1.9.3
    


    
      commons-io
      commons-io
      2.4
    
    
      commons-codec
      commons-codec
      1.9
    

    
      com.fasterxml.jackson.core
      jackson-databind
      2.9.5
      
    

    
    
      com.fasterxml.jackson.core
      jackson-core
      2.9.5
    
    
      com.fasterxml.jackson.core
      jackson-annotations
      2.9.5
    
    
      commons-collections
      commons-collections
      3.2
    

    
    
      javax.servlet
      javax.servlet-api
      3.1.0
      provided
    

    
    
      javax.servlet.jsp
      javax.servlet.jsp-api
      2.3.1
      provided
    

    
      dom4j
      dom4j
      1.1
    

    
      jaxen
      jaxen
      1.1.6
    

    
      org.aspectj
      aspectjrt
      1.8.5
    
    
      org.aspectj
      aspectjweaver
      1.8.5
    
    
    
    
      net.coobird
      thumbnailator
      0.4.8
    
    
    
      com.github.penggle
      kaptcha
      2.3.2
    

  

  
    day03-springmvc
    
      
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-war-plugin
          3.2.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  

 在创建好Maven项目我们要在右边 刷新一下,让Maven管理文件夹,才可以创建SSM框架-------项目环境搭建(小Demo运行)_第2张图片

配置Web.xml



  Archetype Created Web Application

  
    spring-dispatcher
    org.springframework.web.servlet.DispatcherServlet

    
      contextConfigLocation
      classpath:spring/spring-*.xml
    
    
    1
  

  
    spring-dispatcher
    
    /
  

数据库的配置文件db.properties

# 连接数据库的参数
db.driver=com.mysql.jdbc.Driver
db.username=root
db.password=123456
db.url=jdbc:mysql://localhost:3306/spring

Mybatis 配置文件




    
    
        
        

        
        

        
        
    

这里我使用了三层架构这样的配置,在web.xml 中,就加载了三层配置文件

spring-dao层配置文件




    
    
    

    
    
        
        
        
        

        
        
        
    

    
    
        
        
        
        
     

    
    
        
        
        
    

spring-service配置文件




    
    

    
    
        
        
    

    
    

spring-web配置文件



    
    
    
    

    
    

    
    
        
        
    

创建User实体

package com.kangna.domain;

import java.util.Date;

/********************************
 * @Author: kangna
 * @Date: 2019/10/10 18:30
 * @Version: 1.0
 * @Desc: User entity
 ********************************/
public class User {
    private Integer id;
    private String username;
    private Date birthday;
    private String gender;
    private String address;

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Date getBirthday() {
        return birthday;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAddress() {
        return address;
    }

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

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

创建UserDao

package com.kangna.dao;

import com.kangna.domain.User;

import java.util.List;

public interface UserDao {

    List findAll();
}

创建 UserService

package com.kangna.service;

import com.kangna.dao.UserDao;
import com.kangna.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/********************************
 * @Author: kangna
 * @Date: 2019/10/10 19:39
 * @Version: 1.0
 * @Desc:
 ********************************/
@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    public List findAll() {
        return userDao.findAll();
    }
}

创建UserController

package com.kangna.web.controller;

import com.kangna.domain.User;
import com.kangna.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/********************************
 * @Author: kangna
 * @Date: 2019/10/10 19:19
 * @Version: 1.0
 * @Desc:
 ********************************/
@Controller
public class UserController {

    @Autowired
    private UserService userService;

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

简单的JSP

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--导入jstl标签库--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


    用户列表jsp页面


用户列表:

        
用户Id 用户名称 用户地址
${user.id} ${user.username} ${user.address}

Dao层测试

import com.kangna.domain.User;
import com.kangna.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

/********************************
 * @Author: kangna
 * @Date: 2019/10/11 21:30
 * @Version: 1.0
 * @Desc:
 ********************************/
@RunWith(SpringJUnit4ClassRunner.class)
// 告诉junit Spring 配置文件的位置
@ContextConfiguration({ "classpath:spring/spring-dao.xml" , "classpath:spring/spring-service.xml"})
public class UserDaoTest {


    @Autowired
    private UserService userService;

    @Test
    public void testFindAll() {
        System.out.print(userService.findAll());
    }
}

启动Tomcat进行  Controller的测试 

SSM框架-------项目环境搭建(小Demo运行)_第3张图片

至此我们就搭建了一个简单的SSM框架,并且测试可以运行。 

你可能感兴趣的:(SSM框架整合,SSM框架环境配置,依赖添加,POM,JavaWeb)