• MyBatis 的注解实现方法整合(完成数据的增删改查操作)


    Mybatis简介

    MyBatis是一个用于简化数据库访问层的开源持久层框架。它提供了将对象与数据库表之间的映射关系进行配置的方式,而不需要使用繁琐的 JDBC 代码和手动设置参数。MyBatis的目标是使数据库操作变得更加简单,同时保持足够的灵活性和性能。

    一.创建数据库

    假设有一个名为 User 的表,包含 id、username 和 email 字段。

    二.导入相关jar包

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.2</version>
    </dependency>
     
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.47</version>
    </dependency>
    #如果是8版本的MySQL使用下面这个
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.22</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    三.创建mybatis的配置文件

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8"/>
                    <property name="username" value="MySQL的用户名"/>
                    <property name="password" value="MySQL的密码"/>
                </dataSource>
            </environment>
        </environments>
     
        <mappers>
            <mapper resource="mapper/UserDaoMapper.xml"/>
        </mappers>
     
    </configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    四.创建对应的 Java 实体类

    public class User {
        private Long id;
        private String username;
        private String email;
    
        // 省略构造函数和 getter/setter 方法
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    五.MyBatis 注解配置

    1插入数据操作

    import org.apache.ibatis.annotations.Insert;
    
    public interface UserMapper {
    
        @Insert("INSERT INTO user (username, email) VALUES (#{username}, #{email})")
        void insertUser(User user);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2查询数据操作

    import org.apache.ibatis.annotations.Select;
    
    public interface UserMapper {
    
        @Select("SELECT id, username, email FROM user WHERE id = #{id}")
        User getUserById(Long id);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3更新数据操作

    import org.apache.ibatis.annotations.Update;
    
    public interface UserMapper {
    
        @Update("UPDATE user SET username = #{username}, email = #{email} WHERE id = #{id}")
        void updateUser(User user);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4删除数据操作

    import org.apache.ibatis.annotations.Delete;
    
    public interface UserMapper {
    
        @Delete("DELETE FROM user WHERE id = #{id}")
        void deleteUserById(Long id);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    六.使用 Mapper 接口进行操作

    public class MyBatisApp {
    
        public static void main(String[] args) {
            // 获取 MyBatis 的 SqlSessionFactory
    
            // 获取 Mapper 接口的实例
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    
            // 插入数据
            User newUser = new User("John", "john@example.com");
            userMapper.insertUser(newUser);
    
            // 查询数据
            User retrievedUser = userMapper.getUserById(newUser.getId());
            System.out.println("Retrieved User: " + retrievedUser);
    
            // 更新数据
            retrievedUser.setUsername("Johnny");
            userMapper.updateUser(retrievedUser);
    
            // 删除数据
            userMapper.deleteUserById(retrievedUser.getId());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    代码随想录算法训练营 单调栈part01
    论文精读(保姆级解析)——DiFaReli: Diffusion Face Relighting
    22、接口与抽象类、匿名类的介绍
    SpringBoot实现代码生成器——基于SpringBoot和Vue的后台管理系统项目系列博客(十)
    使用RNN像进行写作
    spring 单元测试注解
    【Docker】部署环境
    4K壁纸小程序源码 全内容自动采集
    基于飞机配电优化负荷管理系统研究(Matlab代码实现)
    BSN专网+医疗保险:MediConCen与BSN达成战略合作,提升理赔效率
  • 原文地址:https://blog.csdn.net/m0_66714418/article/details/134424460