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

相关推荐
V+zmm101344 小时前
小说实体书商城微信小程序ssm+论文源码调试讲解
java·小程序·毕业设计·mvc·ssm·课程设计
tian-ming5 小时前
JavaWeb后端开发案例——苍穹外卖day01
spring boot·spring
xybm16055 小时前
MTSET可溶于DMSO、DMF、THF等有机溶剂,并在水中有轻微的溶解性,91774-25-3
java·spring·eclipse·tomcat·1024程序员节
dream_ready5 小时前
四万字长文SpringBoot、Spring、SpringMVC等相关面试题(注:该篇博客将会持续维护 最新维护时间:2024年11月12日)
java·spring boot·后端·spring·面试·1024程序员节
张铁铁是个小胖子6 小时前
显示微服务间feign调用的日志
java·spring·微服务
Beekeeper&&P...6 小时前
spring中r类是什么
spring·r语言
V+zmm101347 小时前
校园服务平台小程序ssm+论文源码调试讲解
java·小程序·毕业设计·mvc·课程设计·1024程序员节
Neoest10 小时前
场景解决之mybatis当中resultType= map时,因某个字段为null导致返回的map的key不存在怎么处理
mybatis
噜啦啦噜啦啦噜啦噜啦嘞噜啦噜啦11 小时前
源码解析-Spring Eureka
java·spring·eureka
ModelBulider11 小时前
九、HttpMessageConverter
java·开发语言·后端·spring·springmvc