Maven实战之搭建SSM项目

前言

在之前进行框架整合开发的时候,就深深感觉到每次的jar包的管理太过于麻烦,占用空间也大,在学习了Maven之后,便开始深深的爱上了这个神器,那么我就带着大家一起来实战搭建一下吧。

Maven搭建SSM开发环境

1. 新建Maven Web 项目

这里就不在多赘述了,不会的请参考我的上一篇文章Eclipse中Maven的使用以及Pom.xml详解

你新建好的web项目应该和下面一样:

Maven实战之搭建SSM项目_第1张图片
web项目

2. 导入对应spring mvc、spring、mybatis的依赖包以及编写所需要的配置文件

(1)在项目的pom.xml文件中需要添加spring、springmvc、mybatis的依赖包:


  4.0.0
  com.mxf
  Maven_SSM
  0.0.1-SNAPSHOT
  war
  
    
    
        
            org.apache.maven.plugins
            maven-compiler-plugin
            3.0
            
                1.8
                1.8
            
        
        
        
            org.apache.tomcat.maven
            tomcat7-maven-plugin
            2.2
        
    
    
    
    
            
                src/main/java
                
                
                    **/*.properties
                    **/*.xml
                
            
            
            
                src/main/resources
                  
                    **/*.properties  
                    **/*.xml  
                
            
        
  
  
  
  
    4.2.4.RELEASE
    3.2.8
  
  
  
    
    
        jstl
        jstl
        1.2
    
    
    
        javax.servlet
        servlet-api
        2.5
        provided
    
    
    
    
        org.springframework
        spring-web
        ${spring.version}
    
    
        org.springframework
        spring-test
        ${spring.version}
    
    
        org.springframework
        spring-context
        ${spring.version}
    
    
        org.springframework
        spring-beans
        ${spring.version}
    
    
        org.springframework
        spring-jdbc
        ${spring.version}
    
    
        org.springframework
        spring-webmvc
        ${spring.version}
    
    
        org.springframework
        spring-aspects
        ${spring.version}
    
    
        org.springframework
        spring-tx
        ${spring.version}
    
    
        org.springframework
        spring-orm
        ${spring.version}
    
    
        org.springframework
        spring-core
        ${spring.version}
    
    
    
    
    
        org.mybatis
        mybatis-spring
        1.3.0
    
    
        org.mybatis
        mybatis
        ${mybatis.version}
    
    
    
    
    
    
        mysql
        mysql-connector-java
        5.1.40
    
    
    
    
        c3p0
        c3p0
        0.9.1.2
    
    
    
    
    
    
        ch.qos.logback
        logback-core
        1.1.7
    
    
        ch.qos.logback
        logback-classic
        1.1.7
    
    
        org.slf4j
        slf4j-api
        1.7.12
    
    
        org.slf4j
        slf4j-log4j12
        1.6.4
    
    
    
  

注意:因为我这里使用的c3p0数据库连接池和logback日志打印,所以也需要在pom文件中对应引入。

更新maven:项目右键->Maven->Update Project

(2)添加spring配置文件applicationContext.xml


     
    
    
    
    
    
    
        
        
        
        
        
        
        
        
        
        
        
        
    
    
    
     
    
        
        
        
    
    
    
        
        
    

(2)添加springmvc配置文件springmvc.xml



    
    
    
    
    
    
    
    
    
          
          
      

(3)配置数据库连接文件db.properties

jdbc.mysql.driver=com.mysql.jdbc.Driver
jdbc.mysql.url=jdbc:mysql://localhost:3306/first?useUnicode=true&characterEncoding=UTF-8
jdbc.mysql.username=root
jdbc.mysql.password=
jdbc.mysql.minPoolSize=2
jdbc.mysql.maxPoolSize=10
jdbc.mysql.maxIdleTime=1800000
jdbc.mysql.acquireIncrement=3
jdbc.mysql.maxStatements=1000
jdbc.mysql.initialPoolSize=3

(4)配置日志记录文件logback.xml

    
        
            
        
            
            
            
            
        ${APP_NAME}    
        
            
            
                
                ${ENCODER_PATTERN}    
                
            
        
       
            
            
                
                ${LOG_HOME}/output.%d{yyyy-MM-dd}.log  
                  
                30    
                
                
                ${ENCODER_PATTERN}    
                
            
        
            
            
                
                ${LOG_HOME}/error.%d{yyyy-MM-dd}.log    
                30    
                
                
                ${ENCODER_PATTERN}    
                
                
                WARN    
                
            
            
            
            
                
                ${LOG_HOME}/sync.%d{yyyy-MM-dd}.log    
                30    
                
                
                ${ENCODER_PATTERN}    
                
            
            
            
                
            
          
          
            
                
                
                
            
  

(5)配置项目目录下的web.xml文件:



  Maven_SSM
  
    contextConfigLocation
    classpath:applicationContext.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  
  
    logbackConfigLocation
    classpath:logback.xml
  
  
    CharacterEncodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      utf-8
    
  
  
    CharacterEncodingFilter
    /*
  
  
    springDispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:springmvc.xml
    
    1
  
  
    springDispatcherServlet
    /
  

3. 进行框架整合的测试

(1)按照上面配置文件中的配置对应创建相应的包:

  • com.mxf.controller:控制层
  • com.mxf.entity:实体类
  • com.mxf.mappers:映射文件
  • com.mxf.service:服务层
  • com.mxf.service.impl:服务层实现类
Maven实战之搭建SSM项目_第2张图片
创建所有的包

(2)编写对应的类

完成后看起来应该会是下面这样:

Maven实战之搭建SSM项目_第3张图片
编码

Teacher.java

package com.mxf.entity;

public class Teacher {
    
    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;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
}

ITeacherMapper.java

package com.mxf.mappers;

import java.util.List;

import com.mxf.entity.Teacher;

public interface ITeacherMapper {
    List getAllTeacherInfo();
}

TeacherMapper.xml




  
  

ITeacherServcie.java

package com.mxf.service;

import java.util.List;

import com.mxf.entity.Teacher;

public interface ITeacherServcie {
    List getAllTeacherInfo();
}

TeacherServiceImpl.java

package com.mxf.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mxf.entity.Teacher;
import com.mxf.mappers.ITeacherMapper;
import com.mxf.service.ITeacherServcie;

@Service("teacherService")
public class TeacherServiceImpl implements ITeacherServcie{

    @Autowired
    private ITeacherMapper teacherMapper;
    
    @Override
    public List getAllTeacherInfo() {
        return teacherMapper.getAllTeacherInfo();
    }

}

TeacherController.java

package com.mxf.controller;

import java.util.List;

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 com.mxf.entity.Teacher;
import com.mxf.service.ITeacherServcie;

@Controller
public class TeacherController {
    
    @Autowired
    private ITeacherServcie teacherServcie;
    
    @RequestMapping("/getAllTeacherInfo")
    public String getAllTeacherInfo(Model model){
        List list = teacherServcie.getAllTeacherInfo();
        model.addAttribute("list", list);
        return "teacher_info";
    }
    
}

WEB_INF/views/teacher_info.jsp

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




教师信息页面


    
        
id=${teacher.id } name=${teacher.name } password=${teacher.password }

(3)进行测试

右键项目->Run As->Maven build...->->

Maven实战之搭建SSM项目_第4张图片
运行项目(1)
Maven实战之搭建SSM项目_第5张图片
运行项目(2)

输出栏没有任何错误信息输出,如下所示:

Maven实战之搭建SSM项目_第6张图片
输出栏信息

在浏览器中访问http://localhost:8080/Maven_SSM/getAllTeacherInfo,对应输出页面:

Maven实战之搭建SSM项目_第7张图片
成功页面

需要完整代码的可以点此去下载(包括SQL源文件):
Maven搭建SSM完整代码

参考文章:
org.apache.jasper.JasperException: Unable to compile class for JSP

Maven项目mybatis Invalid bound statement (not found)解决方法

你可能感兴趣的:(Maven实战之搭建SSM项目)