How to use MyBatis-Plus to realize CRUD in SpringCloud
It is believed that many inexperienced people have no idea about how to use MyBatis-Plus to achieve CRUD in SpringCloud. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
1. Add Mybatis-Plus dependency
Com.baomidou
Mybatis-plus-boot-starter
3.2.0
two。 Configure the driver of the data source import database to view mysql version 5.7.29
Mark
Go to the maven warehouse to check the applicable mysql driver. There is no mysql driver in 5.7, and 8.0 is compatible with 5.7.So choose 8.0 driver.
Mysql
Mysql-connector-java
8.0.17
3. Configure MyBatis-Plus
Add application.yml file configuration data source
File path: / 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
Configure the mapper mapping file path
Smart prompt mybatis-plus when configuring mabatis-plus:
Mapper-locations: classpath:/mapper/**/*.xml
Global-config:
Db-config:
Id-type: auto
Add MapperScan comment
@ MapperScan ("com.jackson0714.passjava.question.dao")
@ SpringBootApplication
Publicclass PassjavaQuestionApplication {
Public static void main (String [] args) {
SpringApplication.run (PassjavaQuestionApplication.class, args)
}
}
4. Test the CRUD method of mybatis-plus
Create type data of type javaBasic
@ Autowired
TypeService typeService
/ / create a question type
@ Test
Void testCreateType () {
TypeEntity typeEntity = new TypeEntity ()
TypeEntity.setType ("javaBasic")
TypeService.save (typeEntity)
System.out.println ("created successfully")
}
Create type data of type javaBasic
Update the table data of id=1
/ / Update type=jvm
@ Test
Void testUpdateType () {
TypeEntity typeEntity = new TypeEntity ()
TypeEntity.setId (1L)
TypeEntity.setType ("jvm")
TypeService.updateById (typeEntity)
System.out.println ("modified successfully")
}
Update the table data of id=1
Query the table data of id=1
/ / query question type
@ Test
Void testSelectType () {
List typeEntityList = typeService.list (new QueryWrapper () .eq ("id", 1L))
TypeEntityList.forEach ((item)-> {
System.out.println (item)
});
System.out.println (query success)
}
Query the table data of id=1
Delete id=1 table data
/ / Delete the record of question type
@ Test
Void testRemoveType () {
TypeService.removeById (1L)
System.out.println ("deleted successfully")
}
Delete id=1 table data after reading the above, do you know how to use MyBatis-Plus to achieve CRUD in SpringCloud? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!