发布于2021-06-12 14:21 阅读(1038) 评论(0) 点赞(21) 收藏(4)
Mybatis-Plus是在Mybatis框架的基础上再次进行封装,从而增强了Mybatis的功能,他最核心的功能就是自动生成CURD语句,使我们不需要再去编写简单的Sql语句,只需要编写一些复杂的需要定制化的Sql语句,而且他还有很多实用的插件可以使用
Mybatis-Plus也提供了Spring Boot的场景启动器,和Spring Boot的整合也很方便
首先需要引入Mybatis-Plus的启动器依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
需要注意的是,Mybatis-Plus的启动器依赖了Mybatis的核心依赖包,所以引入了Mybatis-Plus的启动器后不要再引入Mybaits的相关依赖
设置Mybatis的配置使用mybatis-plus替代mybatis作为前缀,其属性跟mybatis下的属性是一样的
mybatis-plus:
configuration:
cache-enabled: true
lazy-loading-enabled: true
map-underscore-to-camel-case: true
type-aliases-package: com.blackball.entity
mybatis-plus还为我们配置了默认的Sql映射文件的包扫描路径,默认值是classpath*:/mapper/**/*.xml
首先创建一个Mapper接口,然后继承BaseMapper
类,设置泛型为对应的实体类
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
测试之前我们打开mybatis-plus的Sql打印功能
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
测试接口
@SpringBootTest
class SpringDemoApplicationTests {
@Autowired
UserMapper userMapper;
@Test
void contextLoads() {
List<User> users = userMapper.selectList(null);
users.forEach(System.out::println);
}
}
测试结果如下
自动生成了Sql语句
原文链接:https://blog.csdn.net/blackball1998/article/details/117718604
作者:小胖子爱java
链接:http://www.javaheidong.com/blog/article/222271/e5b2cbc973bca64af936/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!