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