本文使用IDEA,利用Maven管理项目,整合SSM框架、步骤详细,讲解丰富,适合新手入门。
IntelliJ IDEA Ultimate 2019.1
Apache-tomcat-9.0.5
JDK 1.8.0_121
MySQL 5.7
Maven 3.3.9
1. 新建Maven项目:
点击File -> New -> Project -> Maven -> 勾选 Create from archetype -> 选择 maven-archetype-webapp (注意:此处不要错选成上面的cocoom-22-archetype-webapp)
这里没什么设置的,一直点next就可以了
经过上面的步骤,已经将ssm的骨架搭建出来了,如下图所示:
修改pom.xm文件内容,引入jar包。
4.0.0
com
first
war
1.0-SNAPSHOT
first Maven Webapp
http://maven.apache.org
UTF-8
UTF-8
5.0.3.RELEASE
3.4.4
junit
junit
3.8.1
test
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-test
${spring.version}
javax.servlet
javax.servlet-api
3.0.1
provided
javax.servlet.jsp
jsp-api
2.2
provided
javax.servlet
jstl
1.2
taglibs
standard
1.1.2
com.fasterxml.jackson.core
jackson-databind
2.9.4
mysql
mysql-connector-java
5.1.38
com.mchange
c3p0
0.9.5.2
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.3.1
ch.qos.logback
logback-classic
1.1.1
first
org.apache.maven.plugins
maven-compiler-plugin
1.8
在src\main下新建目录java,并将java目录标记为Source Root 如下图:
此时可以看到java目录已经变成蓝色了。现在就需要在java下建立包com.myssm.paul
完成之后如下图所示:
在com.myssm.paul包下分别建立如下四个包controller、pojo、dao、service。
pojo: 存放自定义的java类。如:employee类等,每个类的属性设为private,并提供public属性的getter/setter方法让外界访问
service:定义接口,包含系统所提供的功能。
dao:定义接口,包含与数据库进行交互的功能。
controller:控制器,负责接收页面请求,转发和处理。
完成之后,如下所示:
1、在controller中新建文件EmployeeController,文件内容如下:
package com.myssm.paul.controller;
import com.myssm.paul.pojo.Employee;
import com.myssm.paul.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
import java.util.Map;
/**
* @author Puhao
* @date 2019/5/8 0008
* @brief
*/
@Controller
public class EmployeeController {
@Autowired
EmployeeService employeeService;
@RequestMapping("/getAll")
public String getList(Map map) {
System.out.println("-------------");
List empls = employeeService.selectAll();
map.put("empls", empls);
return "employee/list";
}
}
2、在dao中新建文件EmployeeMapper.java,文件内容如下:
package com.myssm.paul.dao;
import com.myssm.paul.pojo.Employee;
import java.util.List;
import org.springframework.stereotype.Repository;
@Repository
public interface EmployeeMapper {
List selectAll();
}
3、在pojo中新建文件Employee.java,文件内容如下:
package com.myssm.paul.pojo;
public class Employee {
private Integer id;
private String lastName;
private String email;
private String addr;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
", addr='" + addr + '\'' +
'}';
}
}
4、在service中新建文件EmployeeService.java,文件内容如下:
package com.myssm.paul.service;
import com.myssm.paul.dao.EmployeeMapper;
import com.myssm.paul.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author Puhao
* @date 2019/5/9 0009
* @brief
*/
@Service
public class EmployeeService {
@Autowired
private EmployeeMapper employeeMapper;
public List selectAll(){
return employeeMapper.selectAll();
}
}
在src/main 下创建resource文件,并将其标记为Resource Root。如下图所示:
1、创建mapper映射文件夹mapper:并在该文件下,创建EmployeeMapper.xml文件,内容如下:
2、创建spring文件夹,并在该文件夹下创建两个文件spring-MVC.xml与applicationContext.xml
①.spring-MVC.xml文件如下:
②.applicationContext.xml文件如下:
3、创建mybatis.xml配置文件:
4、创建dbconf.properties,数据库配置文件。
#### 这里需要根据自己的数据库进行修改
#驱动,都一样
jdbc.driver=com.mysql.jdbc.Driver
#数据库连接地址
jdbc.url=jdbc:mysql://localhost:3306/mybatis
#密码
jdbc.password=root
#用户名
jdbc.username=root
数据库创建:
/*
Navicat MySQL Data Transfer
Source Server : localhost_3306
Source Server Version : 50717
Source Host : localhost:3306
Source Database : mybatis
Target Server Type : MYSQL
Target Server Version : 50717
File Encoding : 65001
Date: 2019-05-10 13:13:18
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for empl
-- ----------------------------
DROP TABLE IF EXISTS `empl`;
CREATE TABLE `empl` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`last_name` varchar(20) DEFAULT NULL,
`email` varchar(20) DEFAULT NULL,
`addr` varchar(20) DEFAULT NULL,
`dept_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_emp_dept` (`dept_id`),
CONSTRAINT `fk_emp_dept` FOREIGN KEY (`dept_id`) REFERENCES `dept` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=143 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of empl
-- ----------------------------
INSERT INTO `empl` VALUES ('1', 'Lucy', '[email protected]', 'NNNN', '1');
INSERT INTO `empl` VALUES ('3', 'Tom', '[email protected]', 'Nanjing', '2');
INSERT INTO `empl` VALUES ('4', 'Tom', '[email protected]', 'Nanjing', '1');
INSERT INTO `empl` VALUES ('5', 'admin', '[email protected]', 'Nanjing', '2');
INSERT INTO `empl` VALUES ('6', 'bbbb', '[email protected]', 'Beijing', '1');
INSERT INTO `empl` VALUES ('7', 'Tom', '[email protected]', 'Nanjing', '2');
INSERT INTO `empl` VALUES ('8', 'PPPP', '[email protected]', 'Beijing', '1');
INSERT INTO `empl` VALUES ('9', 'Tom', '[email protected]', 'Nanjing', '2');
INSERT INTO `empl` VALUES ('136', 'Simth', '[email protected]', 'Chongqing', null);
INSERT INTO `empl` VALUES ('137', 'David', '[email protected]', 'Chengdu', null);
INSERT INTO `empl` VALUES ('138', 'Kitty', '[email protected]', 'Guangzhou', null);
INSERT INTO `empl` VALUES ('139', 'Simth', '[email protected]', 'Chongqing', null);
INSERT INTO `empl` VALUES ('140', 'David', '[email protected]', 'Chengdu', null);
INSERT INTO `empl` VALUES ('141', 'Kitty', '[email protected]', 'Guangzhou', null);
INSERT INTO `empl` VALUES ('142', 'sql', '[email protected]', 'Suzhou', null);
在WEB-INF 下创建employee文件夹,并在该文件下创建list.jsp文件 如下:
list.jsp文件内容如下:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Author: Puhao
Date: 2019/5/8 0008
Brief:
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
id
lastName
email
addr
${a.id}
${a.lastName}
${a.email}
${a.addr}
修改index.jsp文件的内容:
Hello World!
select all employee
web.xml文件内容:
contextConfigLocation
classpath:spring/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
springServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/springMVC.xml
1
springServlet
/
CharacterEncoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
CharacterEncoding
/*
1、配置tomcat
2、配置
3、
4、配置根目录,红色框框中填入/,表示根目录
5、启动运行
点击运行
首次运行之后会自动跳转到界面:
效果: