当前位置: 首页 > news >正文

Spring-Mybatis 2.0

前言:

第一点:过于依赖代码生成器或AI,导致基于mybaits的CRUD通通忘了,所以为了找回遗忘的记忆,有了该系列内容。

第二点:通过实践而发现真理,又通过实践而证实真理和发展真理。从感性认识而能动地发展到理性认识,又从理性认识而能动地指导革命实践,改造主观世界和客观世界。实践、认识、再实践、再认识,这种形式,循环往复以至无穷,而实践和认识之每一循环的内容,都比较地进到了高一级的程度。

正片:

基于springBoot——maven项目

第一步:安装依赖

        第一个依赖:对应数据库驱动——Mysql-Driver

        第二个依赖:JDBC框架——本系列采用Mybaits

pop.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.4.1</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>org.example</groupId><artifactId>mybatis</artifactId><version>0.0.1-SNAPSHOT</version><name>mybatis</name><description>mybatis</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.4</version></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter-test</artifactId><version>3.0.4</version><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

第二步:启动项目

理论结果——产生报错

实践结果——产生报错即为成功

第三步:添加数据库配置

第一步:在Resource文件下创建启动配置文件——application.yaml

第二步:根据自身内容添加数据库
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driver #数据库驱动路径url: jdbc:mysql://localhost:3306/steel #数据库表路径username: root    #数据库登录账号password: 123456  #数据库登录密码server:port: 8084     #springBoot内置服务器自定义端口
第三步:运行

理论结果:无报错

实践验证:无报错

第四步:这是作者曾经常用的CRUD模板

第一步:添加实体类——对应数据库表

实体类:

为了更好使用,添加lombok框架——在pop.xml文件中的<dependencies></dependencies>标签内添加以下代码

        <dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>annotationProcessor</scope></dependency>
@Data
public class entity {int id;String username;String password;
}

注意:实体类中的变量名需要严格对应数据库表名!

实践认知:会尝试不必要的报错(可尝试)

第二步:添加Mapper(注解式写法)

第三步:增加XML文件 OR添加对应注解

@Mapper
public interface UserMapper {/*查询ID返回全部内容*/@Select("SELECT id,username,password form user_test where id =#{id}")List<entity> userByAll(int id);/*单独查询*/@Select("SELECT id,username,password form user_test where id =#{id}")entity userById(int id);
}

到了第四步,剩下的我们可以不做了,因为从第四步开始就是处理数据了

现在可以直接调用方法打印数据

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'form steel.user_test' at line 1

报错了,是最基础的SQL报错!

问题找到了,如果你不经常写from,这个会因为form写起来顺手,写错。。。。。

@Mapper
public interface UserMapper {/*查询ID返回全部内容*/@Select("SELECT id,username,password from user_test where id =#{id}")List<entity> userByAll(Long[] id);/*单独查询*/@Select("SELECT id,username,password from user_test where id =#{id}")entity userById(int id);
}

http://www.lryc.cn/news/512700.html

相关文章:

  • Linux 的历史与发展:从诞生到未来
  • SQL Server实现将分组的其他字段数据拼接成一条数据
  • 学习笔记 --C#基础其他知识点(同步和异步)
  • 一维、线性卡尔曼滤波的例程(MATLAB)
  • 极品飞车6的游戏手柄设置
  • FreeRTOS Lwip Socket APi TCP Server 1对多
  • 逆袭之路(11)——python网络爬虫:原理、应用、风险与应对策略
  • KOI技术-事件驱动编程(Sping后端)
  • LVS 负载均衡原理 | 配置示例
  • Hive分区再分桶表
  • 从 Coding (Jenkinsfile) 到 Docker:全流程自动化部署 Spring Boot 实战指南(简化篇)
  • Linux官文转载-- Linux 内核代码风格
  • Qt监控系统放大招/历经十几年迭代完善/多屏幕辅屏预览/多层级设备树/网络登录和回放
  • 【贪心算法】贪心算法七
  • LangChain教程 - 表达式语言 (LCEL) -构建智能链
  • 使用Locust对Redis进行负载测试
  • HIVE数据仓库分层
  • 数据结构与算法之动态规划: LeetCode 2407. 最长递增子序列 II (Ts版)
  • 电子电气架构 --- 什么是自动驾驶技术中的域控制单元(DCU)?
  • html5css3
  • FPGA多路红外相机视频拼接输出,提供2套工程源码和技术支持
  • python实战(十二)——如何进行新词发现?
  • 动手做计算机网络仿真实验入门学习
  • 完整的 FFmpeg 命令使用教程
  • Leetcode 3405. Count the Number of Arrays with K Matching Adjacent Elements
  • Springboot(五十六)SpringBoot3集成SkyWalking
  • 有没有免费提取音频的软件?音频编辑软件介绍!
  • Linux 中查看内存使用情况全攻略
  • 【SQL Server】教材数据库(3)
  • 使用 ECharts 与 Vue 构建数据可视化组件