当前位置: 首页 > article >正文

基于SSM框架(Spring, Spring MVC, MyBatis)的矿场仓储管理系统的基础示例

一个基于SSM框架(Spring, Spring MVC, MyBatis)的矿场仓储管理系统的基础示例,这是一个简单的用户登录功能的实现。这个例子将包括后端的部分代码,因为前端涉及到的设计元素较多,这里主要展示后端逻辑。

1. 创建数据库表

首先,我们需要创建一个用户表来存储用户信息。假设我们使用MySQL数据库,创建用户表的SQL语句如下:

CREATE TABLE `user` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `username` VARCHAR(50) NOT NULL UNIQUE,
  `password` VARCHAR(50) NOT NULL
);

2. Maven依赖

在项目的pom.xml文件中添加所需的依赖:

<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.9</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.9</version>
    </dependency>
    
    <!-- MyBatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.7</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.6</version>
    </dependency>
    
    <!-- MySQL Connector -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.23</version>
    </dependency>
    
    <!-- Servlet API -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

3. 配置文件

Spring配置文件 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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.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">

    <context:component-scan base-package="com.example.miningwarehouse"/>
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mining_warehouse?useSSL=false&serverTimezone=UTC"/>
        <property name="username" value="root"/>
        <property name="password" value="password"/>
    </bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.example.miningwarehouse.mapper"/>
    </bean>
    
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>
Spring MVC配置文件 spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
       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/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.example.miningwarehouse"/>
    
    <mvc:annotation-driven/>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

4. 实体类 User.java

package com.example.miningwarehouse.entity;

public class User {
    private Integer id;
    private String username;
    private String password;

    // Getters and Setters
}

5. Mapper接口及XML映射文件

UserMapper.java
package com.example.miningwarehouse.mapper;

import com.example.miningwarehouse.entity.User;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UserMapper {
    @Select("SELECT * FROM user WHERE username = #{username}")
    User findByUsername(@Param("username") String username);

    @Insert("INSERT INTO user(username, password) VALUES(#{username}, #{password})")
    void insertUser(User user);
}
UserMapper.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.example.miningwarehouse.mapper.UserMapper">
    <resultMap id="BaseResultMap" type="com.example.miningwarehouse.entity.User">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="username" property="username" jdbcType="VARCHAR"/>
        <result column="password" property="password" jdbcType="VARCHAR"/>
    </resultMap>

    <select id="findByUsername" resultMap="BaseResultMap" parameterType="string">
        SELECT * FROM user WHERE username = #{username}
    </select>

    <insert id="insertUser" parameterType="com.example.miningwarehouse.entity.User">
        INSERT INTO user (username, password) VALUES (#{username}, #{password})
    </insert>
</mapper>

6. Service层

UserService.java
package com.example.miningwarehouse.service;

import com.example.miningwarehouse.entity.User;
import com.example.miningwarehouse.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public User login(String username, String password) {
        User user = userMapper.findByUsername(username);
        if (user != null && user.getPassword().equals(password)) {
            return user;
        }
        return null;
    }

    public void register(User user) {
        userMapper.insertUser(user);
    }
}

7. Controller层

UserController.java
package com.example.miningwarehouse.controller;

import com.example.miningwarehouse.entity.User;
import com.example.miningwarehouse.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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public String showLoginForm() {
        return "login";
    }

    @PostMapping("/login")
    public String handleLogin(@RequestParam("username") String username,
                              @RequestParam("password") String password,
                              Model model) {
        User user = userService.login(username, password);
        if (user != null) {
            model.addAttribute("user", user);
            return "home"; // 登录成功后跳转到主页
        } else {
            model.addAttribute("error", "用户名或密码错误!");
            return "login";
        }
    }

    @GetMapping("/register")
    public String showRegisterForm() {
        return "register";
    }

    @PostMapping("/register")
    public String handleRegister(User user, Model model) {
        userService.register(user);
        model.addAttribute("message", "注册成功,请登录!");
        return "login";
    }
}

8. 视图层

视图层通常使用JSP页面来实现。这里省略了具体的JSP页面代码,您可以根据需要自行设计登录和注册页面。

以上就是一个基于SSM框架的矿场仓储管理系统中用户登录功能的基本实现。


http://www.kler.cn/a/394773.html

相关文章:

  • ABAP开发学习——ST05 ABAP SQL跟踪工具
  • GPT-5 要来了:抢先了解其创新突破
  • 算法学习第一弹——C++基础
  • 蓝桥杯c++算法学习【2】之搜索与查找(九宫格、穿越雷区、迷宫与陷阱、扫地机器人:::非常典型的必刷例题!!!)
  • 软件测试面试大全(含答案+文档)
  • 新版 idea 编写 idea 插件时,启动出现 ClassNotFound
  • 【GPTs】Gif-PT:DALL·E制作创意动图与精灵动画
  • 单片机 定时器实验 实验四
  • git配置远程仓库的认证信息
  • 吴恩达Prompt Engineering(2/9): Guidelines for Prompting
  • Perfetto中如何使用SQL语句
  • Excel根据条件动态索引单元格范围
  • PVE纵览-选择适合你的Proxmox VE存储方案:LVM、LVM-Thin、目录与ZFS对
  • docker镜像安装oracle11g
  • 互联网行业面对大数据时代新挑战如何实现数据高速传输
  • 解决 VSCode 中 C/C++ 编码乱码问题的两种方法
  • 【机器学习】K近邻算法
  • C++——视频问题总结
  • 猎板PCB罗杰斯板材的应用案例
  • 【填鸭表单】TDuckX-v2.0发布!
  • 【深度学习】神经网络优化方法 正则化方法 价格分类案例
  • 力扣-Mysql-3322- 英超积分榜排名 III(中等)
  • PyTorch——从入门到精通:PyTorch简介与安装(最新版)【PyTorch系统学习】
  • golang分布式缓存项目 Day4 一致性哈希
  • 前端权限控制代码
  • 计算机毕业设计 | SpringBoot社区物业管理系统 小区管理(附源码)