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

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

1. 创建数据库表

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

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文件中添加所需的依赖:

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 复制代码
<?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 复制代码
<?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

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
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 复制代码
<?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
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
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框架的矿场仓储管理系统中用户登录功能的基本实现。

相关推荐
程序员学姐10 分钟前
基于SpringBoot+Vue的高校社团管理系统
java·开发语言·vue.js·spring boot·后端·mysql·spring
儿时可乖了2 小时前
优化 Spring Boot 性能
spring boot·spring
cooldream20095 小时前
SpringMVC 执行流程详解
java·spring·springmvc
重生成为码农‍5 小时前
SpringMVC-Day1
mvc
ModelBulider5 小时前
SpringMVC应用专栏介绍
java·开发语言·后端·spring·springmvc
leeyayai_xixihah5 小时前
0-1实现SpringBoot项目开发(1)-SpringBoot+mybatis+mysql+Navicat
java·spring boot·spring
雷神乐乐6 小时前
MyBatis的resultType和resultMap区别
mybatis
m0_748230216 小时前
【Spring Boot】Spring AOP中的环绕通知
spring boot·后端·spring
小菜日记^_^7 小时前
Maven高级篇
java·spring boot·后端·spring·maven·intellij-idea·mybatis
计算机毕设指导67 小时前
基于SpringBoot的教学辅助平台系统【附源码】
java·spring boot·后端·mysql·spring·tomcat·mybatis