Spring-Mybatis 2.0

发布于:2025-02-11 ⋅ 阅读:(77) ⋅ 点赞:(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);
}


网站公告

今日签到

点亮在社区的每一天
去签到