MyBatis Plus 学生信息管理:代码示例及配置说明
<?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>
这是mybatis plus 映射文件
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);
}
}
这是服务层
@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(", id=").append(id);
sb.append(", name=").append(name);
sb.append(", no=").append(no);
sb.append(", gender=").append(gender);
sb.append(", classId=").append(classId);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}
这是dao 层
控制层代码怎么写?还有这个代码有什么地方要补充才能运行吗? 控制层代码可以参考如下:
@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);
}
}
其中,通过@Autowired注解注入了StudentService,然后提供了getById、save、update和delete等接口,分别对应了查询、新增、更新和删除操作。
此外,需要在配置文件中配置数据源和mybatis-plus的相关配置,才能让代码正常运行。具体配置可以参考mybatis-plus官方文档。 需要哪些相关配置内容:
为了让代码能够正常运行,需要进行以下相关配置:
-
配置数据源:在配置文件中配置数据源,包括数据源类型、url、用户名、密码等信息。
-
配置Mybatis-Plus:在配置文件中配置Mybatis-Plus的相关配置,包括Mybatis的mapper文件路径、实体类别名等信息。
-
配置扫描包:在配置文件中配置扫描包的路径,让Spring容器能够扫描到相应的类。
-
配置日志:在配置文件中配置日志的级别和输出方式,方便查看和排查问题。
以上是一些基本的配置,具体配置根据实际情况而定,可以参考Mybatis-Plus官方文档进行配置。
原文地址: https://www.cveoy.top/t/topic/lCfD 著作权归作者所有。请勿转载和采集!