SpringCloud中怎么利用MyBatis-Plus实现CRUD,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version></dependency>
到maven仓库查看适用的mysql驱动,5.7的没有,8.0兼容5.7的,所以选择8.0的驱动
<!--添加mysql驱动--><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.17</version></dependency>
添加application.yml 文件配置数据源
文件路径:/passjava-question/src/main/resources/application.yml
spring: datasource: driver-class-name:com.mysql.cj.jdbc.Driver url:jdbc:mysql://129.211.188.xxx:3306/passjava_admin?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username:root password:xxx
配置mapper映射文件路径
mybatis-plus: mapper-locations: classpath:/mapper/**/*.xml global-config: db-config: id-type: auto
添加MapperScan注解
@MapperScan("com.jackson0714.passjava.question.dao")@SpringBootApplicationpublicclass PassjavaQuestionApplication { public static void main(String[] args) { SpringApplication.run(PassjavaQuestionApplication.class, args); }}
创建类型为javaBasic的type表数据
@AutowiredTypeService typeService;// 创建题目类型@Testvoid testCreateType() { TypeEntity typeEntity = new TypeEntity(); typeEntity.setType("javaBasic"); typeService.save(typeEntity); System.out.println("创建成功");}
更新id=1的表数据
// 更新type=jvm@Testvoid testUpdateType() { TypeEntity typeEntity = new TypeEntity(); typeEntity.setId(1L); typeEntity.setType("jvm"); typeService.updateById(typeEntity); System.out.println("修改成功");}
查询id=1的表数据
// 查询题目类型@Testvoid testSelectType() { List<TypeEntity> typeEntityList = typeService.list(new QueryWrapper<TypeEntity>().eq("id",1L)); typeEntityList.forEach((item)-> { System.out.println(item); }); System.out.println("查询成功");}
删除id=1的表数据
// 删除题目类型记录@Testvoid testRemoveType() { typeService.removeById(1L); System.out.println("删除成功");}
看完上述内容,你们掌握SpringCloud中怎么利用MyBatis-Plus实现CRUD的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/4499317/blog/4648297