使用MyBatis和MyBatis Plus实现自动生成主键的插入和查询操作

在这篇文章中,我们将学习如何使用MyBatis和MyBatis Plus实现插入数据时自动生成主键,以及如何进行简单的查询操作。如果你是Java开发者,并且正在使用MyBatis和MyBatis Plus进行数据库操作,这篇文章将非常适合你。

环境准备

  1. 创建数据库表 首先,我们需要在数据库中创建一个表。在这个示例中,我们使用MySQL数据库,并创建一个名为lianxi的表,其中id字段是自动增长的主键。

    CREATE TABLE lianxi ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, phone VARCHAR(255) NOT NULL );

  2. 配置MyBatis和MyBatis Plus 确保你的项目中已经配置好了MyBatis和MyBatis Plus。如果还没有配置,可以参考以下配置方法:

    • 引入依赖pom.xml中添加MyBatis和MyBatis Plus的依赖:

       
      
                                                                                    
                                                                                    
              <dependencies> <!-- MyBatis --> <dependency> 
              <groupId>org.mybatis.spring.boot</groupId>                              
             <artifactId>mybatis-spring-boot-starter</artifactId>                  
             <version>2.2.0</version>                                                       
            </dependency> <!-- MyBatis Plus --> <dependency>       
            <groupId>com.baomidou</groupId>                                   
            <artifactId>mybatis-plus-boot-starter</artifactId>          <version>3.4.3.4</version>                                                     
          </dependency> <!-- MySQL Connector --> <dependency>                   <groupId>mysql</groupId>                                                  <artifactId>mysql-connector-java</artifactId>                      
             <version>8.0.23</version> </dependency> </dependencies>

    • 配置数据源和MyBatis Plusapplication.ymlapplication.properties中配置数据源和MyBatis Plus:

         
        spring: datasource: url: jdbc:mysql://localhost:3306/your_database? 
         useSSL=false&serverTimezone=UTC username: your_username password: your_password driver- 
        class-name: com.mysql.cj.jdbc.Driver mybatis-plus: mapper-locations: 
        classpath:/mapper/*.xml type-aliases-package: com.example.demo.entity

实现自动生成主键的插入操作

  1. 创建实体类

     
     public class Lianxi { private Integer id; private String name; private String password; 
     private String phone; // Getters and Setters }

  2. 创建Mapper接口

     
    public interface LianxiMapper extends BaseMapper<Lianxi> { }

  3. 编写Mapper XML文件

    resources/mapper目录下创建LianxiMapper.xml文件:

     
     <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
     "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper 
     namespace="com.example.demo.mapper.LianxiMapper"> <insert id="addAll" 
     useGeneratedKeys="true" keyProperty="id"> INSERT INTO lianxi (name, password, phone) 
     VALUES (#{name}, #{password}, #{phone}) </insert> </mapper>

  4. 在Service层调用插入操作

     
     @Service public class LianxiService { @Autowired private LianxiMapper lianxiMapper; public 
     void addLianxi(Lianxi lianxi) { lianxiMapper.insert(lianxi); } }

  5. 在Controller层调用Service

     
     @RestController @RequestMapping("/lianxi") public class LianxiController { @Autowired 
     private LianxiService lianxiService; @PostMapping("/add") public ResponseEntity<String> 
     addLianxi(@RequestBody Lianxi lianxi) { lianxiService.addLianxi(lianxi); return 
     ResponseEntity.ok("User added successfully"); } }

实现查询操作

  1. 编写查询方法

    在Service层中添加查询方法:

     
     @Service public class LianxiService { @Autowired private LianxiMapper lianxiMapper; public 
     Lianxi getLianxiByPhoneAndPassword(String phone, String password) { 
     LambdaQueryWrapper<Lianxi> queryWrapper = new LambdaQueryWrapper<>(); 
     queryWrapper.eq(Lianxi::getPhone, phone) .eq(Lianxi::getPassword, password); return 
     lianxiMapper.selectOne(queryWrapper); } }

  2. 在Controller层调用查询方法

     
     @RestController @RequestMapping("/lianxi") public class LianxiController { @Autowired 
     private LianxiService lianxiService; @PostMapping("/login") public ResponseEntity<String> 
     login(@RequestParam String phone, @RequestParam String password) { Lianxi lianxi = 
     lianxiService.getLianxiByPhoneAndPassword(phone, password); if (lianxi == null) { return 
     ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("User not found or incorrect 
     password"); } else { return ResponseEntity.ok("Login successful"); } } }

通过以上步骤,我们实现了在使用MyBatis和MyBatis Plus进行数据插入时自动生成主键,并进行了简单的查询操作。希望这篇文章对你有所帮助。如果有任何问题或建议,欢迎在评论区留言讨论。

  • 14
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值