新手SSM框架学习过程
开发工具:IDEA2020
JDK1.8
Mysql5.7
先上一个项目结构:
代码如下:
pom.xml:
`
4.0.0
org.example
SSM-SpringMVc
1.0-SNAPSHOT
4.3.3.RELEASE
org.apache.shiro
shiro-core
1.3.2
org.apache.shiro
shiro-spring
1.3.2
org.apache.shiro
shiro-web
1.3.2
junit
junit
4.12
javax.servlet
javax.servlet-api
4.0.0-b07
provided
log4j
log4j
1.2.17
org.mybatis
mybatis
3.4.1
org.projectlombok
lombok
1.18.12
provided
com.github.penggle
kaptcha
2.3.2
javax.servlet
javax.servlet-api
commons-dbcp
commons-dbcp
1.4
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.projectlombok
lombok
1.18.12
commons-io
commons-io
2.6
commons-fileupload
commons-fileupload
1.4
org.springframework
spring-webmvc
4.1.6.RELEASE
com.alibaba
druid
1.1.21
mysql
mysql-connector-java
5.1.38
com.mchange
c3p0
0.9.2.1
org.mybatis
mybatis-spring
1.3.0
org.apache.geronimo.bundles
aspectjweaver
1.6.8_2
jstl
jstl
1.2
com.fasterxml.jackson.core
jackson-databind
2.9.1
com.github.pagehelper
pagehelper
5.0.0
SSM
src/main/java
**/*.xml
`
实体类:
user
package cn.fvti.Study.Bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String name;
private String password;
private String sex;
private String address;
private String telephone;
private String email;
private Integer PUID;
}
UserMapper
package cn.fvti.Study.Dao;
import cn.fvti.Study.Bean.User;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserMapper {
// 查询所有
List<User>findAllUser();
// 通过ID查询用户
List<User> findUserByID(Integer id);
// 通过id删除用户
void deleteUserById(Integer id);
// 注册用户
int insertUser(User user);
// 登录用户
User findLogin(String name,String password);
//查询用户的所有记录数
int selectTotalCount();
// 查询所有用户,根据分页来查询
PageInfo<User> findUserLimit(int emptyPage, int Pagesize);
}
userService:
package cn.fvti.Study.Service;
import cn.fvti.Study.Bean.User;
import com.github.pagehelper.PageInfo;
import java.util.List;
public interface UserService {
// 查询所有
List<User> findAllUser();
// 通过ID查询用户
List<User> findUserByID(Integer id);
// 注册用户
int insertUser(User user);
// 登录用户
boolean findLogin(String name,String password);
//通过id删除用户
void deleteUserById(Integer id);
// 查询所有用户,根据分页来查询
PageInfo<User> findUserLimit(int emptyPage, int Pagesize);
//查询用户的所有记录数
int selectTotalCount();
}
userServcieImpl
package cn.fvti.Study.Service.Impl;
import cn.fvti.Study.Bean.User;
import cn.fvti.Study.Dao.UserMapper;
import cn.fvti.Study.Service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper mapper;
@Override
public List<User> findAllUser() {
return mapper.findAllUser();
}
@Override
public List<User> findUserByID(Integer id) {
return mapper.findUserByID(id);
}
@Override
public int insertUser(User user) {
return mapper.insertUser(user);
}
@Override
public boolean findLogin(String name,String password) {
System.out.println("输入的账号:" + name);
System.out.println("输入的密码:" + password);
User login = mapper.findLogin(name,password);
if (login != null) {
if (login.getName().equals(name) && login.getPassword().equals(password))
return true;
}
return false;
}
@Override
public void deleteUserById(Integer id) {
mapper.deleteUserById(id);
System.out.println("你删除的用户id是:"+id);
}
@Override
public PageInfo<User> findUserLimit(int emptyPage,int PageSize) {
// 开启分页支持
PageHelper.startPage(emptyPage,PageSize);
//调用dao层查询所有
List<User> allUser = mapper.findAllUser();
// 分页的相关信息
PageInfo pageInfo=new PageInfo(allUser);
System.out.println("获取当页码:"+pageInfo.getPageNum());
return pageInfo;
}
@Override
public int selectTotalCount() {
return mapper.selectTotalCount();
}
}
Mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.fvti.Study.Dao.UserMapper">
<!--查询所有用户-->
<select id="findAllUser" resultType="cn.fvti.Study.Bean.User">
select * from ssm.user
</select>
<select id="findUserByID" resultType="cn.fvti.Study.Bean.User" parameterType="int">
select * from ssm.user where id =#{id};
</select>
<select id="findLogin" resultType="cn.fvti.Study.Bean.User" parameterType="java.lang.String">
select * from ssm.user where name = #{param1} and password =#{param2}
</select>
<insert id="insertUser" parameterType="cn.fvti.Study.Bean.User" >
insert into ssm.user values (#{id},#{name},#{password},#{sex},#{telephone},#{address},#{email},#{PUID})
</insert>
<!--通过id删除用户-->
<delete id="deleteUserById" parameterType="java.lang.Integer" >
delete from ssm.user where id = #{id}
</delete>
<!--查询所有用户的总记录数-->
<select id="selectTotalCount" resultType="int">
select count(id) from ssm.user;
</select>
<!--分页查询用户-->
<select id="findUserLimit" resultType="cn.fvti.Study.Bean.User" parameterType="int">
select * from user where id limit #{param1},#{param2}
</select>
</mapper>
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8
username=root
password=123456
log4j.properties
##配置文件log4j.properties
### 设置###
### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
log4j.rootLogger=info,fileAppender,consoleAppender
#\u6253\u5370\u65e5\u5fd7\u5230\u63a7\u5236\u53f0\uff0c\u5982\u9700\u8981\u6b64\u529f\u80fd,\u5728\u7b2c\u4e00\u884c\u672b\u5c3e\u52a0\u4e0a\u5185\u5bb9\uff1a,consoleAppender
log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.Target = System.out
log4j.appender.consoleAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern = %d %p %c %L %M - %m%x%n
#\u6253\u5370\u7cfb\u7edf\u65e5\u5fd7\u5230\u6587\u4ef6
log4j.appender.fileAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.fileAppender.File=../logs/pats-client/pats-client.log
log4j.appender.fileAppender.DatePattern = '_'yyyyMMdd
log4j.appender.fileAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.fileAppender.layout.ConversionPattern = %d %p %c %L %M - %m%x%n
#source\u65e5\u5fd7
log4j.logger.com.pats.client.log.datalog.SourceDataLog=INFO,sourceDataLog
log4j.appender.sourceDataLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.sourceDataLog.File=../logs/pats-client/source_data.log
log4j.appender.sourceDataLog.DatePattern = '_'yyyyMMdd
log4j.appender.sourceDataLog.layout = org.apache.log4j.PatternLayout
log4j.appender.sourceDataLog.layout.ConversionPattern = [%d]%m%x%n
log4j.additivity.com.pats.client.log.datalog.SourceDataLog = false
#websocket\u6570\u636e\u65e5\u5fd7
log4j.logger.com.pats.client.log.AskPriceModelDataLog = DEBUG,AskPriceDataLog
log4j.appender.AskPriceDataLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.AskPriceDataLog.File=../logs/pats-client/askPrice_data.log
log4j.appender.AskPriceDataLog.DatePattern = '_'yyyyMMdd
log4j.appender.AskPriceDataLog.layout=org.apache.log4j.PatternLayout
log4j.appender.AskPriceDataLog.layout.ConversionPattern=%d %p %c %L %M - %m%x%n
log4j.additivity.com.pats.client.log.AskPriceModelDataLog = false
#websocket\u6570\u636e\u65e5\u5fd7
log4j.logger.com.pats.client.log.BidPriceModelDataLog = DEBUG,BidPriceDataLog
log4j.appender.BidPriceDataLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.BidPriceDataLog.File=../logs/pats-client/bidPrice_data.log
log4j.appender.BidPriceDataLog.DatePattern = '_'yyyyMMdd
log4j.appender.BidPriceDataLog.layout=org.apache.log4j.PatternLayout
log4j.appender.BidPriceDataLog.layout.ConversionPattern=%d %p %c %L %M - %m%x%n
log4j.additivity.com.pats.client.log.BidPriceModelDataLog = false
#websocket\u6570\u636e\u65e5\u5fd7
log4j.logger.com.pats.client.log.DealLogModelDataLog = DEBUG,DealLogDataLog
log4j.appender.DealLogDataLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DealLogDataLog.File=../logs/pats-client/dealLog_data.log
log4j.appender.DealLogDataLog.DatePattern = '_'yyyyMMdd
log4j.appender.DealLogDataLog.layout=org.apache.log4j.PatternLayout
log4j.appender.DealLogDataLog.layout.ConversionPattern=%d %p %c %L %M - %m%x%n
log4j.additivity.com.pats.client.log.DealLogModelDataLog = false
#websocket\u6570\u636e\u65e5\u5fd7
log4j.logger.com.pats.client.log.FullModelDataLog = DEBUG,FullDataLog
log4j.appender.FullDataLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FullDataLog.File=../logs/pats-client/full_data.log
log4j.appender.FullDataLog.DatePattern = '_'yyyyMMdd
log4j.appender.FullDataLog.layout=org.apache.log4j.PatternLayout
log4j.appender.FullDataLog.layout.ConversionPattern=%d %p %c %L %M - %m%x%n
log4j.additivity.com.pats.client.log.FullModelDataLog = false
#websocket\u6570\u636e\u65e5\u5fd7
log4j.logger.com.pats.client.log.PositionModelDataLog = DEBUG,PositionDataLog
log4j.appender.PositionDataLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.PositionDataLog.File=../logs/pats-client/position_data.log
log4j.appender.PositionDataLog.DatePattern = '_'yyyyMMdd
log4j.appender.PositionDataLog.layout=org.apache.log4j.PatternLayout
log4j.appender.PositionDataLog.layout.ConversionPattern=%d %p %c %L %M - %m%x%n
log4j.additivity.com.pats.client.log.PositionModelDataLog = false
#websocket\u6570\u636e\u65e5\u5fd7
log4j.logger.com.pats.client.log.VwapModelDataLog = DEBUG,VwapDataLog
log4j.appender.VwapDataLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.VwapDataLog.File=../logs/pats-client/vwap_data.log
log4j.appender.VwapDataLog.DatePattern = '_'yyyyMMdd
log4j.appender.VwapDataLog.layout=org.apache.log4j.PatternLayout
log4j.appender.VwapDataLog.layout.ConversionPattern=%d %p %c %L %M - %m%x%n
log4j.additivity.com.pats.client.log.VwapModelDataLog = false
#websocket\u6570\u636e\u65e5\u5fd7
log4j.logger.com.pats.client.log.ProductDataLog = DEBUG,ProductDataLog
log4j.appender.ProductDataLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ProductDataLog.File=../logs/pats-client/product_data.log
log4j.appender.ProductDataLog.DatePattern = '_'yyyyMMdd
log4j.appender.ProductDataLog.layout=org.apache.log4j.PatternLayout
log4j.appender.ProductDataLog.layout.ConversionPattern=%d %p %c %L %M - %m%x%n
log4j.additivity.com.pats.client.log.ProductDataLog = false
#websocket\u6570\u636e\u65e5\u5fd7
log4j.logger.com.pats.client.log.ProductErrorDataLog = DEBUG,ProductErrorDataLog
log4j.appender.ProductErrorDataLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ProductErrorDataLog.File=../logs/pats-client/productError_data.log
log4j.appender.ProductErrorDataLog.DatePattern = '_'yyyyMMdd
log4j.appender.ProductErrorDataLog.layout=org.apache.log4j.PatternLayout
log4j.appender.ProductErrorDataLog.layout.ConversionPattern=%d %p %c %L %M - %m%x%n
log4j.additivity.com.pats.client.log.ProductErrorDataLog = false
#jdbc\u65e5\u5fd7
#log4j.logger.org.springframework.jdbc=debug
#log4j.logger.org.mybatis.spring=debug
#log4j.logger.com.pats.client.dao=debug
Spring-config.xml
`
`
Spring-config.xml
`
`
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:p="http://www.springframework.org/schema/p"
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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置Controller扫描 -->
<context:component-scan base-package="cn.fvti.Study.Controller" />
<!-- 配置注解驱动 -->
<mvc:annotation-driven />
<!-- 配置静态资源映射 -->
<mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>
<!-- <mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/admin/**"/>
<!– 配置不拦截请求的地址 –>
<mvc:exclude-mapping path="/index"/>
<mvc:exclude-mapping path="/login"/>
<bean class="com.xian.web.interceptor.LoginInterceptor2"/>
</mvc:interceptor>
</mvc:interceptors>-->
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Spring-config.xml</param-value>
</context-param>
<!--加载监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置过滤器-->
<!--异步请求必须在web.xml将DispatcherServlet下的子元素<async-supported>true</async-supported>设置为true-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:SpringMvc.xml
1
dispatcherServlet
/
cap
com.google.code.kaptcha.servlet.KaptchaServlet
kaptcha.border
no
kaptcha.textproducer.char.length
5
kaptcha.textproducer.char.string
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
kaptcha.background.clear.to
211,229,237
kaptcha.session.key
captcha
cap
/captcha
/index.jsp
jsp视图
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>SSM整合</title>
<style type="text/css">
table {
margin: auto;
color: blue;
text-align: center;
}
#add {
color: aqua;
}
</style>
</head>
<body>
<form action="/user/login" method="post">
<table bgcolor="#663399" border="2">
<tr>
<th>用户名:</th>
<th><input type="text" name="name" onblur=""></th>
</tr>
<tr>
<th>用户密码:</th>
<th><input type="text" name="password"></th>
</tr>
<tr align="center">
<th><input type="submit" value="登录" ></th>
<th><a href="/user/add" input type="submit" value="注册" id="add">注册</a></th>
</tr>
</table>
</form>
</body>
</html>
welcome.jsp
<%@ page import="cn.fvti.Study.Service.Impl.UserServiceImpl" %>
<%@ page import="cn.fvti.Study.Service.UserService" %>
<%@ page import="cn.fvti.Study.Bean.Page" %><%
request.setCharacterEncoding("UTF-8");
UserService userService = new UserServiceImpl();
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>欢迎</title>
<style>
h1{
text-align: center;
}
.search {
color: aqua;
width: 120px;
}
</style>
</head>
<body>
<h1>SSM简易系统</h1>
<div>用户:${user.name}欢迎登录</div>
<form action="/user/findUserById" method="post">
搜索:<input type="search" name="id" class="search">
<input type="submit" value="提交" >
<br>
</form>
<form>
<div class="col-md-12 column">
<table bgcolor="red" align="center" border="2">
<tr>
<th>用户ID</th>
<th>用户性名</th>
<th>用户密码</th>
<th>用户性别</th>
<th>用户手机</th>
<th>用户地址</th>
<th>用户邮箱</th>
<th>用户PUID</th>
<th>用户操作</th>
</tr>
<c:forEach var="alluser" items="${userLimit.list}">
<tr>
<td>${alluser.id}</td>
<td>${alluser.name}</td>
<td>${alluser.password}</td>
<td>${alluser.sex}</td>
<td>${alluser.telephone}</td>
<td>${alluser.address}</td>
<td>${alluser.email}</td>
<td>${alluser.PUID}</td>
<td><a href="/user/add" input type="submit" value="新增" >新增</a>
<a href="/user/delete" input type="submit" value="删除">删除</a>
<a href="/user/updateUser" input type="submit" value="修改">修改</a> </td>
</tr>
</c:forEach>
</table>
<p align="left">总记录数:${userLimit.total}</p>
<p align="right">共${userLimit.pages}页:
<a href="">现在是:${userLimit.pageNum}页</a>
<a href="http://localhost:8080/user/PageIndex?emptyPage=${userLimit.pageNum-1}">上一页</a>
<a href="http://localhost:8080/user/PageIndex?emptyPage=${userLimit.pageNum+1}">下一页</a>
<a href="#">末页</a> </p>
</div>
</form>
</body>
</html>
adduser.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加用户</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<form action="/user/userAddTest" method="post">
<table border="2" align="center" bgcolor="#8a2be2">
<tr>
<th>用户ID:</th>
<td><input type="text" name="id" id="user_id"></td>
</tr>
<tr>
<th>用户姓名:</th>
<td><input type="text" name="name"></td>
</tr>
<tr>
<th>用户密码:</th>
<td><input type="password" name="password"></td>
</tr>
<tr>
<th>用户性别:</th>
<td><input type="text" name="telephone"></td>
</tr>
<tr>
<th>用户号码:</th>
<td><input type="text" name="id"></td>
</tr>
<tr>
<th>用户地址:</th>
<td><input type="text" name="address"></td>
</tr>
<tr>
<th>用户邮箱:</th>
<td><input type="email" name="email"></td>
</tr>
<tr>
<th>用户PUID:</th>
<td><input type="text" name="puid"></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
delete.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>删除用户</title>
</head>
<body>
<form action="/user/userid" method="post">
请输入你要删除的ID:<input type="text" name="id"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
Error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>404</title>
</head>
<body>
<h3>很遗憾,账号密码输入错误</h3>
${msg}<br>
<a href="/index.jsp">点我回到登录页面</a>
</body>
</html>
welcome02.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="C" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<div>
<form>
<div class="col-md-12 column">
<table bgcolor="red" align="center" border="2">
<tr>
<th>用户ID</th>
<th>用户性名</th>
<th>用户密码</th>
<th>用户性别</th>
<th>用户手机</th>
<th>用户地址</th>
<th>用户邮箱</th>
<th>用户PUID</th>
</tr>
<c:forEach items="${userById}" var="findId">
<tr>
<td>${findId.id}</td>
<td>${findId.name}</td>
<td>${findId.password}</td>
<td>${findId.sex}</td>
<td>${findId.telephone}</td>
<td>${findId.address}</td>
<td>${findId.email}</td>
<td>${findId.PUID}</td>
</tr>
</c:forEach>
</table>
</div>
</form>
</div>
</body>
</html>
自己叨叨一句吧:
这个简易版本的SSM还有一部分可以改进的空间的,但是由于自己的原因,大学废了几年,现在马上就要毕业了,所以前端这部分自己是太弱了,CSS还没学到家,
别说后面一部分了。没人带,有时候一个bug可以解决很久,不敢浪费时间,所以这个简易的ssm只能这样了,会前端的自己拿去改改。
最后附上自己的天翼云盘连接:
https://cloud.189.cn/t/VVbu637nYN73