1.使用idea搭建ssm框架步骤,请参考本人博客
2.项目搭建好,创建以下目录
3.在pom.xml文件中引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<groupId>com.kude.ssm</groupId>
<artifactId>SsmTest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>SsmTest Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!--spring-mvc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!--事务管理依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!--使用aop方式管理和控制事务-->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!--mybatis通用mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.3.9</version>
</dependency>
<!--spring-mybatis[把mybatis的mapper接口生成代理实现类交给spring容器统一管理]-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--数据库和数据库连接池-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--json-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
<!--web-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<!--c标签的依赖-->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<!--log4j打印相关日志信息-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<finalName>SsmTest</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
4.在Resources目录下引入spring配置文件applicationContext.xml,springMVC配置文件springMVC.xml,以及mapper文件mapper.xml
5.创建分页类Page 实体类跟数据库中的表字段与数据库中的一一对应
package com.kude.entity;
public class Employee {
private Integer id;
private String name;
private String password;
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
public Employee(Integer id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
public Employee(){
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
package com.kude.entity;
import java.util.List;
public class Page<E> {
private int currentPage;//当前页
private int totalPage;//总页数
private int pageSize;//每页记录数
private int totalCount;//总的记录数
private List<Employee> list;
public Page(){
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public List<Employee> getList() {
return list;
}
public void setList(List<Employee> list) {
this.list = list;
}
public Page(int currentPage, int totalPage, int pageSize, int totalCount, List<Employee> list) {
this.currentPage = currentPage;
this.totalPage = totalPage;
this.pageSize = pageSize;
this.totalCount = totalCount;
this.list = list;
}
@Override
public String toString() {
return "Page{" +
"currentPage=" + currentPage +
", totalPage=" + totalPage +
", pageSize=" + pageSize +
", totalCount=" + totalCount +
", list=" + list +
'}';
}
}
6.Dao层开发
package com.kude.dao;
import com.kude.entity.Employee;
import org.apache.ibatis.annotations.Select;
import java.util.HashMap;
import java.util.List;
public interface EmployeeDao {
// 插入
public void insert(Employee employee);
//删除
public void delete(Integer id);
//显示所有
public List<Employee> queryAll();
// 查询单条数据
public Employee query(String name);
// 修改
public void update(Employee employee);
// 查询单个
public Employee queryone(Integer id);
@Select("<script> select * from employee <if test='start!=null and size!=null'> limit #{start},#{size} </if> </script>")
List<Employee> findByPage(HashMap<String,Object> map);
@Select("select count(*) from employee")
//查询总记录数
int findAllCount();
}
7.mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kude.dao.EmployeeDao">
<select id="queryAll" resultType="employee">
select * from employee
</select>
<insert id="insert" parameterType="employee">
insert into employee values (#{id},#{name},#{password})
</insert>
<delete id="delete" parameterType="java.lang.Integer">
delete from employee where id=#{id}
</delete>
<select id="query" resultType="employee">
select * from employee where name=#{name}
</select>
<select id="queryone" resultType="employee" parameterType="java.lang.Integer">
select * from employee where id=#{id}
</select>
<update id="update" parameterType="employee">
update employee set
name=#{name},password=#{password}
where id=#{id}
</update>
</mapper>
8.Service层开发
package com.kude.service;
import com.kude.entity.Employee;
import com.kude.entity.Page;
import java.util.List;
public interface EmployeeService {
//显示所有
List<Employee> showall();
// 注册
void register (Employee employee);
//删除
void delete(Integer id);
//登录
Employee login(String name);
// 修改
void update(Employee employee);
Employee query(Integer id);
//查询总记录数
int findAllCount();
Page<Employee> findPage(int currentPage);
}
package com.kude.service;
import com.kude.dao.EmployeeDao;
import com.kude.entity.Employee;
import com.kude.entity.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;
@Service
@Transactional
public class EmployeeServiceimpl implements EmployeeService {
@Autowired
private EmployeeDao employeeDao;
public EmployeeDao getEmployeeDao() {
return employeeDao;
}
public void setEmployeeDao(EmployeeDao employeeDao) {
this.employeeDao = employeeDao;
}
/***
* 显示所有人员信息
* @return
*/
@Override
public List<Employee> showall() {
List<Employee> list=employeeDao.queryAll();
return list;
}
@Override
public void register(Employee employee) {
employeeDao.insert(employee);
}
@Override
public void delete(Integer id) {
employeeDao.delete(id);
}
@Override
public Employee login(String name) {
Employee employee=employeeDao.query(name);
return employee;
}
@Override
public void update(Employee employee) {
employeeDao.update(employee);
}
@Override
public Employee query(Integer id) {
Employee employee= employeeDao.queryone(id);
return employee;
}
@Override
public int findAllCount() {
return employeeDao.findAllCount();
}
@Override
public Page<Employee> findPage(int currentPage) {
HashMap<String,Object> map=new HashMap<>();
Page<Employee> page=new Page<>();
//封装当前的页数
page.setCurrentPage(currentPage);
//每页显示的数据
int pageSize=5;
page.setPageSize(pageSize);
//封装总记录数
int totalCount=employeeDao.findAllCount();
page.setTotalCount(totalCount);
//封装总页数
double totalPage=totalCount;
//向上取整
Double number=Math.ceil(totalPage/pageSize);
page.setTotalPage(number.intValue());
map.put("start",(currentPage-1)*pageSize);
map.put("size",page.getPageSize());
//封装每页显示的数据
List<Employee> list=employeeDao.findByPage(map);
page.setList(list);
return page;
}
}
9.controller开发
package com.kude.action;
import com.kude.entity.Employee;
import com.kude.service.EmployeeService;
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 org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Controller
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
EmployeeService employeeService;
public EmployeeService getEmployeeService() {
return employeeService;
}
public void setEmployeeService(EmployeeService employeeService) {
this.employeeService = employeeService;
}
@RequestMapping("/register")
public String register(Employee employee, HttpServletRequest request) {
String name = request.getParameter("name");
String password = request.getParameter("password");
System.out.println(name);
System.out.println(password);
if (name == null && password == null) {
return "register";
} else {
employeeService.register(employee);
return "login";
}
}
@RequestMapping("/login")
public String login(Employee employee, HttpServletRequest request) {
Employee employee1 = employeeService.login(employee.getName());
request.getSession().setAttribute("employee1", employee1);
if (employee != null) {
if (employee1.getPassword().equals(employee.getPassword())) {
return "forward:/employee/showAll.do";
} else {
return "login";
}
} else {
return "login";
}
}
@RequestMapping("/showAll")
public String showAll(@RequestParam(value = "currentPage",defaultValue = "1",required = false)int currentPage, Model model){
model.addAttribute("pagemsg",employeeService.findPage(currentPage));
return "showAllList";
}
@RequestMapping("/delete")
public String delete(Integer id){
employeeService.delete(id);
return "forward:/employee/showAll.do";
}
@RequestMapping("/select")
public String select(HttpServletRequest request){
String id= request.getParameter("id");
int id2=Integer.parseInt(id);
Employee employee= employeeService.query(id2);
request.setAttribute("employee",employee);
return "updateList";
}
@RequestMapping("/update")
public String update(Employee employee){
employeeService.update(employee);
return "forward:/employee/showAll.do";
}
}
10.applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注解扫描 -->
<context:component-scan base-package="com.kude.service"></context:component-scan>
<!-- 初始化配置,spring对于mybatis的连接池的封装 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url"
value="jdbc:mysql://localhost:3306/stu?characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- Spring 对于MyBatis-config中的实体类别名以及mapper映射文件的注册相关的封装 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 为实体类起别名并配置相关参数 -->
<property name="typeAliasesPackage">
<value>com.kude.entity</value>
</property>
<!-- 需要连接池相关 -->
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="mapperLocations">
<list>
<!-- * 代表通配符 -->
<value>classpath:mapper.xml</value>
</list>
</property>
</bean>
<!-- Spring对于MyBatis中的slqsession工具类做了进一步的封装 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 通过接口名,首字单词首字母小写作为beanid 获得的即是实现类对象 -->
<property name="basePackage">
<value>com.kude.dao</value>
</property>
</bean>
<!-- 编写事务代码 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- 组装切点加额外功能 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
11.springMVC.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 注解扫描器 -->
<context:component-scan base-package="com.kude.action"></context:component-scan>
<!-- springmvc的注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置视图解析器 -->
<!-- 为你控制器中返回的字符串拼接前缀和后缀 -->
<bean id="interal"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
12.简单的前端页面
regiser.jsp
<%--
Created by IntelliJ IDEA.
User: 石光辉
Date: 2019/5/26
Time: 10:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册页面</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/employee/register.do" method="post">
用户名:<br>
<input type="text" name="name" ><br>
密码:<br>
<input type="password" name="password">
<P>
<input type="submit" value="注册">
</P>
</form>
</body>
</html>
login.jsp
<%--
Created by IntelliJ IDEA.
User: 石光辉
Date: 2019/5/26
Time: 10:51
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/employee/login.do">
用户名:<br>
<input type="text" name="name"><br>
密码: <br>
<input type="password" name="password">
<p>
<input type="submit" value="登录" >
</p>
</form>
</body>
</html>
showAllList.jsp
<%--
Created by IntelliJ IDEA.
User: 石光辉
Date: 2019/5/26
Time: 11:02
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>展示所有</title>
</head>
<h2>尊敬的用户:${sessionScope.employee1.name}</h2>
<body>
<table>
<tr>
<td>ID</td>
<td>用户名 </td>
<td>密码</td>
</tr>
<tr>
<c:forEach var="employee" items="${requestScope.pagemsg.list}">
<tr>
<td>${employee.id}</td>
<td>${employee.name}</td>
<td>${employee.password}</td>
<td>
<a href="${pageContext.request.contextPath}/employee/delete.do?id=${employee.id}">删除</a>
<a href="${pageContext.request.contextPath}/employee/select.do?id=${employee.id}">修改</a>
</td>
</tr>
</c:forEach>
</table>
<table border="0" cellspacing="0" cellpadding="0" width="900px">
<tr>
<td>
<span>第${requestScope.pagemsg.currentPage}/${requestScope.pagemsg.totalPage}页</span>
<span>总记录数:${requestScope.pagemsg.totalCount} 每页显示:${requestScope.pagemsg.pageSize}</span>
<span>
<c:if test="${requestScope.pagemsg.currentPage!=1}">
<a href="${pageContext.request.contextPath}/employee/showAll.do?currentPage=1">[首页]</a>
<a href="${pageContext.request.contextPath }/employee/showAll.do?currentPage=${requestScope.pagemsg.currentPage-1}">[上一页]</a>
</c:if>
<c:if test="${requestScope.pagemsg.currentPage != requestScope.pagemsg.totalPage}">
<a href="${pageContext.request.contextPath}/employee/showAll.do?currentPage=${requestScope.pagemsg.currentPage+1}">[下一页]</a>
<a href="${pageContext.request.contextPath }/employee/showAll.do?currentPage=${requestScope.pagemsg.totalPage}">[尾页]</a>
</c:if>
</span>
</td>
</tr>
</table>
</body>
</html>
updateList.jsp
<%--
Created by IntelliJ IDEA.
User: 石光辉
Date: 2019/5/26
Time: 16:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改页面</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/employee/update.do" method="post">
<table>
id:<input name="id" value="${requestScope.employee.id}" readonly="readonly"><br>
name:<input name="name" value="${requestScope.employee.name}"><br>
password:<input name="password" value="${requestScope.employee.password}"><br>
<input type="submit" value="修改">
</table>
</form>
</body>
</html>
最终效果
总结:此案例是最简单的demo,有一些bug没修复,同学们可以自己修改bug,比如没有登录状态下不能通过url直接访问显示所有页面。