MyBatisPlus入门案例
-
准备数据库表
-
新建一个springboot工程
-
导包
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-generator --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.4.0</version> </dependency> <!-- 模板引擎 --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency>
注意:引入 MyBatis-Plus之后请不要再次引入MyBatis以及MyBatis-Spring,以避免因版本差异导致的问题
-
在application.yml配置文件中添加数据库的相关配置:
# 配置数据库 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf8&serverTimezone=GMT%2B8 username: root password: 123456 # 配置日志 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
-
在 Spring Boot 启动类中添加@MapperScan注解,扫描 Mapper 文件夹
package com.yl; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.yl.mapper")//扫描mapper类 public class MybatisPlus01Application { public static void main(String[] args) { SpringApplication.run(MybatisPlus01Application.class, args); } }
-
实体类
package com.yl.bean; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; /** * 用户实体类 */ @Data @NoArgsConstructor public class User implements Serializable { private Integer id; private String name; private Integer age; private String email; }
-
Mapper类
package com.yl.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.yl.bean.User; import org.springframework.stereotype.Repository; /** * 用户映射接口 */ @Repository public interface UserMapper extends BaseMapper<User> { }
-
测试类
package com.yl.mybatis_plus_01; import com.yl.bean.User; import com.yl.mapper.UserMapper; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.List; @SpringBootTest @RunWith(SpringRunner.class) class MybatisPlus01ApplicationTests { @Autowired private UserMapper userMapper; /** *查询所有 */ @Test void contextLoads() { List<User> userList=userMapper.selectList(null); userList.forEach(System.out::println); } /** * 添加用户 */ @Test void add(){ User user=new User(); user.setId(6); user.setAge(17); user.setName("yl01"); user.setEmail("123@qq.com"); userMapper.insert(user); } /** * 根据id修改用户 * mybatisplus的修改sql语句是动态sql,没有修改的字段会保留原来的值 */ @Test void update(){ User user=new User(); user.setId(6); user.setEmail("12345@qq.com"); userMapper.updateById(user); } /** * 根据id删除用户 */ @Test void delete(){ userMapper.deleteById(7); } }
复杂的查询(删改同理)
// 测试批量查询! @Test public void testSelectByBatchId(){ //查询id为1、2、3的用户 List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3)); users.forEach(System.out::println); } // 按条件查询之使用map操作 @Test public void testSelectByBatchIds(){ HashMap<String, Object> map = new HashMap<>(); // 自定义要查询,查询name为yl01且age为3的用户 map.put("name","yl01"); map.put("age",3); List<User> users = userMapper.selectByMap(map); users.forEach(System.out::println); }