MyBatis-Plus数据库增删改查 搭建MyBatis-Plus框架并进行数据库增删改查功能
TZ沅 人气:1想了解搭建MyBatis-Plus框架并进行数据库增删改查功能的相关内容吗,TZ沅在本文为您仔细讲解MyBatis-Plus数据库增删改查的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:MyBatis-Plus数据库增删改查,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); }
加载全部内容