下面介绍一下用GPT4.0生成的SSM框架相关的登录和增删改查代码。没有加css样式。
1.创建数据库和表结构
CREATE DATABASE student_management;
USE student_management;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`age` int(11) NOT NULL,
`gender` varchar(255) NOT NULL,
`address` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
);
2.配置SSM框架(以Maven为例)
在pom.xml文件中添加相关依赖:
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.10</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
3.创建实体类、DAO层、Service层和Controller层
3.1实体类
Student.java:
public class Student {
private int id;
private String name;
private int age;
private String gender;
private String address;
// 构造方法、getter和setter方法省略
}
User.java:
public class User {
private int id;
private String username;
private String password;
// 构造方法、getter和setter方法省略
}
3.2DAO层(Mapper)
StudentMapper.java:
public interface StudentMapper {
List<Student> findAll();
Student findById(int id);
void insert(Student student);
void update(Student student);
void delete(int id);
}
UserMapper.java:
public interface UserMapper {
User findByUsername(String username);
}
3.3Service层
StudentService.java:
public interface StudentService {
List<Student> findAll();
Student findById(int id);
void insert(Student student);
void update(Student student);
void delete(int id);
}
StudentServiceImpl.java:
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public List<Student> findAll() {
return studentMapper.findAll();
}
@Override
public Student findById(int id) {
return studentMapper.findById(id);
}
@Override
public void insert(Student student) {
studentMapper.insert(student);
}
@Override
public void update(Student student) {
studentMapper.update(student);
}
@Override
public void delete(int id) {
studentMapper.delete(id);
}
}
UserService.java:
public interface UserService {
User findByUsername(String username);
}
UserServiceImpl.java:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User findByUsername(String username) {
return userMapper.findByUsername(username);
}
}
3.4Controller层
StudentController.java
@Controller
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping
public String list(Model model) {
List<Student> students = studentService.findAll();
model.addAttribute("students", students);
return "student_list";
}
@PostMapping
public String add(Student student) {
studentService.insert(student);
return "redirect:/students";
}
@PutMapping("/{id}")
public String update(@PathVariable int id, Student student) {
student.setId(id);
studentService.update(student);
return "redirect:/students";
}
@DeleteMapping("/{id}")
public String delete(@PathVariable int id) {
studentService.delete(id);
return "redirect:/students";
}
}
UserController.java
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/login")
public String login(String username, String password, HttpSession session) {
User user = userService.findByUsername(username);
if (user != null && user.getPassword().equals(password)) {
session.setAttribute("user", user);
return "redirect:/students";
} else {
return "login_fail";
}
}
}
4.配置Spring和MyBatis
4.1Spring
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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.example.demo"/>
<mvc:annotation-driven/>
<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/student_management?useSSL=false&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.demo.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".html"/>
</bean>
</beans>
mybatis-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<typeAlias type="com.example.demo.entity.Student" alias="Student"/>
<typeAlias type="com.example.demo.entity.User" alias="User"/>
</typeAliases>
<mappers>
<mapper resource="com/example/demo/mapper/StudentMapper.xml"/>
<mapper resource="com/example/demo/mapper/UserMapper.xml"/>
</mappers>
</configuration>
4.2mapper
StudentMapper.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.demo.mapper.StudentMapper">
<resultMap id="StudentResult" type="Student">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="gender" property="gender"/>
<result column="address" property="address"/>
</resultMap>
<select id="findAll" resultMap="StudentResult">
SELECT * FROM student
</select>
<!-- 省略其他的select、insert、update和delete语句 -->
</mapper>
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.demo.mapper.UserMapper">
<resultMap id="UserResult" type="User">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
</resultMap>
<select id="findByUsername" resultMap="UserResult" parameterType="String">
SELECT * FROM user WHERE username = #{username}
</select>
</mapper>
5.创建前端界面
login.html:
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="/user/login" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<br>
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<br>
<input type="submit" value="Login">
</form>
</body>
</html>
student_list.html:
<!DOCTYPE html>
<html>
<head>
<title>Student List</title>
</head>
<body>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Gender</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<!-- 使用循环遍历students列表,显示学生信息 -->
<#list students as student>
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td>${student.gender}</td>
<td>${student.address}</td>
<td>
<a href="/students/edit/${student.id}">Edit</a>
<a href="/students/delete/${student.id}">Delete</a>
</td>
</tr>
</#list>
</tbody>
</table>
<a href="/students/add">Add Student</a>
</body>
</html>
add_student.html:
<!DOCTYPE html>
<html>
<head>
<title>Add Student</title>
</head>
<body>
<form action="/students" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<br>
<label for="age">Age:</label>
<input type="number" id="age" name="age">
<br>
<label for="gender">Gender:</label>
<select id="gender" name="gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<br>
<label for="address">Address:</label>
<input type="text" id="address" name="address">
<br>
<input type="submit" value="Add Student">
</form>
</body>
</html>
edit_student.html:文章来源:https://www.toymoban.com/news/detail-486227.html
<!DOCTYPE html>
<html>
<head>
<title>Edit Student</title>
</head>
<body>
<form action="/students/${student.id}" method="post">
<input type="hidden" name="_method" value="PUT"> <!-- 用于模拟PUT请求 -->
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="${student.name}">
<br>
<label for="age">Age:</label>
<input type="number" id="age" name="age" value="${student.age}">
<br>
<label for="gender">Gender:</label>
<select id="gender" name="gender">
<option value="Male" <#if student.gender=='Male'>selected</#if>>Male</option>
<option value="Female" <#if student.gender=='Female'>selected</#if>>Female</option>
</select>
<br>
<label for="address">Address:</label>
<input type="text" id="address" name="address" value="${student.address}">
<br>
<input type="submit" value="Update Student">
</form>
</body>
</html>
这些配置和示例代码提供了一个关于如何在SSM项目中配置组件和实现基本功能的概念。在实际开发过程中,请务必根据需求和约束调整配置和代码。文章来源地址https://www.toymoban.com/news/detail-486227.html
到了这里,关于用GPT4实现SSM框架实现学生信息管理系统的登录和增删改查的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!