pom.xml 导入 mybatisplus-starter,mysql和驱动,如下:
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
application.yml 配置数据库连接信息
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.126.156:3306/test
username: root
password: 123456
用户实体
@Data
public class User {
public long id;
public String name;
}
创建数据库操作类
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
增删改查测试
@SpringBootTest
public class MybatisPlusTest {
@Autowired
private UserMapper userMapper;
@Test
public void test(){
// 根据条件查询
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getName, "terry");
List<User> list = userMapper.selectList(queryWrapper);
System.out.println(list);
// 插入
userMapper.insert(user);
// 修改
userMapper.updateById(user);
// 删除
userMapper.deleteById("1");
}
}