当前位置:   article > 正文

SpringBoot+Mysql+Mybatis实现增删改查_mybatis、mysql-connector-java 实现mysql数据库的增删改查示例

mybatis、mysql-connector-java 实现mysql数据库的增删改查示例

环境搭建

  1. 准备数据库
  2. 创建springBoot工程(引入依赖(web、mybatis、mysql、lombok)
  3. 配置yml,引入mybatis的配置信息
  4. 三层架构(controller、service、mapper)
  5. controller加注解,service在实现类加service注解,mapper加注解(mapper接口)

项目目录

首先实现一个表的增加删除查询操作:

数据库中bm表(部门)

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.7.14</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>demomvc</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>demomvc</name>
  15. <description>demomvc</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <!-- thymeleaf依赖-->
  25. <dependency>
  26. <groupId>org.thymeleaf</groupId>
  27. <artifactId>thymeleaf-spring5</artifactId>
  28. </dependency>
  29. <!-- mysql依赖-->
  30. <dependency>
  31. <groupId>mysql</groupId>
  32. <artifactId>mysql-connector-java</artifactId>
  33. <version>5.1.43</version>
  34. </dependency>
  35. <!-- jdbc依赖-->
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-jdbc</artifactId>
  39. </dependency>
  40. <!-- mybatis依赖-->
  41. <dependency>
  42. <groupId>org.mybatis.spring.boot</groupId>
  43. <artifactId>mybatis-spring-boot-starter</artifactId>
  44. <version>2.1.0</version>
  45. </dependency>
  46. <dependency>
  47. <groupId>org.springframework.boot</groupId>
  48. <artifactId>spring-boot-devtools</artifactId>
  49. <scope>runtime</scope>
  50. <optional>true</optional>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.projectlombok</groupId>
  54. <artifactId>lombok</artifactId>
  55. <optional>true</optional>
  56. </dependency>
  57. <dependency>
  58. <groupId>org.springframework.boot</groupId>
  59. <artifactId>spring-boot-starter-test</artifactId>
  60. <scope>test</scope>
  61. </dependency>
  62. <dependency>
  63. <groupId>log4j</groupId>
  64. <artifactId>log4j</artifactId>
  65. <version>1.2.17</version>
  66. </dependency>
  67. </dependencies>
  68. <build>
  69. <plugins>
  70. <plugin>
  71. <groupId>org.springframework.boot</groupId>
  72. <artifactId>spring-boot-maven-plugin</artifactId>
  73. <configuration>
  74. <excludes>
  75. <exclude>
  76. <groupId>org.projectlombok</groupId>
  77. <artifactId>lombok</artifactId>
  78. </exclude>
  79. </excludes>
  80. </configuration>
  81. </plugin>
  82. </plugins>
  83. </build>
  84. </project>

 yml文件配置

  1. spring:
  2. server:
  3. port:9000
  4. thymeleaf:
  5. mode: HTML
  6. cache: false
  7. datasource:
  8. driver-class-name: com.mysql.jdbc.Driver
  9. username: root
  10. password: 123456
  11. url: jdbc:mysql://localhost:3306/boom?useUnicode=true&characterEncoding=utf-8&useSSL=false
  12. mybatis:
  13. mapper-locations: classpath:mapping/*.xml
  14. type-aliases-package: com.example.demo.pojo
  15. configuration:
  16. map-underscore-to-camel-case: true
  17. logging:
  18. level:
  19. com:
  20. example:
  21. demomvc:
  22. mapper : debug

Result统一响应

  1. package com.example.demomvc.pojo;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. @Data
  6. @AllArgsConstructor
  7. @NoArgsConstructor
  8. public class Result {
  9. private int code;
  10. private String msg;
  11. private Object data;
  12. public static Result success(Object data){
  13. return new Result(1,"success",data);
  14. }
  15. public static Result success(){
  16. return new Result(1,"success",null);
  17. }
  18. public static Result error(String msg){
  19. return new Result(0,msg,null);
  20. }
  21. @Override
  22. public String toString() {
  23. return "Result{"+
  24. "code="+code+
  25. ",msg="+msg+'\''+
  26. ",data="+data+
  27. "}";
  28. }
  29. }

 

Bm(pojo类

  1. package com.example.demomvc.pojo;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. import java.time.LocalDateTime;
  6. @Data
  7. @AllArgsConstructor
  8. @NoArgsConstructor
  9. public class Bm {
  10. private int bmid;
  11. private String bmname;
  12. private LocalDateTime creattime;
  13. private LocalDateTime updatetime;
  14. }

BmMapper

  1. package com.example.demomvc.mapper;
  2. import com.example.demomvc.pojo.Bm;
  3. import org.apache.ibatis.annotations.Delete;
  4. import org.apache.ibatis.annotations.Insert;
  5. import org.apache.ibatis.annotations.Mapper;
  6. import org.apache.ibatis.annotations.Select;
  7. import java.util.List;
  8. @Mapper
  9. public interface BmMapper {
  10. /*查询全部部门数据*/
  11. @Select("select * from bm")
  12. List<Bm> list();
  13. /*根据部门id删除信息*/
  14. @Delete("delete from bm where bmid=#{id}")
  15. void deleteById(int id);
  16. /*新增部门*/
  17. @Insert("insert into bm(bmid,bmname,creattime,updatetime) values(#{id},#{name},#{creattime},#{updatetime})")
  18. void setbm(Bm bm);
  19. }

BmService接口 

  1. package com.example.demomvc.service;
  2. import com.example.demomvc.pojo.Bm;
  3. import java.util.List;
  4. public interface BmService {
  5. //查询全部部门数据
  6. List<Bm> list();
  7. void delete(int id);
  8. void setbm(Bm bm);
  9. }

BmServiceImp

  1. package com.example.demomvc.service.impl;
  2. import com.example.demomvc.mapper.BmMapper;
  3. import com.example.demomvc.pojo.Bm;
  4. import com.example.demomvc.service.BmService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import java.time.LocalDateTime;
  8. import java.util.List;
  9. @Service
  10. public class BmServiceImpl implements BmService {
  11. @Autowired
  12. private BmMapper bmMapper;
  13. @Override
  14. public List<Bm> list() {
  15. return bmMapper.list();
  16. }
  17. @Override
  18. public void delete(int id) {
  19. bmMapper.deleteById(id);
  20. }
  21. @Override
  22. public void setbm(Bm bm) {
  23. bm.setCreattime(LocalDateTime.now());
  24. bm.setUpdatetime(LocalDateTime.now());
  25. bmMapper.setbm(bm);
  26. }
  27. }

 

BmController

  1. package com.example.demomvc.controller;
  2. import com.example.demomvc.pojo.Bm;
  3. import com.example.demomvc.pojo.Result;
  4. import com.example.demomvc.service.BmService;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.web.bind.annotation.*;
  9. import java.util.List;
  10. import java.util.logging.Logger;
  11. @Slf4j //日志有关log
  12. @RestController
  13. public class BmController {
  14. //private static Logger log= (Logger) LoggerFactory.getLogger(BmController.class);
  15. @Autowired
  16. private BmService bmService;
  17. @RequestMapping(value = "/bm",method = RequestMethod.GET)
  18. public Result list(){
  19. log.info("查询全部部门数据");
  20. List<Bm> bmList=bmService.list();
  21. return Result.success(bmList);
  22. }
  23. /*删除部门*/
  24. @RequestMapping("deletebm/{id}")
  25. public Result delete(@PathVariable int id){
  26. log.info("根据id删除部门:",id);
  27. //调用service,删除部门
  28. bmService.delete(id);
  29. return Result.success();
  30. }
  31. /*新增部门*/
  32. @RequestMapping("setbm")
  33. public Result setBm(@RequestBody Bm bm){
  34. log.info("新增部门:",bm);
  35. //调用service,新增部门
  36. bmService.setbm(bm);
  37. return Result.success();
  38. }
  39. }

ok~现在是访问实现!!!

实现

查询实现!

删除实现 

这里是把id是9的删了,又select了一下

 查看数据库也没有了(对比一下

结束啦~道阻且长呢 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/500552
推荐阅读
相关标签
  

闽ICP备14008679号