通用枚举
表中的有些字段值是固定的,例如性别(男或女),此时可以用MP的通用枚举来实现。
数据库表中添加字段
创建通用枚举类型
package com.atguigu.mybatis_plus.enums;
import com.baomidou.mybatisplus.annotation.EnumValue;
import lombok.Getter;
@Getter
public enum SexEnum {
MALE(1,"男"),
FEMALE(0,"女");
@EnumValue
private Integer sex;
private String sexName;
SexEnum(Integer sex,String sexName){
this.sex = sex;
this.sexName = sexName;
}
}
配置扫描通用枚举
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
table-prefix: t_
id-type: auto
type-aliases-package: com.atguigu.mybatis_plus.pojo
type-enums-package: com.atguigu.mybatis_plus.enums
测试
@Test
public void test(){
User user = new User();
user.setName("K");
user.setAge(23);
user.setEmail("K@AG.com");
user.setSex(SexEnum.FEMALE);
int result = userMapper.insert(user);
System.out.println("result:" + result);
}
代码生成器
添加依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
快速生成
package com.atguigu.mybatis_plus;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Collections;
@SpringBootTest
public class FastAutoGeneratorTest {
public static void main(String[] args) {
FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/mybatis_plus?", "root", "123456")
.globalConfig(builder -> {
builder.author("atguigu")
.fileOverride()
.outputDir("D://mybatis_plus");
})
.packageConfig(builder -> {
builder.parent("com.atguigu")
.moduleName("mybatisplus")
.pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D://mybatis_plus"));
})
.strategyConfig(builder -> {
builder.addInclude("user")
.addTablePrefix("t_", "c_");
})
.templateEngine(new FreemarkerTemplateEngine())
.execute();
}
}
多数据源
适用于多种场景:纯粹多库、读写分离、一主多从、混合模式等。
该例中模拟一个纯粹多库的场景,其他场景与之类似。
当前有两个数据库,一个是mybatis_plus,存储user表;另一个表是mybatis_plus_1,存储t_product表。
引入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
配置多数据源
spring:
datasource:
dynamic:
primary: master
strict: false
datasource:
master:
url: jdbc:mysql://localhost:3306/mybatis_plus
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
slave_1:
url: jdbc:mysql://localhost:3306/mybatis_plus_1
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
创建用户Service
package com.atguigu.service;
import com.atguigu.pojo.User;
import com.baomidou.mybatisplus.extension.service.IService;
public interface UserService extends IService<User> {
}
package com.atguigu.service;
import com.atguigu.mapper.UserMapper;
import com.atguigu.pojo.User;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
@DS("master")
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{
}
创建商品Service
package com.atguigu.service;
import com.atguigu.pojo.Product;
import com.baomidou.mybatisplus.extension.service.IService;
public interface ProductService extends IService<Product> {
}
package com.atguigu.service;
import com.atguigu.mapper.ProductMapper;
import com.atguigu.pojo.Product;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
@Service
@DS("slave_1")
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService{
}
测试
@Test
public void test(){
System.out.println(userService.getById(1));
System.out.println(productService.getById(1));
}
MyBatisX
要使用MyBatisX生成代码,首先要连接IDEA中的database。
注:Mysql 8版本需要在Advanced设置时区ServerTimezone为Asia/Shanghai。
打开连接后,右键要生成代码的表,点开MybatisX-Generator:
在MyBatisX生成的Mapper接口中,可以利用MyBatisX插件快速进行CRUD的操作。MybatisX会提示我们可以进行的操作,显而易见的是,我们会看见一只红色的鸟儿。
Alt + Enter,选择第二行,就可以快速生成Sql语句 点击可爱的小红鸟,我们就可以前往UserMapper.xml中对应的sql语句了
|