SSM整合加增上改查
一、导入依赖:
< properties>
< spring-varsion> 5.1.3.RELEASE</ spring-varsion>
</ properties>
< dependencies>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 8.0.13</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis</ artifactId>
< version> 3.5.2</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis-spring</ artifactId>
< version> 1.3.2</ version>
</ dependency>
< dependency>
< groupId> commons-dbcp</ groupId>
< artifactId> commons-dbcp</ artifactId>
< version> 1.4</ version>
</ dependency>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.12</ version>
</ dependency>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
< version> 1.18.18</ version>
</ dependency>
< dependency>
< groupId> org.slf4j</ groupId>
< artifactId> slf4j-api</ artifactId>
< version> 1.7.5</ version>
</ dependency>
< dependency>
< groupId> log4j</ groupId>
< artifactId> log4j</ artifactId>
< version> 1.2.17</ version>
</ dependency>
< dependency>
< groupId> com.github.pagehelper</ groupId>
< artifactId> pagehelper</ artifactId>
< version> 5.1.2</ version>
</ dependency>
< dependency>
< groupId> org.jdom</ groupId>
< artifactId> jdom</ artifactId>
< version> 1.1.3</ version>
</ dependency>
< dependency>
< groupId> dom4j</ groupId>
< artifactId> dom4j</ artifactId>
< version> 1.6.1</ version>
</ dependency>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
< version> 1.18.18</ version>
</ dependency>
< dependency>
< groupId> jstl</ groupId>
< artifactId> jstl</ artifactId>
< version> 1.2</ version>
</ dependency>
< dependency>
< groupId> javax.servlet</ groupId>
< artifactId> javax.servlet-api</ artifactId>
< version> 3.1.0</ version>
</ dependency>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
< version> 1.16.22</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-webmvc</ artifactId>
< version> 5.3.5</ version>
< scope> compile</ scope>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-jdbc</ artifactId>
< version> 5.1.3.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-aspects</ artifactId>
< version> ${spring-varsion}</ version>
</ dependency>
</ dependencies>
二、创建包
1.controller包
2.entity包
3.mapper包
4.service包
三、配置web.XML文件
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
< web-app>
< display-name> Archetype Created Web Application</ display-name>
< context-param>
< param-name> contextConfigLocation</ param-name>
< param-value> classpath*:applicationContext.xml</ param-value>
</ context-param>
< filter>
< filter-name> characterEncodingFilter</ filter-name>
< filter-class> org.springframework.web.filter.CharacterEncodingFilter</ filter-class>
</ filter>
< filter-mapping>
< filter-name> characterEncodingFilter</ filter-name>
< url-pattern> /*</ url-pattern>
</ filter-mapping>
< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener</ listener-class>
</ listener>
< servlet>
< servlet-name> springmvc</ servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet</ servlet-class>
< init-param>
< param-name> contextConfigLocation</ param-name>
< param-value> classpath*:springmvc.xml</ param-value>
</ init-param>
< load-on-startup> 1</ load-on-startup>
</ servlet>
< servlet-mapping>
< servlet-name> springmvc</ servlet-name>
< url-pattern> /</ url-pattern>
</ servlet-mapping>
</ web-app>
四、配置applicationContext.xml文件同时配置mybatis文件
<?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: 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
https://www.springframework.org/schema/context/spring-context.xsd" >
< bean id = " dateSource" class = " org.apache.commons.dbcp.BasicDataSource" >
< property name = " driverClassName" value = " com.mysql.cj.jdbc.Driver" > </ property>
< property name = " url" value = " jdbc:mysql://localhost:3306/mybatis?useSSL=true& useUnicode=true& characterEncoding=UTF8& serverTimezone=GMT%2B8" > </ property>
< property name = " username" value = " root" > </ property>
< property name = " password" value = " 1234" > </ property>
</ bean>
< bean id = " SqlSessionFactoryBean" class = " org.mybatis.spring.SqlSessionFactoryBean" >
< property name = " dataSource" ref = " dateSource" > </ property>
< property name = " mapperLocations" value = " classpath*:mapper/*Mapper.xml" > </ property>
< property name = " typeAliasesPackage" value = " com.xiong.entity" > </ property>
< property name = " configLocation" value = " classpath:mybatis-config.xml" > </ property>
</ bean>
< bean class = " org.mybatis.spring.mapper.MapperScannerConfigurer" >
< property name = " basePackage" value = " com.xiong.mapper" > </ property>
</ bean>
< context: component-scan base-package = " com.xiong.mapper,com.xiong.service" > </ context: component-scan>
</ beans>
1.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
< configuration>
< settings>
< setting name = " logImpl" value = " STDOUT_LOGGING" />
</ settings>
< plugins>
< plugin interceptor = " com.github.pagehelper.PageInterceptor" > </ plugin>
</ plugins>
</ configuration>
五、配置SpringMVC的文件
<?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: context= " http://www.springframework.org/schema/context"
xmlns: mvc= " http://www.springframework.org/schema/mvc"
xsi: schemaLocation= " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd" >
< context: component-scan base-package = " com.xiong.controller" > </ context: component-scan>
< mvc: annotation-driven> </ mvc: annotation-driven>
< bean class = " org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name = " prefix" value = " /" > </ property>
< property name = " suffix" value = " .jsp" > </ property>
</ bean>
</ beans>
六、编写java代码
1.controller包下UserController类代码
package com. xiong. controller;
import com. github. pagehelper. PageInfo;
import com. xiong. entity. Users;
import com. xiong. service. UserService;
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;
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping ( "/setAll" )
public String selAll ( @RequestParam ( value = "pageNum" , required = true , defaultValue = "1" ) Integer pageNum,
@RequestParam ( value = "pageSize" , required = true , defaultValue = "4" ) Integer pageSize,
Model model) {
PageInfo< Users> pageInfo = userService. selAll ( pageNum, pageSize) ;
model. addAttribute ( "pageInfo" , pageInfo) ;
return "index" ;
}
@RequestMapping ( "/addOrUpdate" )
public String addOrUpdate ( Integer id, Model model) {
if ( id!= null) {
Users users = userService. selbyId ( id) ;
model. addAttribute ( "users" , users) ;
}
return "addOrUpdate" ;
}
@RequestMapping ( "/update" )
public String update ( Users users) {
userService. update ( users) ;
return "redirect:/setAll" ;
}
@RequestMapping ( "/add" )
public String add ( Users users) {
int i = userService. addUsers ( users) ;
return "redirect:/setAll" ;
}
@RequestMapping ( "/del" )
public String del ( Integer id) {
int i = userService. delUsers ( id) ;
return "redirect:/setAll" ;
}
}
2.entity包下Users代码
package com. xiong. entity;
import lombok. Data;
@Data
public class Users {
private long id;
private String name;
private Integer age;
private String email;
}
3.mapper包下UserMapper代码
package com. xiong. mapper;
import com. xiong. entity. Users;
import java. util. List;
public interface UserMapper {
List< Users> selAll ( ) ;
int addUsers ( Users users) ;
int delUsers ( Integer id) ;
Users selbyId ( Integer id) ;
int update ( Users users) ;
}
4.service包下impl包下UserServiceImpl代码
package com. xiong. service. impl;
import com. github. pagehelper. PageHelper;
import com. github. pagehelper. PageInfo;
import com. xiong. entity. Users;
import com. xiong. mapper. UserMapper;
import com. xiong. service. UserService;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. beans. factory. annotation. Qualifier;
import org. springframework. stereotype. Service;
import javax. annotation. Resource;
import java. util. List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public PageInfo< Users> selAll ( Integer pageNum, Integer pageSize) {
PageHelper. startPage ( pageNum, pageSize) ;
List< Users> users = userMapper. selAll ( ) ;
PageInfo< Users> pageInfo= new PageInfo < > ( users) ;
return pageInfo;
}
@Override
public int addUsers ( Users users) {
return userMapper. addUsers ( users) ;
}
@Override
public int delUsers ( Integer id) {
return userMapper. delUsers ( id) ;
}
@Override
public Users selbyId ( Integer id) {
return userMapper. selbyId ( id) ;
}
@Override
public int update ( Users users) {
return userMapper. update ( users) ;
}
}
5.service包下UserService代码
package com. xiong. service;
import com. github. pagehelper. PageInfo;
import com. xiong. entity. Users;
import java. util. List;
public interface UserService {
PageInfo< Users> selAll ( Integer pageNum, Integer pageSize) ;
int addUsers ( Users users) ;
int delUsers ( Integer id) ;
Users selbyId ( Integer id) ;
int update ( Users users) ;
}
6.resources包下mapper包下文件
<?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.xiong.mapper.UserMapper" >
< select id = " selAll" resultType = " com.xiong.entity.Users" >
select * from users
</ select>
< insert id = " addUsers" >
INSERT users ( id, NAME, age, email )
VALUES
(
NULL,#{name},#{age},#{email})
</ insert>
< delete id = " delUsers" >
delete from users where id=#{id}
</ delete>
< select id = " selbyId" resultType = " com.xiong.entity.Users" >
select *from users where id=#{id}
</ select>
< update id = " update" >
UPDATE `mybatis`.`users`
< set>
`name` = #{name},
`age` = #{age},
`email` = #{email}
WHERE
`id` = #{id};
</ set>
</ update>
</ mapper>
7.resources包下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: 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
https://www.springframework.org/schema/context/spring-context.xsd" >
< bean id = " dateSource" class = " org.apache.commons.dbcp.BasicDataSource" >
< property name = " driverClassName" value = " com.mysql.cj.jdbc.Driver" > </ property>
< property name = " url" value = " jdbc:mysql://localhost:3306/mybatis?useSSL=true& useUnicode=true& characterEncoding=UTF8& serverTimezone=GMT%2B8" > </ property>
< property name = " username" value = " root" > </ property>
< property name = " password" value = " 1234" > </ property>
</ bean>
< bean id = " SqlSessionFactoryBean" class = " org.mybatis.spring.SqlSessionFactoryBean" >
< property name = " dataSource" ref = " dateSource" > </ property>
< property name = " mapperLocations" value = " classpath*:mapper/*Mapper.xml" > </ property>
< property name = " typeAliasesPackage" value = " com.xiong.entity" > </ property>
< property name = " configLocation" value = " classpath:mybatis-config.xml" > </ property>
</ bean>
< bean class = " org.mybatis.spring.mapper.MapperScannerConfigurer" >
< property name = " basePackage" value = " com.xiong.mapper" > </ property>
</ bean>
< context: component-scan base-package = " com.xiong.mapper,com.xiong.service" > </ context: component-scan>
</ beans>
8.resources包下mybatis-config.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
< configuration>
< settings>
< setting name = " logImpl" value = " STDOUT_LOGGING" />
</ settings>
< plugins>
< plugin interceptor = " com.github.pagehelper.PageInterceptor" > </ plugin>
</ plugins>
</ configuration>
9.resources包下springmvc.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: context= " http://www.springframework.org/schema/context"
xmlns: mvc= " http://www.springframework.org/schema/mvc"
xsi: schemaLocation= " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd" >
< context: component-scan base-package = " com.xiong.controller" > </ context: component-scan>
< mvc: annotation-driven> </ mvc: annotation-driven>
< bean class = " org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name = " prefix" value = " /" > </ property>
< property name = " suffix" value = " .jsp" > </ property>
</ bean>
</ beans>
10.webapp包下WEB-INF包下文件
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
< web-app>
< display-name> Archetype Created Web Application</ display-name>
< context-param>
< param-name> contextConfigLocation</ param-name>
< param-value> classpath*:applicationContext.xml</ param-value>
</ context-param>
< filter>
< filter-name> characterEncodingFilter</ filter-name>
< filter-class> org.springframework.web.filter.CharacterEncodingFilter</ filter-class>
< init-param>
< param-name> encoding</ param-name>
< param-value> UTF-8</ param-value>
</ init-param>
</ filter>
< filter-mapping>
< filter-name> characterEncodingFilter</ filter-name>
< url-pattern> /*</ url-pattern>
</ filter-mapping>
< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener</ listener-class>
</ listener>
< servlet>
< servlet-name> springmvc</ servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet</ servlet-class>
< init-param>
< param-name> contextConfigLocation</ param-name>
< param-value> classpath*:springmvc.xml</ param-value>
</ init-param>
< load-on-startup> 1</ load-on-startup>
</ servlet>
< servlet-mapping>
< servlet-name> springmvc</ servlet-name>
< url-pattern> /</ url-pattern>
</ servlet-mapping>
</ web-app>
11.webapp包下addOrUpdate.jsp文件
<%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2021-05-12
Time: 7:01
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>添加</title>
</head>
<style type="text/css">
.box{
margin-top: 200px;
margin: 0 auto;
width: 300px;
height: 400px;
padding: 20px;
border: 1px #DCDFE6;
text-align: center;
}
.box:hover{
box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04)
}
</style>
<body>
<div class="box">
<table class="table table-bordered">
<form class="form-horizontal" action="${users==null?'/add':'/update'}">
<div class="form-group">
<label for="id" class="col-sm-2 control-label">编号</label>
<div class="col-sm-10">
<input type="id" name="id" class="form-control" id="id" value="${users.id}" placeholder="id" <c:if test="${users!=null}">readonly</c:if>>
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">姓名</label>
<div class="col-sm-10">
<input type="name" name="name" class="form-control" id="name" value="${users.name}" placeholder="Name">
</div>
</div>
<div class="form-group">
<label for="age" class="col-sm-2 control-label">年龄</label>
<div class="col-sm-10">
<input type="age" name="age" class="form-control" id="age" value="${users.age}" placeholder="age">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">邮箱</label>
<div class="col-sm-10">
<input type="email" name="email" class="form-control" id="email" value="${users.email}" placeholder="Email">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">提交</button>
</div>
</div>
</form>
</table>
</div>
</body>
</html>
12.webapp包下index.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>Bootstrap 101 Template</title>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 -->
<!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 -->
<!--[if lt IE 9]>
<script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->
<style type="text/css">
.box{
margin-top: 200px;
margin: 0 auto;
width: 800px;
height: 400px;
padding: 20px;
border: 1px #DCDFE6;
text-align: center;
}
.box:hover{
box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04)
}
</style>
</head>
<body>
<div class="box">
<a href="/addOrUpdate"><button type="button" class="btn btn-primary" style="float:left">(添加)Add</button></a>
<table class="table table-bordered" >
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>邮箱</th>
<th>操作</th>
</tr>
<c:forEach items="${pageInfo.list}" var="user">
<tr class="warning">
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
<td>${user.email}</td>
<td>
<a href="/addOrUpdate?id=${user.id}">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
</a>
<a href="/del?id=${user.id}">
<span class="glyphicon glyphicon-trash" aria-hidden="true" lang="${user.id}"></span>
</a>
</td>
</tr>
</c:forEach>
</table>
<nav aria-label="Page navigation" class="page">
<ul class="pagination">
<li>
<c:if test="${pageInfo.hasPreviousPage}">
<a href="/setAll?pageNum=${pageInfo.pageNum-1}" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</c:if>
</li>
<c:forEach begin="1" end="${pageInfo.pages}" var="i">
<li
<c:if test="${i==pageInfo.pageNum}">
class="active"
</c:if>
><a href="/setAll?pageNum=${i}" >${i}</a></li>
</c:forEach>
<li>
<c:if test="${pageInfo.hasNextPage}">
<a href="/setAll?pageNum=${pageInfo.pageNum+1}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</c:if>
</li>
</ul>
</nav>
</div>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" crossorigin="anonymous"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
</body>
</html>