1.新建Maven工程,需要注意的是:在Archetype上,选择Artifact Id为maven-archetype-webapp的那项。
2.第一次构建Maven项目,会下载一些JAR包,由于Maven中央仓库速度太慢,我们选择阿里的镜像,在%MAVEN_HOME%/conf/settings.xml里,添加如下信息:(可选配置Maven本地仓库的地址。同样是在settings.xml里添加
除非你想每次下载JAR包都很慢,否则还是配置吧...
3.在Build Path里添加Tomcat的JAR包支持。成功之后的工程项目结构如图所示:
4.此时,已可以在Tomcat v8.5 Server at localhost里添加Guaipangpang项目,输入http://localhost:8080/Guaipangpang。如下图所示:
5.此时可以开始添加Spring,SpringMVC,Mybatis,MySQL,Druid,Log4j等支持。pom.xml依赖如下所示:
pom.xml
junit
junit
3.8.1
test
org.apache.maven.plugins
maven-resources-plugin
2.4.3
org.mybatis
mybatis
3.2.1
org.mybatis
mybatis-spring
1.2.3
org.springframework
spring-context
4.1.3.RELEASE
org.springframework
spring-beans
4.1.3.RELEASE
org.springframework
spring-webmvc
4.1.3.RELEASE
org.springframework
spring-jdbc
4.1.3.RELEASE
org.springframework
spring-aspects
4.1.3.RELEASE
mysql
mysql-connector-java
5.0.8
com.alibaba
druid
1.0.16
javax.servlet
jstl
1.2
log4j
log4j
1.2.16
org.slf4j
slf4j-api
1.6.1
org.slf4j
slf4j-nop
1.6.4
6.修改src/main/java/webapp/WEB-INF/web.xml
web.xml
webMaven
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
contextConfigLocation
classpath:spring.xml,classpath:mybatis-spring.xml
org.springframework.web.context.ContextLoaderListener
springDispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
springDispatcherServlet
/
index.html
index.jsp
7.在src/main/resources下添加配置文件:
spring.xml
mybatis-spring.xml
spring-mvc.xml
mybatis-config.xml
log4j.properties
log4j.rootLogger=DEBUG, stdout
log4j.logger.org.mybatis=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n
mysqldb.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.0.106:3306/test
jdbc.username=root
jdbc.password=root
8.Java文件与JSP文件如下所示:
PersonController
package com.gpp.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.gpp.model.Person;
import com.gpp.service.IPersonService;
@Controller
@RequestMapping("/personController")
public class PersonController {
private IPersonService personService;
public IPersonService getPersonService() {
return personService;
}
@Autowired
public void setPersonService(IPersonService personService) {
this.personService = personService;
}
@RequestMapping("/showPerson")
public String showPersons(Model model){
List persons = personService.loadPersons();
model.addAttribute("persons", persons);
return "showperson";
}
}
PersonMapper
package com.gpp.dao;
import java.util.List;
import com.gpp.model.Person;
public interface PersonMapper {
/**
* 插入一条记录
* @param person
*/
void insert(Person person);
/**
* 查询所有
* @return
*/
List queryAll();
}
PersonMapper
Person.java
package com.gpp.model;
/**
* person 实体
* @author Administrator
*
*/
public class Person {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
}
IPersonService
package com.gpp.service;
import java.util.List;
import com.gpp.model.Person;
public interface IPersonService {
/**
* 加载全部的person
* @return
*/
List loadPersons();
}
PersonServiceImpl
package com.gpp.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gpp.dao.PersonMapper;
import com.gpp.model.Person;
import com.gpp.service.IPersonService;
@Service("personService")
public class PersonServiceImpl implements IPersonService {
private PersonMapper personMapper;
public PersonMapper getPersonMapper() {
return personMapper;
}
@Autowired
public void setPersonMapper(PersonMapper personMapper) {
this.personMapper = personMapper;
}
public List loadPersons() {
return personMapper.queryAll();
}
}
9.项目结构如图所示:
10.数据库表的设计与记录如图所示:
11.输入http://localhost:8080/Guaipangpang/personController/showPerson。
即可得到如下所示的结果:
12.至此,由Maven搭建SSM框架正式完成。