哈哈,首先请允许我吐槽一下:最近的工作很久没接触后端了,有点手生,特地回来复习一下 java 的知识。经过仔细斟酌,觉得学习的事情还是得记录下来才行,毕竟,人的脑容量有限, 而学习的路程却是漫长而又遥远。
好了,废话不多说,直入主题吧!
一、运行数据库脚本
1、创建表
DROP TABLE IF EXISTS user;
CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
2、插入数据
DELETE FROM user;
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');
二、在 pom.xml 文件中引入依赖:
<!-- SpringBoot启动依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- springboot 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- MybatisPlus依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>compile</scope>
</dependency>
三、yml 文件中配置数据库驱动
# mysql
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/project-demo01?useUnicode=true&characterEncoding=UTF-8&useSSL=false
# 用户名
username: root
# 密码
password: root
四、创建 User 实体类
package com.hzs.demo1.entity;
import lombok.*;
// Data注解提供 setter、getter、toString、EqualsAndHashCode 方法
@Data
// 满参构造器
@AllArgsConstructor
// 空参构造器
@NoArgsConstructor
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
五、在 启动类上面开启 MybatisPlus 的 mapper 扫描:
六、创建 UserMapper 文件
package com.hzs.demo1.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hzs.demo1.entity.User;
public interface UserMapper extends BaseMapper<User> {
}
七、创建测试类 UserTest
package com.hzs.demo1.test01;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.demo1.entity.User;
import com.hzs.demo1.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.*;
@SpringBootTest
public class UserTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelect() {
System.out.println(("----- 查询所有,queryWrapper 为 null 代表不带查询条件,默认查所有 ------"));
List<User> userList = userMapper.selectList(null);
userList.forEach(System.out::println);
System.out.println("-------------------条件查询---------------------");
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.lt("age", "23").orderByDesc("age");
List<User> users = userMapper.selectList(queryWrapper);
for (User user1 : users) {
System.out.println("user1 = " + user1);
}
}
@Test
public int insert () {
User user = new User();
user.setAge(22);
user.setName("晓晓");
user.setEmail("qq@.com");
int count = userMapper.insert(user);
return count;
}
}
最后。不出意外应该可以在控制台看到:
八、最后
附上源码供参考:
链接:https://pan.baidu.com/s/1rWQusEGskWxY1Ud3CraH7Q 提取码:bb8e