本篇内容主要讲解“如何搭建MyBatis-Plus框架并进行数据库增删改查功能”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何搭建MyBatis-Plus框架并进行数据库增删改查功能”吧!
搭建MyBatis-Plus框架并进行数据库添加
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
Jdk:jdk1.8.0_261
Idea:IntelliJ IDEA
Maven:apache-maven-3.3.6
MySQL:mysql-5.5.61
创建数据库
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>
扫描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); } }
成功界面
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; } }
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> { }
在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对象中 } }
成功界面
需求信息:将id为1的员工的姓名更改为"tzy"
@Test void testUpdateById() { user user2=userdao.selectById(1); user2.setName("tzy"); int result = userdao.updateById(user2); System.out.println(result); } }
查询id为1的员工信息
@Test void selectById() { user user2=userdao.selectById(1); System.out.println(user2); } }
删除id为1的员工信息
@Test void testDeleteById() { int result = userdao.deleteById(1); System.out.println(result); }
到此,相信大家对“如何搭建MyBatis-Plus框架并进行数据库增删改查功能”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。