SpringBoot利用Mybatis实现数据库信息的crud
Posted zhaochunhui
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot利用Mybatis实现数据库信息的crud相关的知识,希望对你有一定的参考价值。
整个项目是使用Mybatis操作的数据库(Mybatis在前面也有总结使用的方法:https://www.cnblogs.com/zhaochunhui/p/11372793.html)
项目代码上传github:https://github.com/H-Designer/SpringBoot-Mybatis-CRUD
项目在创建的时候,要选好我们需要的依赖的模块,web,mysql,jdbc,mybatis
数据库连接的是虚拟机里面的,更换一下地址的ip就行
#数据库信息的相关配置 spring: datasource: url: jdbc:mysql://192.168.43.197:3306/cache username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver #驼峰命名法的启用 mybatis: configuration: map-underscore-to-camel-case: true
数据库的连接信息根绝实际的情况进行填写就行
项目实现的是员工信息的crud,创建一个employee的实体类
package com.example.cache.bean; public class Employee private Integer id; private String lastName; private String email; private Integer gender; //性别 1男 0女 private Integer dId; public Employee() super(); public Employee(Integer id, String lastName, String email, Integer gender, Integer dId) super(); this.id = id; this.lastName = lastName; this.email = email; this.gender = gender; this.dId = dId; public Integer getId() return id; public void setId(Integer id) this.id = id; public String getLastName() return lastName; public void setLastName(String lastName) this.lastName = lastName; public String getEmail() return email; public void setEmail(String email) this.email = email; public Integer getGender() return gender; public void setGender(Integer gender) this.gender = gender; public Integer getdId() return dId; public void setdId(Integer dId) this.dId = dId; @Override public String toString() return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + ", dId=" + dId + "]";
用Mybatis操作数据库需要制定Mapper文件。有两种方式,一种是在mapper类文件上加上mapper的注解:@Mappaer
另一种是在启动函数或者是在配置文件中加上:@MapperScan("com.example.cache.mapper")具体的文件的路径根据实际情况修改
package com.example.cache.mapper; import com.example.cache.bean.Employee; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; public interface EmployeeMapper @Select("select * from employee where id=#id") public Employee getEmpById(Integer id); @Update("update employee set lastName=#lastName,email=#email,gender=#gender,d_id=#dId where id=#id") public void updateEmp(Employee employee); @Delete("delete from employee where id=#id") public void deleteEmp(Integer id); @Insert("insert into employee(lastName,email,gender,d_id) values(#lastName,#email,#gender,#dId)") public void insertEmp(Employee employee);
这些就是对数据库信息的修改的形式,增删改查的底层的实现
然后就是service的编写,实现具体的增删改查的类的编写:
package com.example.cache.service; import com.example.cache.bean.Employee; import com.example.cache.mapper.EmployeeMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class EmployeeService @Autowired EmployeeMapper employeeMapper; //查询员工信息 public Employee getEmp(Integer id) System.out.println("查询"+id+"号员工信息"); Employee emp = employeeMapper.getEmpById(id); return emp; //删除员工信息 public void deleteEmp(Integer id) System.out.println("删除"+id+"号员工信息"); employeeMapper.deleteEmp(id); //增加员工信息 public void insertEmp(Employee employee) System.out.println("增加新的员工信息"); employeeMapper.insertEmp(employee); //修改员工信息 public void updateEmp(Employee employee) System.out.println("修改"+employee.getId()+"号员工信息"); employeeMapper.updateEmp(employee);
具体的方法实现以后,就是在发送请求的时候,解析请求,实现具体的方法的调用:这就是Controller的内容了;
package com.example.cache.controller; import com.example.cache.bean.Employee; import com.example.cache.service.EmployeeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class EmpController @Autowired EmployeeService employeeService; //查询员工信息 @GetMapping("/getEmp/id") public Employee getEmployee(@PathVariable("id") Integer id) Employee emp = employeeService.getEmp(id); return emp; //删除员工信息 @GetMapping("/deleteEmp/id") public void deleteEmployee(@PathVariable("id") Integer id) employeeService.deleteEmp(id); //增加员工信息 @GetMapping("/insertEmp") //这里,在传参的时候,只需要将床底的参数的名称和Bean里面的参数的名称一致即可,employee就可以接收到所有的参数信息 public void insertEmployee(Employee employee) System.out.println(employee); employeeService.insertEmp(employee); //修改员工信息 @GetMapping("/updateEmp/id") public void updateEmployee(Employee employee,@PathVariable("id") Integer id) System.out.println(employee); employeeService.updateEmp(employee);
这里需要值得一提的是,在我们的insert和update操作的时候,需要将员工的全部信息传递过去,这时候 不需要每一个参数进行一一的接收,我们只需要在我们传递参数的时候,将变量名和bean里面的变量名保持一致,这样就可以以实体类的方式,将全部的参数信息接收到。
以上是关于SpringBoot利用Mybatis实现数据库信息的crud的主要内容,如果未能解决你的问题,请参考以下文章
基于SpringBoot+SpringSecurity+mybatis+layui实现的一款权限系统
springboot+mybatis+vue+iviewui实现前后端分离的小Demo
#yyds干货盘点#SpringBoot + MyBatis + MySQL 实现读写分离!
SpringBoot整合ORM开发框架MyBatis和JPA