MyBatisPlus操作

本文详细介绍了MyBatisPlus(MP)的快速入门、常见配置、主键生成策略和通用CRUD操作,包括实体类注解、Mapper接口、分页配置、条件构造器Wrapper的使用,并提供了代码示例和资源链接。
摘要由CSDN通过智能技术生成

MyBatiesPlus入门

非常适用于单表之间的操作
demo的代码链接
链接:https://pan.baidu.com/s/1jh4tTgVll-5kKe1b12Aoug
提取码:goih

1.快速入门
  1. 新建maven工程,pom依赖导入
    当不为SpringBoot项目时单独导入MP依赖
<!‐‐SpringBoot起步依赖‐‐>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter</artifactId>
</dependency>
<!‐‐SpringBoot测试依赖‐‐>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐test</artifactId>
<scope>test</scope>
</dependency>
<!‐‐简化代码的工具包,使用@Data注解省略实体类中的get、set方法‐‐>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!‐‐mybatis‐plus的springboot支持‐‐>
<!‐‐当不为SpringBoot项目时单独导入依赖‐‐>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis‐plus‐boot‐starter</artifactId>
<version>3.1.0</version>
</dependency>
<!‐‐mysql驱动‐‐>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql‐connector‐java</artifactId>
<version>8.0.11</version>
</dependency>
  1. 编写application.properties配置文件,最好使用yaml文件
spring.application.name = yh-MP
spring.datasource.driver‐class‐name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mpdemo?useUnicode=true&characterEncoding=UTF‐
8&serverTimezone=Asia/Shanghai&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
# Logger Config
logging.level.root: debug
  1. 编写pojo实体类
    @Data:lombok的注解,使用它可以省略getter/setter方法。
    @NoArgsConstructor: 生成无参构造 方法
    @AllArgsConstructor:生成所有参数构造 方法,参数顺序与属性定义顺序一致。
    @TableName:指定表名
    @TableId:指定主键名
    @TableField:指定列名:注意:当某属性在表字段中没有时需要设置属性exist = false,否则会报错
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_user")
public class User {
   
@TableId("ID")
private Long id;
@TableField("USER_NAME")
private String userName; //驼峰命名,则无需注解
@TableField("PASSWORD")
private String password;
@TableField("NAME")
private String name;
@TableField("AGE")
private Integer age;
@TableField("EMAIL")
private String email;
@TableField("BIRTHDAY")
private LocalDateTime birthday;
}

数据库建表sql如下

‐‐ 创建测试表
CREATE TABLE `tb_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_name` varchar(20) NOT NULL COMMENT '用户名',
`password` varchar(20) NOT NULL COMMENT '密码',
`name` varchar(30) DEFAULT NULL COMMENT '姓名',
`age` int(11) DEFAULT NULL COMMENT '年龄',
`email` varchar(50) DEFAULT NULL COMMENT '邮箱',
`birthday` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
‐‐ 插入测试数据
insert into `tb_user`(`id`,`user_name`,`password`,`name`,`age`,`email`,`birthday`) values
(1,'zhangsan','123456','张三',18,'test1@itcast.cn','2019‐09‐26 11:42:01'),
(2,'lisi','123456','李四',20,'test2@itcast.cn','2019‐10‐01 11:42:08'),
(3,'wangwu','123456','王五',28,'test3@itcast.cn','2019‐10‐02 11:42:14'),
(4,'zhaoliu','123456','赵六',21,'test4@itcast.cn','2019‐10‐05 11:42:18'),
(5,'sunqi','123456','孙七',24,'test5@itcast.cn','2019‐10‐14 11:42:23');
  1. 编写Mapper接口
    UserMapper接口需要继承MP提供的BaseMapper< T >接口。
    此处为BaseMapper< User>
public interface UserMapper extends BaseMapper<User> {
   
}
  1. 编写SpringBoot启动类,添加MapperScan注解,指定mapper接口的扫描包。
@MapperScan("cn.itcast.mp.mapper") //设置mapper接口的扫描包
@SpringBootApplication
public class MyApplication {
   
public static void main(String[] args) {
   
SpringApplication.run(MyApplication.class, args);
}
}
  1. 通过Service层对Mapper进行调用完成单表的CRUD。

2.常见配置

  1. configLocations配置:加载自己的mybatis的xml配置
mybatis‐plus.config‐location = classpath:mybatis‐config.xml
  1. mapperLocations配置:当mpper的xml文件不和map
MybatisPlus是一个优秀的Java持久层框架,可以与ClickHouse数据库进行集成。在操作ClickHouse时,需要对MybatisPlus进行扩展以适应ClickHouse的特性。根据引用和引用,ClickHouse的修改和删除SQL操作与MySQL不同,因此需要对MybatisPlus源码进行扩展。 为了实现SpringBoot与ClickHouse的集成,可以使用dynamic-datasource-spring-boot-starter多数据源的配置。首先,需要引入相关依赖,如引用所述。然后,在配置文件中编写相关配置,包括ClickHouse的连接信息和Druid的配置信息。接下来,在对应的Service类中添加@DS注解,以指定使用ClickHouse数据源进行操作。 在使用MybatisPlus操作ClickHouse时,可以使用MybatisPlus提供的API进行常规的增删改查操作。需要注意的是,由于ClickHouse的特性不同于传统的关系型数据库,可能需要进行一些自定义的类型转换。可以参考引用中的文章,了解如何使用自定义类型转换器来查询ClickHouse的数据。 综上所述,通过对MybatisPlus进行扩展,使用dynamic-datasource-spring-boot-starter进行多数据源配置,并注意ClickHouse的特性进行相应的操作,就可以使用MybatisPlus操作ClickHouse数据库。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot + Mybatis-puls + ClickHouse (分页查询、添加、修改、删除)](https://blog.csdn.net/qq_15371293/article/details/117090780)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [SpringBoot+MybatisPlus集成Clickhouse](https://download.csdn.net/download/qq_36250202/86777605)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [mybatis-plus+clickhouse+druid+多数据源+clickhouse array类型的读取和写入](https://blog.csdn.net/a783295110/article/details/123011034)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值