搭建MyBatis-Plus框架並進行數據庫增刪改查功能
搭建MyBatis-Plus框架並進行數據庫添加
MyBatis-Plus(簡稱 MP)是一個 MyBatis 的增強工具,在 MyBatis 的基礎上隻做增強不做改變,為簡化開發、提高效率而生。
1.搭建環境
Jdk:jdk1.8.0_261
Idea:IntelliJ IDEA
Maven:apache-maven-3.3.6
MySQL:mysql-5.5.61
2.項目搭建
3.配置maven
4.項目結構
創建數據庫
CREATE DATABASE tzy03; USE tzy03; CREATE TABLE user( id INT(11) PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50), pass VARCHAR(50), ); INSERT INTO user(name,passs) VALUES('Tom','1215'); SELECT * FROM user;
新建完成以後,打開pom.xml後添加以下依賴:
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.49</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> </dependency>
5.配置 MapperScan 註解
掃描mapper層
package com.tzy.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @MapperScan("com.tzy.demo.mapper") @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
成功界面
6.創建實體
package com.tzy.demo.pojo; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; public class user { @TableId(value = "id") public int id; @TableField(value = "name") public String name; @TableField(value = "pass") public String pass; @Override public String toString() { return "user{" + "id=" + id + ", name='" + name + '\'' + ", pass='" + pass + '\'' + '}'; } public user(){} public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } }
7.創建接口
package com.tzy.demo.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.tzy.demo.pojo.user; import org.springframework.stereotype.Repository; @Repository public interface Userdao extends BaseMapper<user> { }
8.測試
在test文件裡自動生成DemoApplicationTests類裡往數據庫中添加數據進行測試。
package com.tzy.demo; import com.tzy.demo.mapper.Userdao; import com.tzy.demo.pojo.user; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class DemoApplicationTests { @Autowired private Userdao userdao; @Test void insert(){ user user1=new user(); user1.setName("田哲沅"); user1.setPass("1218"); int result=userdao.insert(user1); System.out.println("result:"+result); System.out.println(user1);//id會自動回填到user對象中 } }
成功界面
8.1 updateById
需求信息:將id為1的員工的姓名更改為”tzy”
@Test void testUpdateById() { user user2=userdao.selectById(1); user2.setName("tzy"); int result = userdao.updateById(user2); System.out.println(result); } }
8.2 selectById
查詢id為1的員工信息
@Test void selectById() { user user2=userdao.selectById(1); System.out.println(user2); } }
8.3 deleteById
刪除id為1的員工信息
@Test void testDeleteById() { int result = userdao.deleteById(1); System.out.println(result); }
到此這篇關於搭建MyBatis-Plus框架並進行數據庫增刪改查的文章就介紹到這瞭,更多相關MyBatis-Plus數據庫增刪改查內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- Mybatis分頁查詢的實現(Rowbounds和PageHelper)
- 使用IDEA配置Mybatis-Plus框架圖文詳解
- Spring框架基於註解開發CRUD詳解
- MyBatis-Plus使用ActiveRecord(AR)實現CRUD
- 一篇超詳細的Spring Boot整合Mybatis文章