• springboot整合mybatis实现增删改查(xml)--项目阶段1


    目录

    一、前言

    二、创建项目

    创建MySQL数据库和表

    创建springboot项目

    本文总体代码结构图预览

    三、编写代码

     (一)新建实体层属性类

    (二)新建数据层mapper接口

    (三)新建mapper的映射SQL(xml)

    (四)新建服务层接口

    (五)新建服务层的实现类

     (六)控制层类

    (七)配置文件加入扫描xml文件

    四、运行代码

     查询全部

    根据ID查询 

    根据ID删除

     新增

    修改

    五、代码获取


    一、前言

    在之前使用了mybatis-plus做项目,感觉不是很方便,特别是涉及到多表查询,虽然MP不用写SQL语句,但是缺点就是不够灵活。做做一些小demo就差不多了。 真正项目里面使用最多的还是mybatis,根据自己的需要写SQL语句。中小型项目sql语句用注解可能会方便一点,大型的项目用xml会更好一些,所以我这里直接演示用的是xml形式

    二、创建项目

    创建MySQL数据库和表

    创建MySQL数据库和创建表的详细步骤(navicat)_mysql navicat新建数据库_云边的快乐猫的博客-CSDN博客

    创建springboot项目

    使用这个文章里面的方式二创建springboot项目

    IDEA创建SpringBoot项目的两个方式详细步骤(2023)_云边的快乐猫的博客-CSDN博客

    源码链接:https://pan.baidu.com/s/1TA9QvOG8rRzen6CROvpIeQ?pwd=jiu1

    本文总体代码结构图预览

    👆按照上面的方式创建springboot项目。这里面的东西都会有的

    总体配置文件(仅供参考,可跳过)

    1. server:
    2. port: 80
    3. spring:
    4. datasource:
    5. type: com.alibaba.druid.pool.DruidDataSource
    6. driver-class-name: com.mysql.cj.jdbc.Driver
    7. url: jdbc:mysql://localhost:3306/dndata?serverTimezone=GMT%2b8
    8. username: root
    9. password: 123456
    10. mybatis:
    11. mapper-locations: classpath:mapping/*.xml
    12. type-aliases-package: com.example.jiu.entity

    pom.xml依赖文件(仅供参考,可跳过)

    1. "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.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.7.15version>
    9. <relativePath/>
    10. parent>
    11. <groupId>com.examplegroupId>
    12. <artifactId>JiuartifactId>
    13. <version>0.0.1-SNAPSHOTversion>
    14. <name>Jiuname>
    15. <description>Jiudescription>
    16. <properties>
    17. <java.version>11java.version>
    18. properties>
    19. <dependencies>
    20. <dependency>
    21. <groupId>org.springframework.bootgroupId>
    22. <artifactId>spring-boot-starter-webartifactId>
    23. dependency>
    24. <dependency>
    25. <groupId>org.mybatis.spring.bootgroupId>
    26. <artifactId>mybatis-spring-boot-starterartifactId>
    27. <version>2.3.1version>
    28. dependency>
    29. <dependency>
    30. <groupId>org.springframework.bootgroupId>
    31. <artifactId>spring-boot-devtoolsartifactId>
    32. <scope>runtimescope>
    33. <optional>trueoptional>
    34. dependency>
    35. <dependency>
    36. <groupId>com.mysqlgroupId>
    37. <artifactId>mysql-connector-jartifactId>
    38. <scope>runtimescope>
    39. dependency>
    40. <dependency>
    41. <groupId>org.projectlombokgroupId>
    42. <artifactId>lombokartifactId>
    43. <optional>trueoptional>
    44. dependency>
    45. <dependency>
    46. <groupId>org.springframework.bootgroupId>
    47. <artifactId>spring-boot-starter-testartifactId>
    48. <scope>testscope>
    49. dependency>
    50. <dependency>
    51. <groupId>org.mybatis.spring.bootgroupId>
    52. <artifactId>mybatis-spring-boot-starter-testartifactId>
    53. <version>2.3.1version>
    54. <scope>testscope>
    55. dependency>
    56. <dependency>
    57. <groupId>com.alibabagroupId>
    58. <artifactId>druid-spring-boot-starterartifactId>
    59. <version>1.2.16version>
    60. dependency>
    61. dependencies>
    62. <build>
    63. <plugins>
    64. <plugin>
    65. <groupId>org.springframework.bootgroupId>
    66. <artifactId>spring-boot-maven-pluginartifactId>
    67. <configuration>
    68. <excludes>
    69. <exclude>
    70. <groupId>org.projectlombokgroupId>
    71. <artifactId>lombokartifactId>
    72. exclude>
    73. excludes>
    74. configuration>
    75. plugin>
    76. plugins>
    77. build>
    78. project>

    三、编写代码

     (一)新建实体层属性类

    1.新建一个entity包,里面再创建一个和数据库表名对应的类(驼峰命名)

    类名:User

    这个类是和数据库表里面的字段对应上的,用Date注解里面包含了实体属性常用的get、set构造函数这些,具体可以去自行了解一下。 

    1. package com.example.jiu.entity;
    2. import lombok.Data;
    3. //使用@Data注解
    4. @Data
    5. public class User {
    6. private Integer id;
    7. private String username;
    8. private String password;
    9. }

    (二)新建数据层mapper接口

    2.新建一个mapper包,里面创建一个数据层的接口

    接口名:UserMapper

    这里面的方法都是自定义的,是最开始的数据层的方法语句,后续的命名调用都根据这里的来

    1. package com.example.jiu.mapper;
    2. import com.example.jiu.entity.User;
    3. import org.apache.ibatis.annotations.Mapper;
    4. import java.util.List;
    5. @Mapper
    6. public interface UserMapper {
    7. //自定义查询全部的方法
    8. List findAll();
    9. //根据id查询
    10. User findById(Integer id);
    11. //自定义根据删除的方法
    12. int deleteUserById(Integer id);
    13. //自定义增加的方法
    14. int addUser(User user);
    15. //自定义修改的方法
    16. int updateUser(User user);
    17. }

    (三)新建mapper的映射SQL(xml)

    3.resources目录下新建一个mapping包,里面创建一个xml文件

    这个xml映射文件:UserMapper.xml

    mapper标签里面对应的路径是mapper包里面对应映射接口的位置

    SQL语句标签里面的id对应mapper接口里面定义的方法名

    如果是查询的语句用resultType,后面跟着实体属性类的位置

    其他的增删改用parameterType,后面跟着的是代表要往这个SQL里面传入什么,一般只有删除是int类型,其他的也都是实体属性类位置

    1. "1.0" encoding="UTF-8" ?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.example.jiu.mapper.UserMapper">
    6. <select id="findAll" resultType="com.example.jiu.entity.User">
    7. select * from user;
    8. select>
    9. <select id="findById" resultType="com.example.jiu.entity.User">
    10. select * from user where id =#{id}
    11. select>
    12. <delete id="deleteUserById" parameterType="java.lang.Integer">
    13. delete from user where id = #{id}
    14. delete>
    15. <insert id="addUser" parameterType="com.example.jiu.entity.User">
    16. insert into user (username,password) values (#{username},#{password})
    17. insert>
    18. <update id="updateUser" parameterType="com.example.jiu.entity.User">
    19. update user set username = #{username},password = #{password} where id = #{id}
    20. update>
    21. mapper>

    (四)新建服务层接口

    4.新建一个service包,这里是服务层,主要就是编写逻辑代码的,拿mapper层的数据来编写逻辑代码,然后再交给控制层去调用。

     服务层接口:UserService

    这个接口里面的方法和mapper层接口里面的方法一样,这个接口存在的意义是为了降低耦合度

    1. package com.example.jiu.service;
    2. import com.example.jiu.entity.User;
    3. import java.util.List;
    4. //这里写的和mapper接口里面的一样
    5. public interface UserService {
    6. List findAll();
    7. User findById(Integer id);
    8. int deleteUserById(Integer id);
    9. int addUser(User user);
    10. int updateUser(User user);
    11. }

    (五)新建服务层的实现类

    5.在service包下再建立一个Impl包(包名首字母大写的),里面写上服务类接口的实现类

    服务层实现类名称:UserServiceImpl

    这里是实现了服务层的接口,并把mapper数据层注入到里面,里面的方法都是用快捷键去实现的,return后面跟着的全局变量.方法

    1. package com.example.jiu.service.Impl;
    2. import com.example.jiu.entity.User;
    3. import com.example.jiu.mapper.UserMapper;
    4. import com.example.jiu.service.UserService;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.stereotype.Service;
    7. import org.springframework.web.bind.annotation.RequestBody;
    8. import java.util.List;
    9. @Service
    10. public class UserServiceImpl implements UserService {
    11. //调用mapper的接口用做全局变量
    12. private final UserMapper userMapper;
    13. //代表当这个实现类被实例化就会自动找到mapper接口,并注入其中
    14. @Autowired
    15. public UserServiceImpl(UserMapper userMapper) {
    16. this.userMapper = userMapper;
    17. }
    18. //这里都是用快捷键去实现生成接口里面的方法的。不过return后面跟着的要自己写,全局变量.方法
    19. //查询全部
    20. @Override
    21. public List findAll() {
    22. return userMapper.findAll();
    23. }
    24. //根据id查询
    25. @Override
    26. public User findById(Integer id) {
    27. return userMapper.findById(id);
    28. }
    29. //根据id删除
    30. @Override
    31. public int deleteUserById(Integer id) {
    32. return userMapper.deleteUserById(id);
    33. }
    34. //增加
    35. @Override
    36. public int addUser(@RequestBody User user) {
    37. return userMapper.addUser(user);
    38. }
    39. //修改
    40. @Override
    41. public int updateUser(User user) {
    42. return userMapper.updateUser(user);
    43. }
    44. }

     (六)控制层类

    6.新建一个controller包,里面创建对应的控制类

    控制层类名:UserController

    这里面要自动注入服务层的实现类供下面的方法调用

    方法名字可以自定义

    1. package com.example.jiu.controller;
    2. import com.example.jiu.entity.User;
    3. import com.example.jiu.service.Impl.UserServiceImpl;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.web.bind.annotation.*;
    6. import java.util.List;
    7. @RestController
    8. @RequestMapping("/user")
    9. public class UserController {
    10. @Autowired
    11. private UserServiceImpl userServiceImpl;
    12. //查询全部的方法,调用服务层
    13. @GetMapping
    14. public List findAll(){
    15. return userServiceImpl.findAll();
    16. }
    17. //根据id查询
    18. @GetMapping("/{id}")
    19. public User findById(@PathVariable Integer id){
    20. return userServiceImpl.findById(id);
    21. }
    22. //根据id删除,调用服务层
    23. @DeleteMapping("/{id}")
    24. public int delete(@PathVariable Integer id){
    25. return userServiceImpl.deleteUserById(id);
    26. }
    27. //增加的方法,调用服务层
    28. @PostMapping
    29. public int add(@RequestBody User user){
    30. return userServiceImpl.addUser(user);
    31. }
    32. //修改的方法,调用服务层
    33. @PutMapping
    34. public int put(@RequestBody User user){
    35. return userServiceImpl.updateUser(user);
    36. }
    37. }

    (七)配置文件加入扫描xml文件

    7.在yml配置文件里面加入这个,代表可以把xml的文件加载扫描到

    第一个是代表扫描到xml文件的包路径里面

    第二个是代表实体类的包路径

    1. mybatis:
    2. mapper-locations: classpath:mapping/*.xml
    3. type-aliases-package: com.example.jiu.entity

    四、运行代码

    这里用的是postman测试

    postman测试后端增删改查_云边的快乐猫的博客-CSDN博客

     查询全部

    get请求。就可以直接网页上面运行 

    http://localhost:80/user
    根据ID查询 

    get请求。例如查询id为1的就这样可以查询 

    http://localhost:80/user/1
    根据ID删除

    delete请求。例如删除id为8的那行数据,删除成功返回1

    http://localhost:80/user/8

     新增

     post请求。例如添加一个数据进去,由于数据库里面的id是设置为自增的,这里就不用添加id

    http://localhost:80/user

    修改

    put请求,往里面传入数据,用json格式,修改id为9的数据,修改成功返回1 

    http://localhost:80/user

    五、代码获取

    链接:https://pan.baidu.com/s/18Cy1RluCx04_9TRi4nsiVg?pwd=jiux 
    提取码:jiux

  • 相关阅读:
    vue+ssm+Element实现登录页面的优化和员工管理
    [C语言刷题篇]链表运用讲解
    python中is与==
    【深度学习】特征融合的重要方法 | 张量的拼接 | torch.cat()函数 | torch.add(函数
    数据库锁介绍
    万字总结:分布式系统的 38 个知识点
    一本通1047;判断能否被3,5,7整除
    spring高级源码50讲-43-50(spring续)
    k8s控制器之Deployment第二弹之创建Deployment
    Axure绘制数字加减器
  • 原文地址:https://blog.csdn.net/m0_52861000/article/details/132521144