在这篇文章中,我们将学习如何使用MyBatis和MyBatis Plus实现插入数据时自动生成主键,以及如何进行简单的查询操作。如果你是Java开发者,并且正在使用MyBatis和MyBatis Plus进行数据库操作,这篇文章将非常适合你。
环境准备
-
创建数据库表 首先,我们需要在数据库中创建一个表。在这个示例中,我们使用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 );
-
配置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 Plus 在
application.yml
或application.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
-
实现自动生成主键的插入操作
-
创建实体类
public class Lianxi { private Integer id; private String name; private String password; private String phone; // Getters and Setters }
-
创建Mapper接口
public interface LianxiMapper extends BaseMapper<Lianxi> { }
-
编写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>
-
在Service层调用插入操作
@Service public class LianxiService { @Autowired private LianxiMapper lianxiMapper; public void addLianxi(Lianxi lianxi) { lianxiMapper.insert(lianxi); } }
-
在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"); } }
实现查询操作
-
编写查询方法
在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); } }
-
在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进行数据插入时自动生成主键,并进行了简单的查询操作。希望这篇文章对你有所帮助。如果有任何问题或建议,欢迎在评论区留言讨论。