搭建MyBatis-Plus框架并进行数据库增删改查功能
程序员文章站
2022-03-30 11:04:37
搭建mybatis-plus框架并进行数据库添加mybatis-plus(简称 mp)是一个 mybatis 的增强工具,在 mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。1.搭...
搭建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数据库增删改查内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!