MyBatis Plus 学生管理系统实战:从映射文件到控制层
MyBatis Plus 学生管理系统实战:从映射文件到控制层
本文将带你一步步搭建一个基于 MyBatis Plus 的学生管理系统,涵盖映射文件、服务层、DAO 层和控制层代码示例,并提供完善的代码补充和配置说明,助你快速上手 MyBatis Plus 开发。
1. 映射文件
<?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.demo1.mapper.StudentMapper'>
<resultMap id='BaseResultMap' type='com.example.demo1.domain.Student'>
<id property='id' column='id' jdbcType='INTEGER'/>
<result property='name' column='name' jdbcType='VARCHAR'/>
<result property='no' column='no' jdbcType='VARCHAR'/>
<result property='gender' column='gender' jdbcType='VARCHAR'/>
<result property='classId' column='class_id' jdbcType='INTEGER'/>
</resultMap>
<sql id='Base_Column_List'>
id,name,no,
gender,class_id
</sql>
</mapper>
2. 服务层
public interface StudentService extends IService<Student> {
}
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
implements StudentService {
@Autowired
private StudentMapper studentMapper;
//
// @Override
// public Student getById(Serializable id) {
// return studentMapper.selectById(id);
// }
@Override
public Student getById(Serializable id) {
return super.getById(id);
}
}
3. DAO 层
@Mapper
public interface StudentMapper extends BaseMapper<Student> {
}
@TableName(value ='tb_student')
public class Student implements Serializable {
/**
*
*/
@TableId
private Integer id;
/**
*
*/
private String name;
/**
*
*/
private String no;
/**
*
*/
private String gender;
/**
*
*/
private Integer classId;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
/**
*
*/
public Integer getId() {
return id;
}
/**
*
*/
public void setId(Integer id) {
this.id = id;
}
/**
*
*/
public String getName() {
return name;
}
/**
*
*/
public void setName(String name) {
this.name = name;
}
/**
*
*/
public String getNo() {
return no;
}
/**
*
*/
public void setNo(String no) {
this.no = no;
}
/**
*
*/
public String getGender() {
return gender;
}
/**
*
*/
public void setGender(String gender) {
this.gender = gender;
}
/**
*
*/
public Integer getClassId() {
return classId;
}
/**
*
*/
public void setClassId(Integer classId) {
this.classId = classId;
}
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
Student other = (Student) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName()))
&& (this.getNo() == null ? other.getNo() == null : this.getNo().equals(other.getNo()))
&& (this.getGender() == null ? other.getGender() == null : this.getGender().equals(other.getGender()))
&& (this.getClassId() == null ? other.getClassId() == null : this.getClassId().equals(other.getClassId()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
result = prime * result + ((getNo() == null) ? 0 : getNo().hashCode());
result = prime * result + ((getGender() == null) ? 0 : getGender().hashCode());
result = prime * result + ((getClassId() == null) ? 0 : getClassId().hashCode());
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append('[');
sb.append('Hash = ').append(hashCode());
sb.append(',').append(' id=').append(id);
sb.append(',').append(' name=').append(name);
sb.append(',').append(' no=').append(no);
sb.append(',').append(' gender=').append(gender);
sb.append(',').append(' classId=').append(classId);
sb.append(',').append(' serialVersionUID=').append(serialVersionUID);
sb.append(']');
return sb.toString();
}
}
4. 控制层
@RestController
@RequestMapping('/students')
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping('/{id}')
public Student getById(@PathVariable('id') Integer id) {
return studentService.getById(id);
}
@PostMapping('/')
public boolean save(@RequestBody Student student) {
return studentService.save(student);
}
@PutMapping('/')
public boolean update(@RequestBody Student student) {
return studentService.updateById(student);
}
@DeleteMapping('/{id}')
public boolean delete(@PathVariable('id') Integer id) {
return studentService.removeById(id);
}
}
5. 代码补充和配置说明
- 导入依赖: 在项目的
pom.xml文件中添加 MyBatis Plus 的依赖。 - 配置数据源: 在配置文件中配置数据库连接信息,例如:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/your_database_name?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: your_username
password: your_password
- 配置 MyBatis Plus: 在配置文件中配置 MyBatis Plus 的相关配置,例如:
mybatis-plus:
global-config:
db-config:
id-type: auto
mapper-locations: classpath:mapper/**/*.xml
type-aliases-package: com.example.demo1.domain
注意: 以上代码仅供参考,实际使用时需要根据你的项目情况进行调整。
6. 总结
本文详细介绍了如何使用 MyBatis Plus 搭建一个简单的学生管理系统,并提供了完善的代码示例和配置说明,希望能够帮助你快速上手 MyBatis Plus 开发。
原文地址: https://www.cveoy.top/t/topic/lCfw 著作权归作者所有。请勿转载和采集!