在之前进行框架整合开发的时候,就深深感觉到每次的jar包的管理太过于麻烦,占用空间也大,在学习了Maven之后,便开始深深的爱上了这个神器,那么我就带着大家一起来实战搭建一下吧。
这里就不在多赘述了,不会的请参考我的上一篇文章Eclipse中Maven的使用以及Pom.xml详解
你新建好的web项目应该和下面一样:
(1)在项目的pom.xml文件中需要添加spring、springmvc、mybatis的依赖包:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.mxfgroupId>
<artifactId>Maven_SSMartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>warpackaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.0version>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.2version>
plugin>
plugins>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
resource>
resources>
build>
<properties>
<spring.version>4.2.4.RELEASEspring.version>
<mybatis.version>3.2.8mybatis.version>
properties>
<dependencies>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-ormartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.0version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>${mybatis.version}version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.40version>
dependency>
<dependency>
<groupId>c3p0groupId>
<artifactId>c3p0artifactId>
<version>0.9.1.2version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-coreartifactId>
<version>1.1.7version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.1.7version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.12version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.6.4version>
dependency>
dependencies>
project>
注意:因为我这里使用的c3p0数据库连接池和logback日志打印,所以也需要在pom文件中对应引入。
更新maven:项目右键->Maven->Update Project
(2)添加spring配置文件applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.mxf.service">context:component-scan>
<context:property-placeholder location="classpath:db.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.mysql.driver}" />
<property name="jdbcUrl" value="${jdbc.mysql.url}" />
<property name="user" value="${jdbc.mysql.username}" />
<property name="password" value="${jdbc.mysql.password}" />
<property name="maxPoolSize" value="${jdbc.mysql.maxPoolSize}" />
<property name="minPoolSize" value="${jdbc.mysql.minPoolSize}" />
<property name="maxIdleTime" value="${jdbc.mysql.maxIdleTime}" />
<property name="acquireIncrement" value="${jdbc.mysql.acquireIncrement}" />
<property name="maxStatements" value="${jdbc.mysql.maxStatements}" />
<property name="initialPoolSize" value="${jdbc.mysql.initialPoolSize}" />
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.mxf.entity" />
<property name="mapperLocations" value="classpath:com/mxf/mappers/*.xml">property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mxf.mappers" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
bean>
beans>
(3)添加springmvc配置文件springmvc.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.mxf.controller"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven />
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
bean>
beans>
(4)配置数据库连接文件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
(5)配置日志记录文件logback.xml
<configuration info="true">
<property name="APP_NAME" value="moxiaofei" />
<property name="LOG_HOME" value="F:\\MavenLog4j" />
<property name="ENCODER_PATTERN"
value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level 类名:%logger{80} 方法名:%M 行号:%L- %msg%n" />
<contextName>${APP_NAME}contextName>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>${ENCODER_PATTERN}Pattern>
encoder>
appender>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/output.%d{yyyy-MM-dd}.logfileNamePattern>
<maxHistory>30maxHistory>
rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${ENCODER_PATTERN}pattern>
encoder>
appender>
<appender name="ERROR_FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/error.%d{yyyy-MM-dd}.logfileNamePattern>
<maxHistory>30maxHistory>
rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${ENCODER_PATTERN}pattern>
encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARNlevel>
filter>
appender>
<appender name="SYNC_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/sync.%d{yyyy-MM-dd}.logfileNamePattern>
<maxHistory>30maxHistory>
rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${ENCODER_PATTERN}pattern>
encoder>
appender>
<logger name="log.sync" level="INFO" addtivity="true">
<appender-ref ref="SYNC_FILE" />
logger>
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
<appender-ref ref="ERROR_FILE" />
root>
configuration>
(6)配置项目目录下的web.xml文件:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Maven_SSMdisplay-name>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<context-param>
<param-name>logbackConfigLocationparam-name>
<param-value>classpath:logback.xmlparam-value>
context-param>
<filter>
<filter-name>CharacterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>CharacterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<servlet>
<servlet-name>springDispatcherServletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springDispatcherServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
web-app>
(1)按照上面配置文件中的配置对应创建相应的包:
- com.mxf.controller:控制层
- com.mxf.entity:实体类
- com.mxf.mappers:映射文件
- com.mxf.service:服务层
- com.mxf.service.impl:服务层实现类
(2)编写对应的类
完成后看起来应该会是下面这样:
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
<mapper namespace="com.mxf.mappers.ITeacherMapper" >
<select id="getAllTeacherInfo" resultType="com.mxf.entity.Teacher" >
select id,name,password from teacher
select>
mapper>
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" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>教师信息页面title>
head>
<body>
<c:forEach items="${list }" var="teacher">
<table>
<tr>
<td>id=${teacher.id }td>
<td>name=${teacher.name }td>
<td>password=${teacher.password }td>
tr>
table>
c:forEach>
body>
html>
(3)进行测试
右键项目->Run As->Maven build…->->
输出栏没有任何错误信息输出,如下所示:
在浏览器中访问http://localhost:8080/Maven_SSM/getAllTeacherInfo,对应输出页面:
需要完整代码的可以点此去下载(包括SQL源文件):
Maven搭建SSM完整代码
参考文章:
org.apache.jasper.JasperException: Unable to compile class for JSP
Maven项目mybatis Invalid bound statement (not found)解决方法