文章目录
- [1、创建MyBatis Plus简单工程](#1、创建MyBatis Plus简单工程)
- 2、常用注解
- [3、MyBatis Plus 代码自动生成](#3、MyBatis Plus 代码自动生成)
1、创建MyBatis Plus简单工程
对 MyBatis 的封装,自动完成 SQL 语句的执行,以及结果集的映射,提供了很多接口,开发者可以直接调用,直接拿到结果,不需要定义 SQL 以及结果集到 Java 的映射。
pom.xml文件导入依赖
xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.1</version>
</dependency>
创建实体类
java
package com.southwind.entity;
import lombok.Data;
@Data
public class User {
private Integer id;
private String username;
private String password;
private Integer age;
}
接口继承BaseMapper
java
package com.southwind.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.southwind.entity.User;
public interface UserMapper extends BaseMapper<User> {
}
创建启动类
java
package com.southwind;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.southwind.mapper")
public class Mp001Application {
public static void main(String[] args) {
SpringApplication.run(Mp001Application.class, args);
}
}
配置yaml文件
yaml
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/test12
driver-class-name: com.mysql.cj.jdbc.Driver
启动测试
java
package com.southwind;
import com.southwind.entity.User;
import com.southwind.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.List;
@SpringBootTest
class Mp001ApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
void contextLoads() {
List<User> list = this.userMapper.selectList(null);
for (User user : list) {
System.out.println(user);
}
}
}
如果实体类中的成员变量和数据表中的字段不一致,可以通过注解的形式进行映射解决。
java
package com.southwind.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("user")
public class Account {
@TableId("id")
private Integer num;
@TableField("password")
private String name;
@TableField("username")
private String pwd;
@TableField("age")
private Integer score;
}
2、常用注解
@TableName 映射实体类到表名
@TableId 映射主键
type:
AUTO(0), 数据库ID自增
NONE(1), 没有设置
INPUT(2), 添加之前要手动set值
ASSIGN_ID(3), MP分配,Number/String
ASSIGN_UUID(4) MP分配,String
@TableField 映射其他字段
exist、select = false,实体类中有额外的字段,和数据表不一致
fill 自动填充,MP 自动给字段赋值,常用于时间类型的数据
java
@TableField(value = "create_time",fill = FieldFill.INSERT)
private Date createTime;
@TableField(value = "update_time",fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
配置处理器
java
package com.southwind.handler;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class DateHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.setFieldValByName("createTime", new Date(),metaObject);
this.setFieldValByName("updateTime", new Date(),metaObject);
}
@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updateTime", new Date(),metaObject);
}
}
3、MyBatis Plus 代码自动生成
MyBatis 自动生成:根据数据表,自动生成实体类、Mapper 接口、Mapper.xml
MP 在此基础上,还可以自动生成 Controller、Service
1、pom.xml
xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
2、启动类
java
package com.htl;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
//创建对象
AutoGenerator autoGenerator = new AutoGenerator();
//数据源
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.MYSQL);
dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/mybatis_plus_note");
dataSourceConfig.setUsername("root");
dataSourceConfig.setPassword("password");
autoGenerator.setDataSource(dataSourceConfig);
//全局配置
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setOutputDir(System.getProperty("user.dir")+"/mp_generator001/src/main/java");
globalConfig.setAuthor("作者名");
globalConfig.setOpen(false);
//去掉Service的I
globalConfig.setServiceName("%sService");
autoGenerator.setGlobalConfig(globalConfig);
//包配置
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent("com.htl");
packageConfig.setEntity("entity");
packageConfig.setMapper("mapper");
packageConfig.setService("service");
packageConfig.setServiceImpl("service.impl");
packageConfig.setController("controller");
autoGenerator.setPackageInfo(packageConfig);
//策略配置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setInclude("user"); // 选择对应的表
strategyConfig.setNaming(NamingStrategy.underline_to_camel);// 下划线转驼峰 (针对表名)
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);// 下划线转驼峰(针对字段名)
strategyConfig.setEntityLombokModel(true);// 添加Lombok注解
TableFill tableFill = new TableFill("create_time", FieldFill.INSERT);
TableFill tableFill2 = new TableFill("update_time", FieldFill.INSERT_UPDATE);
List<TableFill> list = Arrays.asList(tableFill,tableFill2);
strategyConfig.setTableFillList(list);
autoGenerator.setStrategy(strategyConfig);
//启动
autoGenerator.execute();
}
}