Mybatis Plus
Mybatis Plus介绍:MyBatis-Plus](简称 MP)是一个MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生
中文文档: https://mybatis.plus
集成Mybatis Plus
1:pom.xml导入坐标
1.1:安装lombok
<dependencies>
<!-- mybatis-plus支持 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
<!-- mysql 驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
2:application.yml中配置数据库连接信息
spring:
application:
name: schedule-service #服务名
datasource:
url: jdbc:mysql://192.168.200.129:3306/chongba_schedule?serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
3:实体类与表字段的映射关系
package com.chongba.schedule.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.io.Serializable;
import java.util.Date;
/**
* Created by 噎翔
*/
@Data
@ToString
@NoArgsConstructor
@TableName("taskinfo") //表名
public class TaskInfoEntity implements Serializable{
//序列化ID
private static final long serialVersionUID = -3218564191925663119L;
/*
id生成策略分别是:
AUTO 数据库ID自增
INPUT 用户输入ID
ID_WORKER 全局唯一ID,Long类型的主键
ID_WORKER_STR 字符串全局唯一ID
UUID 全局唯一ID,UUID类型的主键
NONE 该类型为未设置主键类型id
*/
@TableId(type = IdType.ID_WORKER)//主键使用雪花算法生成
private Long taskId;
@TableField
private Date executeTime;
@TableField
private Integer priority;
@TableField
private Integer taskType;
@TableField
private byte[] parameters;
}
为何实现Serializable类:
/*
知识小贴士:什么情况下需要序列化?(实现Serializable)
1:当你想把的内存中的对象状态保存到一个文件中或者数据库中时候;
2:当你想用套接字在网络上传送对象的时候;
3:当你想通过RMI传输对象的时候;
Java 提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个字节序列,该字节序列包括该对象的
数据、有关对象的类型的信息和存储在对象中数据的类型。将序列化对象写入文件之后,可以从文件中读取出来,并
且对它进行反序列化
*/
设置快速生成序列化id
Alt+Enter
4:创建通用mapper
/**
* Created by 噎翔
*/
public interface TaskInfoMapper extends BaseMapper<TaskInfoEntity>{
@Select("select * from taskinfo")
public List<TaskInfoEntity> selectAll();
}
5:启动类添加mapper扫描注解(完成)
/**
* @author 噎翔
* @date 2020/7/1 18:40
*/
@SpringBootApplication
@MapperScan("com.chongba.schedule.mapper") //扫描mapper包
public class ScheduleApplication {
public static void main(String[] args) {
SpringApplication.run(ScheduleApplication.class,args);
}
}
6:创建测试类进行测试
/**
* Created by 噎翔
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ScheduleApplication.class)
public class TaskInfoMapperTest {
@Autowired
private TaskInfoMapper taskInfoMapper;
@Test
public void test1(){
//添加
TaskInfoEntity taskInfoEntity = new TaskInfoEntity();
taskInfoEntity.setExecuteTime(new Date());
taskInfoEntity.setPriority(1);
taskInfoEntity.setTaskType(1001);
taskInfoEntity.setParameters("test".getBytes());
taskInfoMapper.insert(taskInfoEntity);
}
@Test
public void test4(){
//条件查询
QueryWrapper<TaskInfoEntity> queryWrapper = new QueryWrapper<TaskInfoEntity>();
queryWrapper.eq("task_type",1002); // where task_type = 1002
queryWrapper.orderByDesc("priority"); // order by priority desc
List<TaskInfoEntity> entities = taskInfoMapper.selectList(queryWrapper);
for (TaskInfoEntity entity : entities) {
System.out.println(entity);
}
}
@Test
public void test5(){
//查询
List<TaskInfoEntity> entities = taskInfoMapper.selectAll();
for (TaskInfoEntity entity : entities) {
System.out.println(entity);
}
}
}
成功返回结果
数据库数据: