Python 官方文档:入门教程 => 点击学习
目录1.搭建环境2.项目搭建3.配置Maven4.项目结构5.配置 MapperScan 注解 6.创建实体7.创建接口8.测试 8.1 updateById 8.2 se
搭建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框架并进行数据库增删改查的文章就介绍到这了,更多相关MyBatis-Plus数据库增删改查内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: 搭建MyBatis-Plus框架并进行数据库增删改查功能
本文链接: https://lsjlt.com/news/121557.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0