一、前言
对mybatis-plus进行简单学习,以下是快速构建起mybatis-plus的流程
二、流程
2.1、先在springboot项目中的pom.xml文件中导入依赖
<!-- mybatis-plus -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
2.2、创建实体类,在其中封装好需要的属性,这里使用user类示例
public class User {
private int user_Id;
private String user_Name;
private String user_Age;
}
2.3、需要创建mapper层,UserMapper类
@Mapper
public interface UserMapper extends BaseMapper<User> {}
2.4、service层,UserService类
public interface UserService extends IService<User> {
}
2.5、在service层中创建serviceImpl,创建UserServiceImpl类
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Autowired
UserMapper userMapper;
}
可以在改类中写详细的代码逻辑
例如查刚刚实体类中的属性,存在数据库中的信息
public List<User> allUserinfo(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.select("user_Name","user_Age");
List<User> list = userMapper.selectList(queryWrapper);
return list;
}
2.6、编写controller类即可
三、总结
使用mybatis-plus相对mybatis来说还是方便很多的,但有一些比较复杂的写入、查询运用之前的mybatis会更好一些。