springboot使用mybatis-plus


1.引入依賴

//mybatis-plus依賴

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1.tmp</version>
</dependency>

// lombok插件依賴 詳解用法:
   <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
2.建立實體類
@Data
@TableName("test")//對應數據庫test表
public class Student {
private String address;
private String name;
}

3.建立test表 並添加數據
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `address` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `create_time` timestamp(0) DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

INSERT INTO `test` VALUES (2, '張三2', '北京', '2019-09-10 12:22:23');
INSERT INTO `test` VALUES (3, '張三3', '北京', '2019-09-05 12:22:23');
INSERT INTO `test` VALUES (4, '李四1', '上海張三', '2019-09-06 12:22:23');
INSERT INTO `test` VALUES (5, '李四2', '上海', '2019-09-07 12:22:23');
INSERT INTO `test` VALUES (6, '李四3', '上海', '2019-09-12 12:22:23');
INSERT INTO `test` VALUES (7, '王二1', '廣州', '2019-09-03 12:22:23');
INSERT INTO `test` VALUES (8, '王二2', '廣州', '2019-09-04 12:22:23');
INSERT INTO `test` VALUES (9, '王二3', '廣州', '2019-09-12 12:22:23');
INSERT INTO `test` VALUES (15, '校長', '上海', '2020-04-28 11:24:16');

SET FOREIGN_KEY_CHECKS = 1;

yml中添加任意一種數據庫配置即可

 

 



4.controller 如下

 

 
        

 


 

 
        

 

 
        

 

其中的mapper

 

 

 
        

 


 

 

相應代碼
/**
* mybatis-plus
* @return
*/
@RequestMapping("/deleteStudent")
@ResponseBody
public Object deleteStudent() {
QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id",1);//此處可以理解為查詢id=1的Stusent
mapper.delete(queryWrapper);
return "delete";
}

/**
* mybatis-plus
* @return
*/
@RequestMapping("/updateStudent")
@ResponseBody
public Object updateStudent() {
QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id",2);
Student student =new Student();
student.setName("改姓名");
mapper.update(student,queryWrapper);
return "update";
}


/**
* mybatis-plus
* @return
*/
@RequestMapping("/getStudentList")
@ResponseBody
public Object getStudent() {
QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
Student student=new Student();
queryWrapper.like("address","");//模糊查詢字段為address 值為北的信息
return mapper.selectList(queryWrapper);
}
 

 

 

 

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM