SpringBBoot整合MyBatis
2024-09-01 21:41:50
一、目录展示
二、导入依赖
三、配置文件application.yml
四、Student实体类
package com.zn.entity;
public class Student {
private Integer stu_id;
private String stu_name; @Override
public String toString() {
return "Student{" +
"stu_id=" + stu_id +
", stu_name='" + stu_name + '\'' +
'}';
} public Student() {
} public Student(String stu_name) {
this.stu_name = stu_name;
} public Student(Integer stu_id, String stu_name) {
this.stu_id = stu_id;
this.stu_name = stu_name;
} public Integer getStu_id() {
return stu_id;
} public void setStu_id(Integer stu_id) {
this.stu_id = stu_id;
} public String getStu_name() {
return stu_name;
} public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
}
五、StudentDao层
package com.zn.dao; import com.zn.entity.Student;
import org.springframework.stereotype.Repository; import java.util.List; @Repository
public interface StudentDao { //添加数据
int insertStudent(Student student); //修改数据
int updateStudent(Student student); //删除数据
int deleteStudent(Integer id); //查询数据
List<Student> findAll();
}
六、resources下的mapper中的StudentDao.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.zn.dao.StudentDao" >
<!--添加数据-->
<insert id="insertStudent">
insert into studentinfo(stu_name) values(#{stu_name})
</insert> <!--修改数据-->
<update id="updateStudent">
update studentinfo set stu_name=#{stu_name} where stu_id=#{stu_id}
</update> <!--删除数据-->
<delete id="deleteStudent">
delete from studentinfo where stu_id=#{stu_id}
</delete> <!--查询数据-->
<select id="findAll" resultType="com.zn.entity.Student">
select * from studentinfo
</select>
</mapper>
七、StudentService层
package com.zn.service; import com.zn.dao.StudentDao;
import com.zn.entity.Student;
import org.springframework.stereotype.Service; import javax.annotation.Resource;
import java.util.List; @Service
public class StudentService { @Resource
StudentDao studentDao; //增加数据
public int insertStudent(Student student) {
return studentDao.insertStudent(student);
} //修改数据
public int updateStudent(Student student) {
return studentDao.updateStudent(student);
} //删除数据
public int deleteStudent(Integer id) {
return studentDao.deleteStudent(id);
} //查询数据
public List<Student> findAll(){
return studentDao.findAll();
}
}
八、StudentController层
package com.zn.controller; import com.zn.entity.Student;
import com.zn.service.StudentService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource;
import java.util.List; @RestController
public class StudentController { @Resource
StudentService studentService; //添加数据
@RequestMapping("/insertStudent")
public int insertStudent(){
return studentService.insertStudent(new Student("刘姐"));
} //修改数据
@RequestMapping("/updateStudent")
public int updateStudent(){
return studentService.updateStudent(new Student(5,"小傻子"));
} //删除数据
@RequestMapping("/deleteStudent")
public int deleteStudent(){
return studentService.deleteStudent(4);
} //查询数据
@RequestMapping("/findAll")
public List<Student> findAll(){
return studentService.findAll();
}
}
九、测试类
十、效果展示
省略增删改效果
(1)查询数据
最新文章
- BZOJ1367 [Baltic2004]sequence
- BZOJ 1051 受欢迎的牛(Tarjan缩点)
- java对称加密(AES)
- Deferred Shading(延迟渲染)
- hdoj 2502 月之数
- .NET基础拾遗(7)多线程开发基础1
- Halloween party
- poj 3177
- Android常用工具类封装---SharedPreferencesUtil
- 开源 免费 java CMS - FreeCMS1.4-功能说明-站点管理
- Windows 查看端口占用
- 1、Flutter_初体验_创建第一个应用_AndroidStudio_windows
- express 遇到问题 - Error: Can&#39;t set headers after they are sent
- HDFS的Java客户端编写
- Java Integer常量池——IntegerCache内部类
- Hibernate的多表关系
- redis数据操作笔记
- Spring boot 远程调试
- oracle-闪回技术2
- 0_Simple__simpleVoteIntrinsics + 0_Simple__simpleVoteIntrinsics_nvrtc