学工管理系统
小明:嘿,李老师,我最近在学习Java Web开发,听说您有一个学工管理系统的项目,能给我介绍一下吗?
李老师:当然可以!我们这个学工管理系统是用Spring Boot框架搭建的,整个系统包括学生信息管理、成绩录入、通知发布等功能。你想了解哪一部分呢?
小明:我对后端部分特别感兴趣,能讲讲你们是怎么设计的吗?
李老师:好的。我们采用了前后端分离的架构,后端使用Spring Boot作为主要框架,结合MyBatis进行数据库操作,同时使用了Spring Security来处理权限控制。
小明:那Spring Boot具体有什么优势呢?
李老师:Spring Boot最大的优势就是简化了Spring应用的初始搭建和开发过程。它通过自动配置机制,让开发者不需要写大量的XML配置文件,而是通过少量的注解就可以快速构建一个可运行的应用。
小明:听起来很高效啊。那你们是怎么组织项目的结构的?
李老师:我们采用的是Maven项目结构,分为几个模块:model、dao、service、controller,分别对应数据模型、数据库访问层、业务逻辑层和控制层。这样结构清晰,便于维护。
小明:那你能给我看看核心代码吗?
李老师:当然可以。这是我们的主类,也就是启动类:
@SpringBootApplication
public class StudentManagementApplication {
public static void main(String[] args) {
SpringApplication.run(StudentManagementApplication.class, args);
}
}
小明:这看起来很简单,但功能却很强大。那数据库部分是怎么处理的呢?
李老师:我们使用了MyBatis作为ORM框架,下面是一个简单的DAO接口示例:
public interface StudentMapper {
List
Student selectById(Long id);
int insert(Student student);
int update(Student student);
int deleteById(Long id);
}
小明:那对应的XML文件呢?
李老师:这里是一个MyBatis的XML映射文件,用来定义SQL语句:
<?xml version="1.0" encoding="UTF-8" ?>
<!-- StudentMapper.xml -->
<mapper namespace="com.example.mapper.StudentMapper">
<select id="selectAll" resultType="com.example.model.Student">
SELECT * FROM student
</select>
<select id="selectById" parameterType="long" resultType="com.example.model.Student">
SELECT * FROM student WHERE id = #{id}
</select>
<insert id="insert" parameterType="com.example.model.Student">
INSERT INTO student (name, gender, major, grade)
VALUES (#{name}, #{gender}, #{major}, #{grade})
</insert>
<update id="update" parameterType="com.example.model.Student">
UPDATE student SET name = #{name}, gender = #{gender}, major = #{major}, grade = #{grade} WHERE id = #{id}
</update>
<delete id="deleteById" parameterType="long">
DELETE FROM student WHERE id = #{id}
</delete>
</mapper>
小明:明白了,那业务逻辑层是怎么写的呢?
李老师:业务逻辑层一般会调用DAO层的方法,并且做一些校验或处理。例如,这是一个StudentService类的示例:
@Service
public class StudentService {
@Autowired
private StudentMapper studentMapper;
public List
return studentMapper.selectAll();
}
public Student getStudentById(Long id) {
return studentMapper.selectById(id);
}
public void addStudent(Student student) {
if (student.getName() == null || student.getName().isEmpty()) {
throw new IllegalArgumentException("姓名不能为空");
}
studentMapper.insert(student);
}
public void updateStudent(Student student) {
if (student.getId() == null || student.getId() <= 0) {
throw new IllegalArgumentException("ID无效");
}
studentMapper.update(student);
}
public void deleteStudent(Long id) {
if (id <= 0) {
throw new IllegalArgumentException("ID无效");
}
studentMapper.deleteById(id);
}
}
小明:那控制器层又是怎么写的呢?
李老师:控制器层负责接收HTTP请求,并调用服务层处理。例如,这是一个StudentController类的示例:
@RestController
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/")
public List
return studentService.getAllStudents();
}
@GetMapping("/{id}")
public Student getStudent(@PathVariable Long id) {
return studentService.getStudentById(id);
}
@PostMapping("/")
public void createStudent(@RequestBody Student student) {
studentService.addStudent(student);
}
@PutMapping("/{id}")
public void updateStudent(@PathVariable Long id, @RequestBody Student student) {
student.setId(id);
studentService.updateStudent(student);
}
@DeleteMapping("/{id}")
public void deleteStudent(@PathVariable Long id) {
studentService.deleteStudent(id);
}
}
小明:太好了,我现在对这个系统有了更深入的理解。那权限控制方面是怎么做的呢?
李老师:我们使用了Spring Security来处理用户权限。比如,我们为不同的角色设置了不同的访问权限。下面是一个简单的Security配置类:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/admin/**").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
}
小明:那用户登录是怎么实现的呢?
李老师:我们使用了JWT(JSON Web Token)来进行身份验证。当用户登录成功后,服务器会生成一个JWT令牌返回给客户端,后续请求中需要携带该令牌。
小明:那JWT是怎么生成和验证的呢?
李老师:我们使用了jjwt库来处理JWT的生成和解析。下面是一个生成JWT的示例:
public String generateToken(User user) {
return Jwts.builder()
.setSubject(user.getUsername())
.claim("roles", user.getRoles())
.setIssuedAt(new Date())
.setExpiration(new Date(System.currentTimeMillis() + 3600000)) // 1小时
.signWith(SignatureAlgorithm.HS512, "secretKey")
.compact();
}
小明:那验证令牌的代码呢?
李老师:下面是一个验证JWT的工具类:
public static boolean validateToken(String token, String secretKey) {
try {
Jws
return true;
} catch (JwtException e) {
return false;
}
}

小明:谢谢您,李老师,今天收获很大!
李老师:不客气,希望你也能尝试做一个类似的项目。如果有问题随时问我。