smbms项目
smbms超市订单管理系统
功能模块
- 登录注销
- 用户管理:增、删、改、查
- 订单管理:增、删、改、查
- 供应商管理:增、删、改、查
创建数据库和表
上图图片来自网络
搭建项目准备工作
使用Maven,依赖jar包
- 搭建一个maven web项目,使用webapp模板,新建java文件夹,标记resources文件夹
修改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"
metadata-complete="true">
</web-app>
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.liweixiao</groupId>
<artifactId>smbms</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>smbms Maven Webapp</name>
<url>http://maven.apache.org</url>
</project>
-
配置tomcat
-
测试项目能否跑起来
-
pom.xml导入项目中会使用的jar包,junit、servlet、jsp、mysql等
<dependencies>
<!--servlet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!--jsp依赖-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--jstl表达式依赖-->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<!--standard标签库-->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!--junit测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
-
创建项目包结构:com.liweixiao,dao数据库操作、filter过滤器、pojo实体类、service服务层、servlet跳转页面、util工具类
-
编写实体类,ORM映射:表-类映射
pojo包下
public class User {
//属性
private Integer id;//id
private String userCode;//用户编码
private String userName;//用户名称
private String userPassword;//用户密码
private Integer gender;//性别
private Date birthday;//出生日期
private String phone;//电话
private String address;//地址
private Integer userRole;//用户角色
private Integer createBy;//创建人
private Date creationDate;//创建时间
private Integer modifyBy;//修改人
private Date modifyDate;//修改时间
private Integer age;//年龄
private String userRoleName;//用户角色名称
public Integer getAge() {
Date date = new Date();
Integer age = date.getYear() - birthday.getYear();
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getUserRoleName() {
return userRoleName;
}
public void setUserRoleName(String userRoleName) {
this.userRoleName = userRoleName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getUserRole() {
return userRole;
}
public void setUserRole(Integer userRole) {
this.userRole = userRole;
}
public Integer getCreateBy() {
return createBy;
}
public void setCreateBy(Integer createBy) {
this.createBy = createBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
}
- 编写基础公共类
1)数据库配置文件,db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=utf-8&useSSL=true
username=root
password=123456
2)编写数据库的公共类,BaseDao
public class BaseDao {
//属性
private static String driver;
private static String url;
private static String username;
private static String password;
//静态代码块,类加载时就初始化了
static {
Properties properties = new Properties();
//通过类加载器读取对应的资源
InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
try {
properties.load(is);
} catch (IOException e) {
throw new RuntimeException(e);
}
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
}
//获取数据库连接
public static Connection getConnection(){
Connection connection =null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
//编写查询公共方法
public static ResultSet execute(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet, String sql, Object[] params) throws SQLException {
//预编译的sql,在后面直接执行
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
//setObject从1开始,params从0开始
preparedStatement.setObject(i+1,params[i]);
}
resultSet = preparedStatement.executeQuery();
return resultSet;
}
//编写增删改公共方法
public static int execute(Connection connection,PreparedStatement preparedStatement, String sql, Object[] params) throws SQLException {
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
//setObject从1开始,params从0开始
preparedStatement.setObject(i+1,params[i]);
}
int updateRows = preparedStatement.executeUpdate();
return updateRows;
}
//释放资源
public static boolean closeResource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet){
boolean flag=true;
if(resultSet != null){
try {
resultSet.close();
//GC回收
resultSet=null;
} catch (SQLException e) {
e.printStackTrace();
flag=false;
}
}
if(preparedStatement!=null){
try {
preparedStatement.close();
//GC回收
preparedStatement=null;
} catch (SQLException e) {
e.printStackTrace();
flag=false;
}
}
if(connection != null){
try {
connection.close();
//GC回收
connection=null;
} catch (SQLException e) {
e.printStackTrace();
flag=false;
}
}
return flag;
}
}
3)编写编码字符过滤器
filter包下
public class CharacterEncodingFiter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
filterChain.doFilter(servletRequest,servletResponse);
}
public void destroy() {
}
}
web.xml
<!--字符编码过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.liweixiao.filter.CharacterEncodingFiter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- 导入静态资源,calendar、css、images、js、jsp、login.jsp、error.jsp
登录功能实现
- 编写前端界面,已导入
- 设置首页,web.xml
<!--设置欢迎界面-->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
- 编写dao层用户登录接口,dao下新建user文件夹
public interface UserDao {
//得到要登录的用户
public User getLoginUser(Connection connection, String userCode,String userPassword) throws SQLException;
}
- 编写dao接口的实现类
public class UserDaoImpl implements UserDao{
public User getLoginUser(Connection connection, String userCode,String userPassword) throws SQLException {
PreparedStatement pstm=null;
ResultSet rs=null;
User user=null;
if(connection != null){
String sql="select * from smbms_user where userCode=? and userPassword=?";
Object[] params={userCode,userPassword};
rs = BaseDao.execute(connection, pstm, rs, sql, params);
if(rs.next()){
user=new User();
user.setId(rs.getInt("id"));
user.setUserCode(rs.getString("userCode"));
user.setUserName(rs.getString("userName"));
user.setUserPassword(rs.getString("userPassword"));
user.setGender(rs.getInt("gender"));
user.setBirthday(rs.getDate("birthday"));
user.setPhone(rs.getString("phone"));
user.setAddress(rs.getString("address"));
user.setUserRole(rs.getInt("userRole"));
user.setCreateBy(rs.getInt("createBy"));
user.setCreationDate(rs.getDate("creationDate"));
user.setModifyBy(rs.getInt("modifyBy"));
user.setModifyDate(rs.getDate("modifyDate"));
}
BaseDao.closeResource(null,pstm,rs);
}
return user;
}
}
- 业务接口层,service下新建user文件夹
public interface UserService {
//用户登录
public User login(String userCode,String userPassword);
}
- 业务层实现类
public class UserServiceImpl implements UserService{
//业务层都会调用dao层,所以要引入dao层
private UserDao userDao;
public UserServiceImpl(){
userDao=new UserDaoImpl();
}
public User login(String userCode, String userPassword) {
Connection connection=null;
User user=null;
try {
connection = BaseDao.getConnection();
//通过业务层调用对应的具体的数据库操作
user = userDao.getLoginUser(connection, userCode,userPassword);
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
BaseDao.closeResource(connection,null,null);
}
return user;
}
}
- 编写servlet,servlet下新建user文件夹
public class LoginServlet extends HttpServlet {
//Servlet控制层,调用业务层代码
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//System.out.println("LoginServlet--start...");
//获取用户名和密码
String userCode = req.getParameter("userCode");
String userPassword = req.getParameter("userPassword");
//和数据库中的用户密码进行对比,调用业务层
UserService userService = new UserServiceImpl();
User user = userService.login(userCode, userPassword); //这里已经把登录的人查出来了
if(user != null){//查有此人,可以登录
//将用户的信息放入Session中
req.getSession().setAttribute(Constants.USER_SESSION,user);
//跳转到内部主页
resp.sendRedirect("jsp/frame.jsp");
}else {//查无此人,无法登录
//转发回登录页面,提示:用户名或密码错误
req.setAttribute("error","用户名或者密码错误");
req.getRequestDispatcher("login.jsp").forward(req,resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
util包下
public class Constants {
public final static String USER_SESSION="userSession";
}
- 注册servlet,web.xml
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.liweixiao.servlet.user.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
登录功能优化
注销Session
public class LogoutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//移除用户的Session
req.getSession().removeAttribute(Constants.USER_SESSION);
resp.sendRedirect(req.getContextPath()+"/login.jsp");//返回登录页面
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
注册
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>com.liweixiao.servlet.user.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/jsp/logout.do</url-pattern>
</servlet-mapping>
登录拦截优化
编写一个过滤器
public class SysFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
//过滤器,从Session中获取用户
User user = (User)request.getSession().getAttribute(Constants.USER_SESSION);
if(user ==null){//说明被移除,或者注销,或者未登录
response.sendRedirect("/smbms/error.jsp");
}else {
chain.doFilter(req,resp);
}
}
public void destroy() {
}
}
注册,webxml
<!-- 用户登录过滤器-->
<filter>
<filter-name>SysFilter</filter-name>
<filter-class>com.liweixiao.filter.SysFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SysFilter</filter-name>
<url-pattern>/jsp/*</url-pattern>
</filter-mapping>
密码修改
1.导入前端页面
2.写项目,建议从底层向上写
3.UserDao接口
//修改当前用户密码
public int updatePwd(Connection connection,int id,String password) throws SQLException;
4.UserDao接口实现
//修改当前用户密码
public int updatePwd(Connection connection, int id, String password) throws SQLException {
PreparedStatement pstm=null;
int execute =0;
if(connection != null){
String sql="UPDATE smbms_user SET userPassword=? WHERE id =?";
Object params[]={password,id};
execute = BaseDao.execute(connection, pstm, sql, params);
BaseDao.closeResource(null,pstm,null);
}
return execute;
}
5.UserService接口
//根据用户id修改密码
public boolean updatePwd(int id, String pwd);
6.UserService接口实现
//根据用户id修改密码
public boolean updatePwd(int id, String pwd) {
Connection connection=null;
boolean flag=false;
//修改密码
try {
connection = BaseDao.getConnection();
if(userDao.updatePwd(connection,id,pwd)>0){
flag=true;
}
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
BaseDao.closeResource(connection,null,null);
}
return flag;
}
7.Servlet
//实现Servlet复用
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//从Session中拿id
Object o = req.getSession().getAttribute(Constants.USER_SESSION);
//String newpassword = req.getParameter("newpassword");
String newpassword = "1231234";
boolean flag = false;
if(o != null && newpassword!=null){
UserService userService = new UserServiceImpl();
flag = userService.updatePwd(((User) o).getId(), newpassword);
if(flag){
req.setAttribute("message","修改密码成功,请退出使用新密码登录。");
//密码修改成功,移除当前Session
req.getSession().removeAttribute(Constants.USER_SESSION);
}else {
req.setAttribute("message","修改密码失败。");
}
}else{
req.setAttribute("message","新密码有问题。");
}
req.getRequestDispatcher("pwdmodify.jsp").forward(req,resp);
}
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
8.注册,web.xml
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.liweixiao.servlet.user.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/jsp/user.do</url-pattern>
</servlet-mapping>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)